Skip to content

Commit

Permalink
add yd_math_max 函数
Browse files Browse the repository at this point in the history
  • Loading branch information
ForeverGuo committed Aug 12, 2024
1 parent 44678e4 commit a2534d3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/math/max.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @description 返回array中最大值,如果为空或假值,则返回undefined
* @author grantguo <https://github.com/ForeverGuo>
* @category math
* @alias yd_math_max
* @param { Array } array
* @return 返回array中最大值
* @summary 返回array中最大值
* @example
* yd_math_max([1, 2, 3, 4]);
* // => 4
* yd_math_max([]);
* // => undefined
*
*/

export default (array) => {
if (array.length === 0) return undefined;
return Math.max(...array);
};
12 changes: 12 additions & 0 deletions lib/math/max.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { describe, it, expect } from 'vitest';
import yd_math_max from './max.js';

describe('yd_math_max', () => {
it('it shoule return a number', () => {
const arr = [1, 2, 3, 4];
const number_1 = yd_math_max(arr);
expect(number_1).toEqual(4);
const number_2 = yd_math_max([]);
expect(number_2).toEqual(undefined);
});
});

0 comments on commit a2534d3

Please sign in to comment.