Last change
on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
836 bytes
|
Rev | Line | |
---|
[6a3a178] | 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;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.