1 var isIterateeCall = require('../internal/isIterateeCall'),
2 trim = require('./trim');
4 /** Used to detect hexadecimal string values. */
5 var reHasHexPrefix = /^0[xX]/;
7 /** Used to detect and test for whitespace. */
9 // Basic whitespace characters.
10 ' \t\x0b\f\xa0\ufeff' +
15 // Unicode category "Zs" space separators.
16 '\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000'
19 /* Native method references for those with the same name as other `lodash` methods. */
20 var nativeParseInt = global.parseInt;
23 * Converts `string` to an integer of the specified radix. If `radix` is
24 * `undefined` or `0`, a `radix` of `10` is used unless `value` is a hexadecimal,
25 * in which case a `radix` of `16` is used.
27 * **Note:** This method aligns with the [ES5 implementation](https://es5.github.io/#E)
33 * @param {string} string The string to convert.
34 * @param {number} [radix] The radix to interpret `value` by.
35 * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
36 * @returns {number} Returns the converted integer.
42 * _.map(['6', '08', '10'], _.parseInt);
45 function parseInt(string, radix, guard) {
46 if (guard && isIterateeCall(string, radix, guard)) {
49 return nativeParseInt(string, radix);
51 // Fallback for environments with pre-ES5 implementations.
52 if (nativeParseInt(whitespace + '08') != 8) {
53 parseInt = function(string, radix, guard) {
54 // Firefox < 21 and Opera < 15 follow ES3 for `parseInt`.
55 // Chrome fails to trim leading <BOM> whitespace characters.
56 // See https://code.google.com/p/v8/issues/detail?id=3109 for more details.
57 if (guard ? isIterateeCall(string, radix, guard) : radix == null) {
62 string = trim(string);
63 return nativeParseInt(string, radix || (reHasHexPrefix.test(string) ? 16 : 10));
67 module.exports = parseInt;