From 9896b1ca08019a01c3530fec5921dbf070e12cd5 Mon Sep 17 00:00:00 2001 From: zgavin <234354+zgavin@users.noreply.github.com> Date: Mon, 20 May 2024 10:47:47 -0500 Subject: [PATCH] fix: make min/max non-mutating (#16) --- src/array/max.ts | 5 +++-- src/array/min.ts | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/array/max.ts b/src/array/max.ts index 4fd95f1..05a9a38 100644 --- a/src/array/max.ts +++ b/src/array/max.ts @@ -15,8 +15,9 @@ declare global { Array.prototype.max = function (this: ToArray | T[], fn?: CallbackFn): R { const values = fn ? ((this as T[]).map(fn) as R[]) : (this as R[]); - let max = values.shift(); - for (const value of values) { + let max = values.first; + for (let i = 1; i < values.length; i++) { + const value = values[i]; if (compare(value, max!) > 0) max = value; } return max!; diff --git a/src/array/min.ts b/src/array/min.ts index 3b75d04..46e3946 100644 --- a/src/array/min.ts +++ b/src/array/min.ts @@ -15,8 +15,9 @@ declare global { Array.prototype.min = function (this: ToArray, fn?: CallbackFn): R { const values = fn ? ((this as T[]).map(fn) as R[]) : (this as R[]); - let min = values.shift(); - for (const value of values) { + let min = values.first; + for (let i = 1; i < values.length; i++) { + const value = values[i]; if (compare(value, min!) < 0) min = value; } return min!;