2016-01-28 01:16:24 -08:00
|
|
|
define(['./_root', './toString'], function(root, toString) {
|
2015-12-16 17:53:20 -08:00
|
|
|
|
2025-12-10 17:03:07 -05:00
|
|
|
/** Used to match leading whitespace. */
|
2016-09-25 13:37:46 -07:00
|
|
|
var reTrimStart = /^\s+/;
|
|
|
|
|
|
2015-12-16 17:53:20 -08:00
|
|
|
/* Built-in method references for those with the same name as other `lodash` methods. */
|
2015-01-08 00:37:01 -08:00
|
|
|
var nativeParseInt = root.parseInt;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Converts `string` to an integer of the specified radix. If `radix` is
|
2016-03-26 00:00:01 -07:00
|
|
|
* `undefined` or `0`, a `radix` of `10` is used unless `value` is a
|
|
|
|
|
* hexadecimal, in which case a `radix` of `16` is used.
|
2015-01-08 00:37:01 -08:00
|
|
|
*
|
2016-03-26 00:00:01 -07:00
|
|
|
* **Note:** This method aligns with the
|
|
|
|
|
* [ES5 implementation](https://es5.github.io/#x15.1.2.2) of `parseInt`.
|
2015-01-08 00:37:01 -08:00
|
|
|
*
|
|
|
|
|
* @static
|
|
|
|
|
* @memberOf _
|
2016-03-26 00:00:01 -07:00
|
|
|
* @since 1.1.0
|
2015-01-08 00:37:01 -08:00
|
|
|
* @category String
|
|
|
|
|
* @param {string} string The string to convert.
|
2016-02-15 20:20:54 -08:00
|
|
|
* @param {number} [radix=10] The radix to interpret `value` by.
|
2016-03-26 00:00:01 -07:00
|
|
|
* @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
|
2015-01-08 00:37:01 -08:00
|
|
|
* @returns {number} Returns the converted integer.
|
|
|
|
|
* @example
|
|
|
|
|
*
|
|
|
|
|
* _.parseInt('08');
|
|
|
|
|
* // => 8
|
|
|
|
|
*
|
|
|
|
|
* _.map(['6', '08', '10'], _.parseInt);
|
|
|
|
|
* // => [6, 8, 10]
|
|
|
|
|
*/
|
|
|
|
|
function parseInt(string, radix, guard) {
|
2015-12-16 17:53:20 -08:00
|
|
|
if (guard || radix == null) {
|
2015-01-08 00:37:01 -08:00
|
|
|
radix = 0;
|
2015-12-16 17:52:15 -08:00
|
|
|
} else if (radix) {
|
|
|
|
|
radix = +radix;
|
2015-01-08 00:37:01 -08:00
|
|
|
}
|
2016-09-25 13:37:46 -07:00
|
|
|
return nativeParseInt(toString(string).replace(reTrimStart, ''), radix || 0);
|
2015-01-08 00:37:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parseInt;
|
|
|
|
|
});
|