Skip to content

Commit

Permalink
revert: min, max 기존 코드로 복구 및 테스트 코드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
ssi02014 committed Jun 25, 2024
1 parent 4c3c0e0 commit 0666aba
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
10 changes: 5 additions & 5 deletions packages/utils/src/math/max/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ export function max<T>(
arr: T[] | readonly T[],
iteratee: (item: T) => number = identity as (item: T) => number
) {
let maxValue = iteratee(arr[0]);
let maxValue = Number.MIN_SAFE_INTEGER;
let maxItem = arr[0];

for (let i = 1; i < arr.length; i++) {
const value = iteratee(arr[i]);
arr.forEach((item) => {
const value = iteratee(item);

if (value > maxValue) {
maxItem = arr[i];
maxItem = item;
maxValue = value;
}
}
});

return maxItem;
}
11 changes: 10 additions & 1 deletion packages/utils/src/math/max/max.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,19 @@ describe('max', () => {
expect(result).toBe('banana');
});

it('should handle empty array', () => {
it('should handle empty array(default)', () => {
const arr: number[] = [];
const result = max(arr);

expect(result).toBeUndefined();
});

it('should handle empty array(iteratee)', () => {
const arr: {
value: number;
}[] = [];
const result = max(arr, (item) => item.value);

expect(result).toBeUndefined();
});
});
10 changes: 5 additions & 5 deletions packages/utils/src/math/min/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ export function min<T>(
arr: T[] | readonly T[],
iteratee: (item: T) => number = identity as (item: T) => number
) {
let minValue = iteratee(arr[0]);
let minValue = Number.MAX_SAFE_INTEGER;
let minItem = arr[0];

for (let i = 1; i < arr.length; i++) {
const value = iteratee(arr[i]);
arr.forEach((item) => {
const value = iteratee(item);

if (value < minValue) {
minItem = arr[i];
minItem = item;
minValue = value;
}
}
});

return minItem;
}
9 changes: 9 additions & 0 deletions packages/utils/src/math/min/min.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,13 @@ describe('min', () => {

expect(result).toBeUndefined();
});

it('should handle empty array(iteratee)', () => {
const arr: {
value: number;
}[] = [];
const result = min(arr, (item) => item.value);

expect(result).toBeUndefined();
});
});

0 comments on commit 0666aba

Please sign in to comment.