Skip to content

Commit 2dcd9c9

Browse files
committed
fix(reverse): reverse should not modify original array
1 parent 610d3ec commit 2dcd9c9

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
@@ -43,6 +43,7 @@ import {
4343
product,
4444
remove,
4545
removeFirst,
46+
reverse,
4647
scan,
4748
scan1,
4849
scanRight,
@@ -100,6 +101,12 @@ test("empty", t => {
100101
t.false(empty([1, 2, 3]));
101102
});
102103

104+
test("reverse", t => {
105+
const a = [1, 2, 4, 3];
106+
t.deepEqual(reverse(a), [3, 4, 2, 1]);
107+
t.deepEqual(a, [1, 2, 4, 3]); // Ensure original array is untouched.
108+
});
109+
103110
test("slice", t => {
104111
t.deepEqual(slice([1, 2, 3, 4], 1), [2, 3, 4]);
105112
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
@@ -113,7 +113,7 @@ export function notEmpty<T>(array: ArrayLike<T>): boolean {
113113
}
114114

115115
export function reverse<T>(array: ArrayLike<T>): T[] {
116-
return nativeReverse.call(array);
116+
return nativeReverse.call(copy(array));
117117
}
118118

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

0 commit comments

Comments
 (0)