Skip to content

Commit

Permalink
fix(reverse): reverse should not modify original array
Browse files Browse the repository at this point in the history
  • Loading branch information
djcsdy committed Jan 15, 2022
1 parent 9052e1e commit 92eff1d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
7 changes: 7 additions & 0 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
product,
remove,
removeFirst,
reverse,
scan,
scan1,
scanRight,
Expand Down Expand Up @@ -90,6 +91,12 @@ test("empty", t => {
t.false(empty([1, 2, 3]));
});

test("reverse", t => {
const a = [1, 2, 4, 3];
t.deepEqual(reverse(a), [3, 4, 2, 1]);
t.deepEqual(a, [1, 2, 4, 3]); // Ensure original array is untouched.
});

test("slice", t => {
t.deepEqual(slice([1, 2, 3, 4], 1), [2, 3, 4]);
t.deepEqual(slice([1, 2, 3, 4, 5], 1, 4), [2, 3, 4]);
Expand Down
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export function notEmpty<T>(array: ArrayLike<T>): boolean {
}

export function reverse<T>(array: ArrayLike<T>): T[] {
return nativeReverse.call(array);
return nativeReverse.call(copy(array));
}

export function slice<T>(array: ArrayLike<T>, start?: number, end?: number): T[] {
Expand Down

0 comments on commit 92eff1d

Please sign in to comment.