forked from chenbimo/yidash
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
44678e4
commit a2534d3
Showing
2 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |