Add _.toNumber.

This commit is contained in:
Xotic750
2015-10-31 20:19:08 +01:00
committed by John-David Dalton
parent 4bc35e9ed4
commit 37955345ef
2 changed files with 366 additions and 4 deletions

View File

@@ -54,7 +54,8 @@
/** Used as references for various `Number` constants. */
var INFINITY = 1 / 0,
MAX_SAFE_INTEGER = 9007199254740991,
MAX_INTEGER = 1.7976931348623157e+308;
MAX_INTEGER = 1.7976931348623157e+308,
NAN = 0 / 0;
/** Used as references for the maximum length and index of an array. */
var MAX_ARRAY_LENGTH = 4294967295,
@@ -126,11 +127,20 @@
var reFlags = /\w*$/;
/** Used to detect hexadecimal string values. */
var reHasHexPrefix = /^0[xX]/;
var reHasHexPrefix = /^0x/i;
/** Used to detect bad signed hex string values. */
var reIsBadHex = /^[-+]0x[0-9a-f]+$/i;
/** Used to detect binary string values. */
var reIsBinary = /^0b[0-1]+$/i;
/** Used to detect host constructors (Safari > 5). */
var reIsHostCtor = /^\[object .+?Constructor\]$/;
/** Used to detect octal string values. */
var reIsOctal = /^0o[0-7]+$/i;
/** Used to detect unsigned integer values. */
var reIsUint = /^(?:0|[1-9]\d*)$/;
@@ -9853,7 +9863,7 @@
* Converts `value` to an integer suitable for use as the length of an
* array-like object.
*
* **Note:** This function is based on [`ToLength`](http://ecma-international.org/ecma-262/6.0/#sec-tolength).
* **Note:** This method is based on [`ToLength`](http://ecma-international.org/ecma-262/6.0/#sec-tolength).
*
* @static
* @memberOf _
@@ -9865,6 +9875,30 @@
return clamp(toInteger(value), 0, MAX_ARRAY_LENGTH);
}
/**
* Converts `value` to a number.
*
* @static
* @memberOf _
* @category Lang
* @param {*} value The value to process.
* @returns {number} Returns the number.
*/
function toNumber(value) {
if (isObject(value)) {
var other = isFunction(value.valueOf) ? value.valueOf() : value;
value = isObject(other) ? (other + '') : other;
}
if (typeof value == 'number' || !isString(value)) {
return +value;
}
value = trim(value);
var isBinary = reIsBinary.test(value);
return (isBinary || reIsOctal.test(value))
? nativeParseInt(value.slice(2), isBinary ? 2 : 8)
: (reIsBadHex.test(value) ? NAN : +value);
}
/**
* Converts `value` to a plain object flattening inherited enumerable
* properties of `value` to own properties of the plain object.
@@ -13522,6 +13556,7 @@
lodash.toInteger = toInteger;
lodash.toLength = toLength;
lodash.toLower = toLower;
lodash.toNumber = toNumber;
lodash.toSafeInteger = toSafeInteger;
lodash.toString = toString;
lodash.toUpper = toUpper;