Skip to content

Commit 92eff1d

Browse files
committed
fix(reverse): reverse should not modify original array
1 parent 9052e1e commit 92eff1d

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

index.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
product,
4242
remove,
4343
removeFirst,
44+
reverse,
4445
scan,
4546
scan1,
4647
scanRight,
@@ -90,6 +91,12 @@ test("empty", t => {
9091
t.false(empty([1, 2, 3]));
9192
});
9293

94+
test("reverse", t => {
95+
const a = [1, 2, 4, 3];
96+
t.deepEqual(reverse(a), [3, 4, 2, 1]);
97+
t.deepEqual(a, [1, 2, 4, 3]); // Ensure original array is untouched.
98+
});
99+
93100
test("slice", t => {
94101
t.deepEqual(slice([1, 2, 3, 4], 1), [2, 3, 4]);
95102
t.deepEqual(slice([1, 2, 3, 4, 5], 1, 4), [2, 3, 4]);

index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export function notEmpty<T>(array: ArrayLike<T>): boolean {
107107
}
108108

109109
export function reverse<T>(array: ArrayLike<T>): T[] {
110-
return nativeReverse.call(array);
110+
return nativeReverse.call(copy(array));
111111
}
112112

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

0 commit comments

Comments
 (0)