Skip to content

Commit f7ab74b

Browse files
committed
fix(reverse): reverse should not modify original array
1 parent 1b4d93a commit f7ab74b

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

index.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import {
4040
prepend,
4141
product,
4242
remove,
43-
removeFirst,
43+
removeFirst, reverse,
4444
scan,
4545
scan1,
4646
scanRight,
@@ -88,6 +88,12 @@ test("empty", t => {
8888
t.false(empty([1, 2, 3]));
8989
});
9090

91+
test("reverse", t => {
92+
const a = [1, 2, 4, 3];
93+
t.deepEqual(reverse(a), [3, 4, 2, 1]);
94+
t.deepEqual(a, [1, 2, 4, 3]) // Ensure original array is untouched.
95+
})
96+
9197
test("slice", t => {
9298
t.deepEqual(slice([1, 2, 3, 4], 1), [2, 3, 4]);
9399
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)