mirror of
https://github.com/musix-org/musix-oss
synced 2024-12-24 01:43:18 +00:00
31 lines
1.1 KiB
JavaScript
31 lines
1.1 KiB
JavaScript
|
'use strict';
|
||
|
var $ = require('../internals/export');
|
||
|
var toInteger = require('../internals/to-integer');
|
||
|
var parseInt = require('../internals/number-parse-int');
|
||
|
|
||
|
var INVALID_NUMBER_REPRESENTATION = 'Invalid number representation';
|
||
|
var INVALID_RADIX = 'Invalid radix';
|
||
|
var valid = /^[\da-z]+$/;
|
||
|
|
||
|
// `Number.fromString` method
|
||
|
// https://github.com/tc39/proposal-number-fromstring
|
||
|
$({ target: 'Number', stat: true }, {
|
||
|
fromString: function fromString(string, radix) {
|
||
|
var sign = 1;
|
||
|
var R, mathNum;
|
||
|
if (typeof string != 'string') throw TypeError(INVALID_NUMBER_REPRESENTATION);
|
||
|
if (!string.length) throw SyntaxError(INVALID_NUMBER_REPRESENTATION);
|
||
|
if (string.charAt(0) == '-') {
|
||
|
sign = -1;
|
||
|
string = string.slice(1);
|
||
|
if (!string.length) throw SyntaxError(INVALID_NUMBER_REPRESENTATION);
|
||
|
}
|
||
|
R = radix === undefined ? 10 : toInteger(radix);
|
||
|
if (R < 2 || R > 36) throw RangeError(INVALID_RADIX);
|
||
|
if (!valid.test(string) || (mathNum = parseInt(string, R)).toString(R) !== string) {
|
||
|
throw SyntaxError(INVALID_NUMBER_REPRESENTATION);
|
||
|
}
|
||
|
return sign * mathNum;
|
||
|
}
|
||
|
});
|