-
Notifications
You must be signed in to change notification settings - Fork 27
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
Showing
9 changed files
with
62 additions
and
81 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
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
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
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
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
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
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
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 |
---|---|---|
@@ -1,34 +1,15 @@ | ||
import isNil from './is-nil'; | ||
|
||
/** | ||
* @param {Array} arr The array to iterate over. | ||
* @return {*} Returns the maximum value. | ||
* @example | ||
* | ||
* max([1, 2]); | ||
* // => 2 | ||
* | ||
* max([]); | ||
* // => undefined | ||
* | ||
* const data = new Array(1250010).fill(1).map((d,idx) => idx); | ||
* | ||
* max(data); | ||
* // => 1250010 | ||
* // Math.max(...data) will encounter "Maximum call stack size exceeded" error | ||
* 计算数组的最大值 | ||
* @param arr 数组 | ||
* @return 最大值 | ||
*/ | ||
export default function max(arr: number[]): number | undefined { | ||
export default function max(arr: number[]): number { | ||
if (!Array.isArray(arr)) return -Infinity; | ||
const length = arr.length; | ||
if (length === 0) return undefined; | ||
if (!length) return -Infinity; | ||
let max = arr[0]; | ||
|
||
const isNonNumber = (value: number) => isNil(value) || isNaN(value); | ||
if (isNonNumber(max)) return undefined; | ||
|
||
for (let i = 1; i < length; i++) { | ||
if (isNonNumber(arr[i])) return undefined; | ||
if (arr[i] > max) max = arr[i]; | ||
max = Math.max(max, arr[i]); | ||
} | ||
|
||
return max; | ||
} |
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