1
|
var baseClamp = require('./_baseClamp'),
|
2
|
toInteger = require('./toInteger');
|
3
|
|
4
|
/** Used as references for various `Number` constants. */
|
5
|
var MAX_SAFE_INTEGER = 9007199254740991;
|
6
|
|
7
|
/**
|
8
|
* Converts `value` to a safe integer. A safe integer can be compared and
|
9
|
* represented correctly.
|
10
|
*
|
11
|
* @static
|
12
|
* @memberOf _
|
13
|
* @since 4.0.0
|
14
|
* @category Lang
|
15
|
* @param {*} value The value to convert.
|
16
|
* @returns {number} Returns the converted integer.
|
17
|
* @example
|
18
|
*
|
19
|
* _.toSafeInteger(3.2);
|
20
|
* // => 3
|
21
|
*
|
22
|
* _.toSafeInteger(Number.MIN_VALUE);
|
23
|
* // => 0
|
24
|
*
|
25
|
* _.toSafeInteger(Infinity);
|
26
|
* // => 9007199254740991
|
27
|
*
|
28
|
* _.toSafeInteger('3.2');
|
29
|
* // => 3
|
30
|
*/
|
31
|
function toSafeInteger(value) {
|
32
|
return value
|
33
|
? baseClamp(toInteger(value), -MAX_SAFE_INTEGER, MAX_SAFE_INTEGER)
|
34
|
: (value === 0 ? value : 0);
|
35
|
}
|
36
|
|
37
|
module.exports = toSafeInteger;
|