1
|
var root = require('./_root'),
|
2
|
toString = require('./toString');
|
3
|
|
4
|
/** Used to match leading and trailing whitespace. */
|
5
|
var reTrimStart = /^\s+/;
|
6
|
|
7
|
/* Built-in method references for those with the same name as other `lodash` methods. */
|
8
|
var nativeParseInt = root.parseInt;
|
9
|
|
10
|
/**
|
11
|
* Converts `string` to an integer of the specified radix. If `radix` is
|
12
|
* `undefined` or `0`, a `radix` of `10` is used unless `value` is a
|
13
|
* hexadecimal, in which case a `radix` of `16` is used.
|
14
|
*
|
15
|
* **Note:** This method aligns with the
|
16
|
* [ES5 implementation](https://es5.github.io/#x15.1.2.2) of `parseInt`.
|
17
|
*
|
18
|
* @static
|
19
|
* @memberOf _
|
20
|
* @since 1.1.0
|
21
|
* @category String
|
22
|
* @param {string} string The string to convert.
|
23
|
* @param {number} [radix=10] The radix to interpret `value` by.
|
24
|
* @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
|
25
|
* @returns {number} Returns the converted integer.
|
26
|
* @example
|
27
|
*
|
28
|
* _.parseInt('08');
|
29
|
* // => 8
|
30
|
*
|
31
|
* _.map(['6', '08', '10'], _.parseInt);
|
32
|
* // => [6, 8, 10]
|
33
|
*/
|
34
|
function parseInt(string, radix, guard) {
|
35
|
if (guard || radix == null) {
|
36
|
radix = 0;
|
37
|
} else if (radix) {
|
38
|
radix = +radix;
|
39
|
}
|
40
|
return nativeParseInt(toString(string).replace(reTrimStart, ''), radix || 0);
|
41
|
}
|
42
|
|
43
|
module.exports = parseInt;
|