-
Notifications
You must be signed in to change notification settings - Fork 544
fix(pullAt): fix incorrect element removal with negative indices #1613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -18,7 +18,9 @@ import { at } from './at.ts'; | |||||||||||||||||||||||
| */ | ||||||||||||||||||||||||
| export function pullAt<T>(arr: T[], indicesToRemove: number[]): T[] { | ||||||||||||||||||||||||
| const removed = at(arr, indicesToRemove); | ||||||||||||||||||||||||
| const indices = new Set(indicesToRemove.slice().sort((x, y) => y - x)); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const normalizedIndices = indicesToRemove.map(index => (index < 0 ? arr.length + index : index)); | ||||||||||||||||||||||||
|
Comment on lines
+21
to
+22
|
||||||||||||||||||||||||
| 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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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()returnsundefinedfor those, it would be good to add a case assertingpullAtalso returnsundefinedand does not remove any element for such indices.