Last change
on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
868 bytes
|
Rev | Line | |
---|
[6a3a178] | 1 | var toNumber = require('./toNumber');
|
---|
| 2 |
|
---|
| 3 | /** Used as references for various `Number` constants. */
|
---|
| 4 | var INFINITY = 1 / 0,
|
---|
| 5 | MAX_INTEGER = 1.7976931348623157e+308;
|
---|
| 6 |
|
---|
| 7 | /**
|
---|
| 8 | * Converts `value` to a finite number.
|
---|
| 9 | *
|
---|
| 10 | * @static
|
---|
| 11 | * @memberOf _
|
---|
| 12 | * @since 4.12.0
|
---|
| 13 | * @category Lang
|
---|
| 14 | * @param {*} value The value to convert.
|
---|
| 15 | * @returns {number} Returns the converted number.
|
---|
| 16 | * @example
|
---|
| 17 | *
|
---|
| 18 | * _.toFinite(3.2);
|
---|
| 19 | * // => 3.2
|
---|
| 20 | *
|
---|
| 21 | * _.toFinite(Number.MIN_VALUE);
|
---|
| 22 | * // => 5e-324
|
---|
| 23 | *
|
---|
| 24 | * _.toFinite(Infinity);
|
---|
| 25 | * // => 1.7976931348623157e+308
|
---|
| 26 | *
|
---|
| 27 | * _.toFinite('3.2');
|
---|
| 28 | * // => 3.2
|
---|
| 29 | */
|
---|
| 30 | function toFinite(value) {
|
---|
| 31 | if (!value) {
|
---|
| 32 | return value === 0 ? value : 0;
|
---|
| 33 | }
|
---|
| 34 | value = toNumber(value);
|
---|
| 35 | if (value === INFINITY || value === -INFINITY) {
|
---|
| 36 | var sign = (value < 0 ? -1 : 1);
|
---|
| 37 | return sign * MAX_INTEGER;
|
---|
| 38 | }
|
---|
| 39 | return value === value ? value : 0;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | module.exports = toFinite;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.