From 2f2964662221b2c632e1f7036ec6492ae55e5b66 Mon Sep 17 00:00:00 2001 From: Daniel Cassidy Date: Sat, 15 Jan 2022 13:18:06 +0000 Subject: [PATCH] fix(reverse): reverse should not modify original array --- index.test.ts | 7 +++++++ index.ts | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/index.test.ts b/index.test.ts index 0ff41bb8..6d2da170 100644 --- a/index.test.ts +++ b/index.test.ts @@ -43,6 +43,7 @@ import { product, remove, removeFirst, + reverse, scan, scan1, scanRight, @@ -100,6 +101,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]); diff --git a/index.ts b/index.ts index 3a892f03..b1967501 100644 --- a/index.ts +++ b/index.ts @@ -113,7 +113,7 @@ export function notEmpty(array: ArrayLike): boolean { } export function reverse(array: ArrayLike): T[] { - return nativeReverse.call(array); + return nativeReverse.call(copy(array)); } export function slice(array: ArrayLike, start?: number, end?: number): T[] {