c4cf71a9609d919885eed61b2576c326b83a4e14
[platform/framework/web/crosswalk-tizen.git] /
1 var isIterateeCall = require('../internal/isIterateeCall'),
2     trim = require('./trim');
3
4 /** Used to detect hexadecimal string values. */
5 var reHasHexPrefix = /^0[xX]/;
6
7 /** Used to detect and test for whitespace. */
8 var whitespace = (
9   // Basic whitespace characters.
10   ' \t\x0b\f\xa0\ufeff' +
11
12   // Line terminators.
13   '\n\r\u2028\u2029' +
14
15   // Unicode category "Zs" space separators.
16   '\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000'
17 );
18
19 /* Native method references for those with the same name as other `lodash` methods. */
20 var nativeParseInt = global.parseInt;
21
22 /**
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.
26  *
27  * **Note:** This method aligns with the [ES5 implementation](https://es5.github.io/#E)
28  * of `parseInt`.
29  *
30  * @static
31  * @memberOf _
32  * @category String
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.
37  * @example
38  *
39  * _.parseInt('08');
40  * // => 8
41  *
42  * _.map(['6', '08', '10'], _.parseInt);
43  * // => [6, 8, 10]
44  */
45 function parseInt(string, radix, guard) {
46   if (guard && isIterateeCall(string, radix, guard)) {
47     radix = 0;
48   }
49   return nativeParseInt(string, radix);
50 }
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) {
58       radix = 0;
59     } else if (radix) {
60       radix = +radix;
61     }
62     string = trim(string);
63     return nativeParseInt(string, radix || (reHasHexPrefix.test(string) ? 16 : 10));
64   };
65 }
66
67 module.exports = parseInt;