Skip to content

Commit 613d4ca

Browse files
committed
1 parent 227b9fd commit 613d4ca

File tree

13 files changed

+122
-1
lines changed

13 files changed

+122
-1
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,16 @@ Math.signbit(-1); // => false
17231723
Math.signbit(0); // => true
17241724
Math.signbit(-0); // => false
17251725
```
1726+
* `Number.fromString` [proposal](https://github.com/mathiasbynens/proposal-number-fromstring) - module [`esnext.number.from-string`](https://github.com/zloirock/core-js/blob/v3/packages/core-js/modules/esnext.number.from-string.js)
1727+
```js
1728+
class Number {
1729+
fromString(string: string, radix: number): number;
1730+
}
1731+
```
1732+
[*CommonJS entry points:*](#commonjs)
1733+
```js
1734+
core-js(-pure)/features/number/from-string
1735+
```
17261736
17271737
#### Stage 0 proposals
17281738
[*CommonJS entry points:*](#commonjs)

packages/core-js-builder/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ module.exports = {
199199
'esnext.math.scale',
200200
'esnext.math.umulh',
201201
'esnext.math.signbit',
202+
'esnext.number.from-string',
202203
'esnext.promise.finally',
203204
'esnext.promise.try',
204205
'esnext.reflect.define-metadata',
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require('../../modules/esnext.number.from-string');
2+
3+
module.exports = require('../../internals/path').Number.fromString;

packages/core-js/features/number/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ require('../../modules/es.number.parse-float');
1010
require('../../modules/es.number.parse-int');
1111
require('../../modules/es.number.to-fixed');
1212
require('../../modules/es.number.to-precision');
13+
require('../../modules/esnext.number.from-string');
1314

1415
module.exports = require('../../internals/path').Number;

packages/core-js/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ require('./modules/esnext.math.radians');
196196
require('./modules/esnext.math.scale');
197197
require('./modules/esnext.math.umulh');
198198
require('./modules/esnext.math.signbit');
199+
require('./modules/esnext.number.from-string');
199200
require('./modules/esnext.promise.finally');
200201
require('./modules/esnext.promise.try');
201202
require('./modules/esnext.reflect.define-metadata');
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
var toInteger = require('../internals/to-integer');
3+
var parseInt = require('../internals/parse-int');
4+
var INVALID_NUMBER_REPRESENTATION = 'Invalid number representation!';
5+
var INVALID_RADIX = 'Invalid radix!';
6+
var valid = /^[0-9a-z]+$/;
7+
8+
// https://github.com/mathiasbynens/proposal-number-fromstring
9+
require('../internals/export')({ target: 'Number', stat: true }, {
10+
fromString: function fromString(string, radix) {
11+
var sign = 1;
12+
var R, mathNum;
13+
if (typeof string != 'string') throw new TypeError(INVALID_NUMBER_REPRESENTATION);
14+
if (!string.length) throw new SyntaxError(INVALID_NUMBER_REPRESENTATION);
15+
if (string.charAt(0) == '-') {
16+
sign = -1;
17+
string = string.slice(1);
18+
if (!string.length) throw new SyntaxError(INVALID_NUMBER_REPRESENTATION);
19+
}
20+
R = radix === undefined ? 10 : toInteger(radix);
21+
if (R < 2 || R > 36) throw new RangeError(INVALID_RADIX);
22+
if (!valid.test(string) || (mathNum = parseInt(string, R)).toString(R) !== string) {
23+
throw new SyntaxError(INVALID_NUMBER_REPRESENTATION);
24+
}
25+
return sign * mathNum;
26+
}
27+
});

packages/core-js/stage/1.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ require('../modules/esnext.math.rad-per-deg');
3434
require('../modules/esnext.math.radians');
3535
require('../modules/esnext.math.scale');
3636
require('../modules/esnext.math.signbit');
37+
require('../modules/esnext.number.from-string');
3738
require('../modules/esnext.promise.try');
3839
require('../modules/esnext.string.code-points');
3940
require('../modules/esnext.string.replace-all');

tests/commonjs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ for (const _PATH of ['../packages/core-js-pure', '../packages/core-js']) {
146146
ok(load('features/number/parse-int')('2.1') === 2);
147147
ok(load('features/number/to-fixed')(1, 1) === '1.0');
148148
ok(load('features/number/to-precision')(1) === '1');
149+
ok(load('features/number/from-string')('12', 3) === 5);
149150
ok(load('features/parse-float')('1.5') === 1.5);
150151
ok(load('features/parse-int')('2.1') === 2);
151152
ok(load('features/number/virtual/to-fixed').call(1, 1) === '1.0');
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import fromString from 'core-js-pure/features/number/from-string';
2+
3+
QUnit.test('Number.fromString', assert => {
4+
assert.isFunction(fromString);
5+
if ('name' in fromString) assert.name(fromString, 'fromString');
6+
assert.arity(fromString, 2);
7+
assert.throws(() => fromString(undefined), TypeError, 'The first argument should be a string #1');
8+
assert.throws(() => fromString(Object('10')), TypeError, 'The first argument should be a string #1');
9+
assert.throws(() => fromString(''), SyntaxError, 'Empty string');
10+
assert.same(fromString('-10', 2), -2, 'Works with negative numbers');
11+
assert.throws(() => fromString('-'), SyntaxError, '-');
12+
assert.same(fromString('10'), 10, 'Default radix is 10 #1');
13+
assert.same(fromString('10', undefined), 10, 'Default radix is 10 #2');
14+
for (let radix = 2; radix <= 36; ++radix) {
15+
assert.same(fromString('10', radix), radix, `Radix ${ radix }`);
16+
}
17+
assert.throws(() => fromString('10', -4294967294), RangeError, 'Radix uses ToInteger #1');
18+
assert.same(fromString('10', 2.5), 2, 'Radix uses ToInteger #2');
19+
assert.same(fromString('42'), 42);
20+
assert.same(fromString('42', 10), 42);
21+
assert.throws(() => fromString('0xc0ffee'), SyntaxError);
22+
assert.throws(() => fromString('0o755'), SyntaxError);
23+
assert.throws(() => fromString('0b00101010'), SyntaxError);
24+
assert.throws(() => fromString('C0FFEE', 16), SyntaxError);
25+
assert.same(fromString('c0ffee', 16), 12648430);
26+
assert.same(fromString('755', 8), 493);
27+
assert.throws(() => fromString(''), SyntaxError);
28+
assert.throws(() => fromString(' '), SyntaxError);
29+
assert.throws(() => fromString(' 1'), SyntaxError);
30+
assert.throws(() => fromString(' \n '), SyntaxError);
31+
assert.throws(() => fromString('x'), SyntaxError);
32+
assert.throws(() => fromString('1234', 0), RangeError);
33+
assert.throws(() => fromString('1234', 1), RangeError);
34+
assert.throws(() => fromString('1234', 37), RangeError);
35+
assert.throws(() => fromString('010'), SyntaxError);
36+
assert.throws(() => fromString('1_000_000_000'), SyntaxError);
37+
});

tests/pure/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ import './esnext.math.radians';
139139
import './esnext.math.scale';
140140
import './esnext.math.signbit';
141141
import './esnext.math.umulh';
142+
import './esnext.number.from-string';
142143
import './esnext.observable';
143144
import './esnext.promise.finally';
144145
import './esnext.promise.try';

tests/tests/es.number.parse-int.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { GLOBAL, WHITESPACES } from '../helpers/constants';
33
QUnit.test('Number.parseInt', assert => {
44
const { parseInt } = Number;
55
assert.isFunction(parseInt);
6-
assert.isFunction(parseInt);
76
assert.name(parseInt, 'parseInt');
87
assert.arity(parseInt, 2);
98
assert.looksNative(parseInt);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
QUnit.test('Number.fromString', assert => {
2+
const { fromString } = Number;
3+
assert.isFunction(fromString);
4+
assert.name(fromString, 'fromString');
5+
assert.arity(fromString, 2);
6+
assert.looksNative(fromString);
7+
assert.nonEnumerable(Number, 'fromString');
8+
assert.throws(() => fromString(undefined), TypeError, 'The first argument should be a string #1');
9+
assert.throws(() => fromString(Object('10')), TypeError, 'The first argument should be a string #1');
10+
assert.throws(() => fromString(''), SyntaxError, 'Empty string');
11+
assert.same(fromString('-10', 2), -2, 'Works with negative numbers');
12+
assert.throws(() => fromString('-'), SyntaxError, '-');
13+
assert.same(fromString('10'), 10, 'Default radix is 10 #1');
14+
assert.same(fromString('10', undefined), 10, 'Default radix is 10 #2');
15+
for (let radix = 2; radix <= 36; ++radix) {
16+
assert.same(fromString('10', radix), radix, `Radix ${ radix }`);
17+
}
18+
assert.throws(() => fromString('10', -4294967294), RangeError, 'Radix uses ToInteger #1');
19+
assert.same(fromString('10', 2.5), 2, 'Radix uses ToInteger #2');
20+
assert.same(fromString('42'), 42);
21+
assert.same(fromString('42', 10), 42);
22+
assert.throws(() => fromString('0xc0ffee'), SyntaxError);
23+
assert.throws(() => fromString('0o755'), SyntaxError);
24+
assert.throws(() => fromString('0b00101010'), SyntaxError);
25+
assert.throws(() => fromString('C0FFEE', 16), SyntaxError);
26+
assert.same(fromString('c0ffee', 16), 12648430);
27+
assert.same(fromString('755', 8), 493);
28+
assert.throws(() => fromString(''), SyntaxError);
29+
assert.throws(() => fromString(' '), SyntaxError);
30+
assert.throws(() => fromString(' 1'), SyntaxError);
31+
assert.throws(() => fromString(' \n '), SyntaxError);
32+
assert.throws(() => fromString('x'), SyntaxError);
33+
assert.throws(() => fromString('1234', 0), RangeError);
34+
assert.throws(() => fromString('1234', 1), RangeError);
35+
assert.throws(() => fromString('1234', 37), RangeError);
36+
assert.throws(() => fromString('010'), SyntaxError);
37+
assert.throws(() => fromString('1_000_000_000'), SyntaxError);
38+
});

tests/tests/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ import './esnext.math.radians';
192192
import './esnext.math.scale';
193193
import './esnext.math.signbit';
194194
import './esnext.math.umulh';
195+
import './esnext.number.from-string';
195196
import './esnext.observable';
196197
import './esnext.promise.finally';
197198
import './esnext.promise.try';

0 commit comments

Comments
 (0)