Last change
on this file since b738035 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Line | |
---|
1 | 'use strict';
|
---|
2 | var $ = require('../internals/export');
|
---|
3 | var toInteger = require('../internals/to-integer');
|
---|
4 | var parseInt = require('../internals/number-parse-int');
|
---|
5 |
|
---|
6 | var INVALID_NUMBER_REPRESENTATION = 'Invalid number representation';
|
---|
7 | var INVALID_RADIX = 'Invalid radix';
|
---|
8 | var valid = /^[\da-z]+$/;
|
---|
9 |
|
---|
10 | // `Number.fromString` method
|
---|
11 | // https://github.com/tc39/proposal-number-fromstring
|
---|
12 | $({ target: 'Number', stat: true }, {
|
---|
13 | fromString: function fromString(string, radix) {
|
---|
14 | var sign = 1;
|
---|
15 | var R, mathNum;
|
---|
16 | if (typeof string != 'string') throw TypeError(INVALID_NUMBER_REPRESENTATION);
|
---|
17 | if (!string.length) throw SyntaxError(INVALID_NUMBER_REPRESENTATION);
|
---|
18 | if (string.charAt(0) == '-') {
|
---|
19 | sign = -1;
|
---|
20 | string = string.slice(1);
|
---|
21 | if (!string.length) throw SyntaxError(INVALID_NUMBER_REPRESENTATION);
|
---|
22 | }
|
---|
23 | R = radix === undefined ? 10 : toInteger(radix);
|
---|
24 | if (R < 2 || R > 36) throw RangeError(INVALID_RADIX);
|
---|
25 | if (!valid.test(string) || (mathNum = parseInt(string, R)).toString(R) !== string) {
|
---|
26 | throw SyntaxError(INVALID_NUMBER_REPRESENTATION);
|
---|
27 | }
|
---|
28 | return sign * mathNum;
|
---|
29 | }
|
---|
30 | });
|
---|
Note:
See
TracBrowser
for help on using the repository browser.