Skip to content

Commit

Permalink
fix: make min/max non-mutating (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
zgavin authored May 20, 2024
1 parent 59dae11 commit 9896b1c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/array/max.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ declare global {

Array.prototype.max = function <T, R extends Comparable>(this: ToArray<Comparable> | T[], fn?: CallbackFn<T, R>): 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!;
Expand Down
5 changes: 3 additions & 2 deletions src/array/min.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ declare global {

Array.prototype.min = function <T, R extends Comparable>(this: ToArray<Comparable>, fn?: CallbackFn<T, R>): 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!;
Expand Down

0 comments on commit 9896b1c

Please sign in to comment.