1 var toString = require('./toString');
3 /** Used to match leading and trailing whitespace. */
4 var reTrim = /^\s+|\s+$/g;
6 /** Used to detect hexadecimal string values. */
7 var reHasHexPrefix = /^0x/i;
9 /* Built-in method references for those with the same name as other `lodash` methods. */
10 var nativeParseInt = global.parseInt;
13 * Converts `string` to an integer of the specified radix. If `radix` is
14 * `undefined` or `0`, a `radix` of `10` is used unless `value` is a hexadecimal,
15 * in which case a `radix` of `16` is used.
17 * **Note:** This method aligns with the [ES5 implementation](https://es5.github.io/#x15.1.2.2)
23 * @param {string} string The string to convert.
24 * @param {number} [radix] The radix to interpret `value` by.
25 * @param- {Object} [guard] Enables use as an iteratee for functions like `_.map`.
26 * @returns {number} Returns the converted integer.
32 * _.map(['6', '08', '10'], _.parseInt);
35 function parseInt(string, radix, guard) {
36 // Chrome fails to trim leading <BOM> whitespace characters.
37 // See https://code.google.com/p/v8/issues/detail?id=3109 for more details.
38 if (guard || radix == null) {
43 string = toString(string).replace(reTrim, '');
44 return nativeParseInt(string, radix || (reHasHexPrefix.test(string) ? 16 : 10));
47 module.exports = parseInt;