-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
es.number.parse-int.js
43 lines (40 loc) · 1.97 KB
/
es.number.parse-int.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
37
38
39
40
41
42
43
/* eslint-disable prefer-numeric-literals -- required for testing */
import { WHITESPACES } from '../helpers/constants.js';
import parseInt from 'core-js-pure/es/number/parse-int';
QUnit.test('Number.parseInt', assert => {
assert.isFunction(parseInt);
assert.arity(parseInt, 2);
for (let radix = 2; radix <= 36; ++radix) {
assert.same(parseInt('10', radix), radix, `radix ${ radix }`);
}
const strings = ['01', '08', '10', '42'];
for (const string of strings) {
assert.same(parseInt(string), parseInt(string, 10), `default radix is 10: ${ string }`);
}
assert.same(parseInt('0x16'), parseInt('0x16', 16), 'default radix is 16: 0x16');
assert.same(parseInt(' 0x16'), parseInt('0x16', 16), 'ignores leading whitespace #1');
assert.same(parseInt(' 42'), parseInt('42', 10), 'ignores leading whitespace #2');
assert.same(parseInt(' 08'), parseInt('08', 10), 'ignores leading whitespace #3');
assert.same(parseInt(`${ WHITESPACES }08`), parseInt('08', 10), 'ignores leading whitespace #4');
assert.same(parseInt(`${ WHITESPACES }0x16`), parseInt('0x16', 16), 'ignores leading whitespace #5');
const fakeZero = {
valueOf() {
return 0;
},
};
assert.same(parseInt('08', fakeZero), parseInt('08', 10), 'valueOf #1');
assert.same(parseInt('0x16', fakeZero), parseInt('0x16', 16), 'valueOf #2');
assert.same(parseInt('-0xF'), -15, 'signed hex #1');
assert.same(parseInt('-0xF', 16), -15, 'signed hex #2');
assert.same(parseInt('+0xF'), 15, 'signed hex #3');
assert.same(parseInt('+0xF', 16), 15, 'signed hex #4');
assert.same(parseInt('10', -4294967294), 2, 'radix uses ToUint32');
assert.same(parseInt(null), NaN);
assert.same(parseInt(undefined), NaN);
/* eslint-disable es/no-symbol -- safe */
if (typeof Symbol == 'function') {
const symbol = Symbol('Number.parseInt test');
assert.throws(() => parseInt(symbol), 'throws on symbol argument');
assert.throws(() => parseInt(Object(symbol)), 'throws on boxed symbol argument');
}
});