forked from lodash/lodash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseInt.js
36 lines (32 loc) · 1.02 KB
/
parseInt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import root from './.internal/root.js'
/** Used to match leading and trailing whitespace. */
const reTrimStart = /^\s+/
/* Built-in method references for those with the same name as other `lodash` methods. */
const nativeParseInt = root.parseInt
/**
* Converts `string` to an integer of the specified radix. If `radix` is
* `undefined` or `0`, a `radix` of `10` is used unless `string` is a
* hexadecimal, in which case a `radix` of `16` is used.
*
* **Note:** This method aligns with the
* [ES5 implementation](https://es5.github.io/#x15.1.2.2) of `parseInt`.
*
* @since 1.1.0
* @category String
* @param {string} string The string to convert.
* @param {number} [radix=10] The radix to interpret `string` by.
* @returns {number} Returns the converted integer.
* @example
*
* parseInt('08')
* // => 8
*/
function parseInt(string, radix) {
if (radix == null) {
radix = 0
} else if (radix) {
radix = +radix
}
return nativeParseInt(`${string}`.replace(reTrimStart, ''), radix || 0)
}
export default parseInt