fix(query-core): addToEnd caps result to max when items already exceeds max#10969
fix(query-core): addToEnd caps result to max when items already exceeds max#10969JSap0914 wants to merge 1 commit into
Conversation
…ds max addToEnd(items, item, max) used slice(1) which only ever removed one element. When items.length > max (e.g. maxPages reduced at runtime), the returned array could still exceed max. Replace slice(1) with slice(-max) so the result is always the last max elements of the extended array, regardless of input length.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughIn ChangesaddToEnd max-cap fix
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Bug
addToEnd(items, item, max)usesslice(1)which only ever removes one element from the front. Whenitems.lengthis already greater thanmax, the returned array still exceedsmax.This matters in practice for infinite queries with a dynamic
maxPagesoption: if the user reducesmaxPagesbetween fetches (e.g. via React state),addToEndreceives apagesarray that is longer than the newmax. The result silently accumulates more pages than the configured limit.Before (broken)
After (fix)
Fix
Replace
newItems.slice(1)withnewItems.slice(-max)inaddToEnd.All four existing
addToEndtests are unaffected: they all useitems.length === max, where both expressions produce the same result.addToStarthas the symmetric issue (slice(0, -1)), but an existing test already asserts the current behavior, so that change warrants a separate discussion.Verification
All 5 tests pass (4 pre-existing + 1 new regression test).
Summary by CodeRabbit
Bug Fixes
addToEndfunction to correctly limit array length when items already exceed the maximum capacity, ensuring proper array truncation and maintaining size constraints.Tests
addToEndfunction to verify correct behavior when handling oversized arrays.