Skip to content

fix(pullAt): fix incorrect element removal with negative indices#1613

Open
eunwoo-levi wants to merge 3 commits intotoss:mainfrom
eunwoo-levi:fix/pullAt-negative-indices
Open

fix(pullAt): fix incorrect element removal with negative indices#1613
eunwoo-levi wants to merge 3 commits intotoss:mainfrom
eunwoo-levi:fix/pullAt-negative-indices

Conversation

@eunwoo-levi
Copy link
Copy Markdown
Contributor

@eunwoo-levi eunwoo-levi commented Feb 11, 2026

Problem

When multiple negative indices are passed to pullAt, earlier splice calls shift the array length, causing subsequent negative indices to reference wrong elements.

  const arr = [10, 20, 30, 40, 50];
  pullAt(arr, [-1, -2]);
  // Expected: arr = [10, 20, 30]  (remove 50 and 40)
  // Actual:   arr = [10, 20, 40]  (remove 50 and 30)

Solution

Normalize negative indices to positive indices before splicing, so that array length changes don't affect which elements are removed.

Test

  • Added tests for single, multiple, and mixed negative indices

Copilot AI review requested due to automatic review settings February 11, 2026 08:32
@vercel
Copy link
Copy Markdown

vercel bot commented Feb 11, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
es-toolkit Ready Ready Preview, Comment Apr 10, 2026 5:18pm

Request Review

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/array/pullAt.ts
Comment on lines +21 to +22

const normalizedIndices = indicesToRemove.map(index => (index < 0 ? arr.length + index : index));
Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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);

Copilot uses AI. Check for mistakes.
Comment thread src/array/pullAt.spec.ts
Comment on lines +61 to +67
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]);
});
Copy link

Copilot AI Feb 11, 2026

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() 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.

Copilot uses AI. Check for mistakes.
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.97%. Comparing base (25f7f89) to head (d3c02ea).

Additional details and impacted files

Impacted file tree graph

@@           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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants