Skip to content
24 changes: 24 additions & 0 deletions src/array/pullAt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,30 @@ describe('pullAt', () => {
expect(actual).toEqual([2, 4, 12, 8, 6, 10]);
});

it('should work with a single negative index', () => {
const array = [10, 20, 30, 40, 50];
const result = pullAt(array, [-1]);

expect(array).toEqual([10, 20, 30, 40]);
expect(result).toEqual([50]);
});

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]);
});
Comment on lines +61 to +67

Copilot AI Feb 11, 2026

Copy link

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.

it('should work with mixed positive and negative indices', () => {
const array = [10, 20, 30, 40, 50];
const result = pullAt(array, [0, -1]);

expect(array).toEqual([20, 30, 40]);
expect(result).toEqual([10, 50]);
});

it('should work with objects', () => {
const foo = { foo: 1 };
const bar = { foo: 2 };
Expand Down
2 changes: 1 addition & 1 deletion src/array/pullAt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { at } from './at.ts';
*/
export function pullAt<T>(arr: T[], indicesToRemove: readonly number[]): T[] {
const removed = at(arr, indicesToRemove);
const indices = new Set(indicesToRemove.slice().sort((x, y) => y - x));
const indices = new Set(indicesToRemove.map(index => (index < 0 ? index + arr.length : index)).sort((x, y) => y - x));

for (const index of indices) {
arr.splice(index, 1);
Expand Down