fix(pullAt): fix incorrect element removal with negative indices#1613
fix(pullAt): fix incorrect element removal with negative indices#1613eunwoo-levi wants to merge 3 commits intotoss:mainfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Pull request overview
Fixes pullAt so multiple negative indices remove the intended elements by converting negative indices to their positive equivalents before splicing, preventing index drift caused by earlier splice() calls.
Changes:
- Normalize negative indices to positive indices prior to sorting/deduping and splicing.
- Add unit tests covering single, multiple, and mixed negative indices.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/array/pullAt.ts |
Normalizes negative indices before sorting and splicing to avoid incorrect removals. |
src/array/pullAt.spec.ts |
Adds coverage for negative-index scenarios (single, multiple, and mixed). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| const normalizedIndices = indicesToRemove.map(index => (index < 0 ? arr.length + index : index)); |
There was a problem hiding this comment.
normalizedIndices only adds arr.length for negative values, but doesn’t apply the same integer coercion / bounds handling as at(). This can cause pullAt to mutate the array for out-of-range negative indices (e.g. index < -arr.length becomes a negative normalized index and splice will remove from the end) even though at() returns undefined for those keys. Consider normalizing indices using the same rules as at() (truncate non-integers, add length for negatives) and then filtering out indices that are still out of [0, length) before splicing, so nonexistent indices don’t remove unrelated elements.
| const normalizedIndices = indicesToRemove.map(index => (index < 0 ? arr.length + index : index)); | |
| const length = arr.length; | |
| const normalizedIndices = indicesToRemove | |
| .map((index) => { | |
| let idx = Math.trunc(index); | |
| if (idx < 0) idx += length; | |
| return idx; | |
| }) | |
| .filter((idx) => idx >= 0 && idx < length); |
| it('should work with multiple negative indices', () => { | ||
| const array = [10, 20, 30, 40, 50]; | ||
| const result = pullAt(array, [-1, -2]); | ||
|
|
||
| expect(array).toEqual([10, 20, 30]); | ||
| expect(result).toEqual([50, 40]); | ||
| }); |
There was a problem hiding this comment.
The new negative-index tests cover in-range negatives, but there’s still no coverage for out-of-range negatives (e.g. index < -array.length). Given at() returns undefined for those, it would be good to add a case asserting pullAt also returns undefined and does not remove any element for such indices.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1613 +/- ##
=======================================
Coverage 99.97% 99.97%
=======================================
Files 497 497
Lines 4661 4662 +1
Branches 1347 1348 +1
=======================================
+ Hits 4660 4661 +1
Misses 1 1 🚀 New features to boost your workflow:
|
Problem
When multiple negative indices are passed to
pullAt, earliersplicecalls shift the array length, causing subsequent negative indices to reference wrong elements.Solution
Normalize negative indices to positive indices before splicing, so that array length changes don't affect which elements are removed.
Test