2 var DESCRIPTORS = require('../internals/descriptors');
3 var global = require('../internals/global');
4 var uncurryThis = require('../internals/function-uncurry-this');
5 var isForced = require('../internals/is-forced');
6 var redefine = require('../internals/redefine');
7 var hasOwn = require('../internals/has-own-property');
8 var inheritIfRequired = require('../internals/inherit-if-required');
9 var isPrototypeOf = require('../internals/object-is-prototype-of');
10 var isSymbol = require('../internals/is-symbol');
11 var toPrimitive = require('../internals/to-primitive');
12 var fails = require('../internals/fails');
13 var getOwnPropertyNames = require('../internals/object-get-own-property-names').f;
14 var getOwnPropertyDescriptor = require('../internals/object-get-own-property-descriptor').f;
15 var defineProperty = require('../internals/object-define-property').f;
16 var thisNumberValue = require('../internals/this-number-value');
17 var trim = require('../internals/string-trim').trim;
19 var NUMBER = 'Number';
20 var NativeNumber = global[NUMBER];
21 var NumberPrototype = NativeNumber.prototype;
22 var TypeError = global.TypeError;
23 var arraySlice = uncurryThis(''.slice);
24 var charCodeAt = uncurryThis(''.charCodeAt);
26 // `ToNumeric` abstract operation
27 // https://tc39.es/ecma262/#sec-tonumeric
28 var toNumeric = function (value) {
29 var primValue = toPrimitive(value, 'number');
30 return typeof primValue == 'bigint' ? primValue : toNumber(primValue);
33 // `ToNumber` abstract operation
34 // https://tc39.es/ecma262/#sec-tonumber
35 var toNumber = function (argument) {
36 var it = toPrimitive(argument, 'number');
37 var first, third, radix, maxCode, digits, length, index, code;
38 if (isSymbol(it)) throw TypeError('Cannot convert a Symbol value to a number');
39 if (typeof it == 'string' && it.length > 2) {
41 first = charCodeAt(it, 0);
42 if (first === 43 || first === 45) {
43 third = charCodeAt(it, 2);
44 if (third === 88 || third === 120) return NaN; // Number('+0x1') should be NaN, old V8 fix
45 } else if (first === 48) {
46 switch (charCodeAt(it, 1)) {
47 case 66: case 98: radix = 2; maxCode = 49; break; // fast equal of /^0b[01]+$/i
48 case 79: case 111: radix = 8; maxCode = 55; break; // fast equal of /^0o[0-7]+$/i
51 digits = arraySlice(it, 2);
52 length = digits.length;
53 for (index = 0; index < length; index++) {
54 code = charCodeAt(digits, index);
55 // parseInt parses a string to a first unavailable symbol
56 // but ToNumber should return NaN if a string contains unavailable symbols
57 if (code < 48 || code > maxCode) return NaN;
58 } return parseInt(digits, radix);
63 // `Number` constructor
64 // https://tc39.es/ecma262/#sec-number-constructor
65 if (isForced(NUMBER, !NativeNumber(' 0o1') || !NativeNumber('0b1') || NativeNumber('+0x1'))) {
66 var NumberWrapper = function Number(value) {
67 var n = arguments.length < 1 ? 0 : NativeNumber(toNumeric(value));
69 // check on 1..constructor(foo) case
70 return isPrototypeOf(NumberPrototype, dummy) && fails(function () { thisNumberValue(dummy); })
71 ? inheritIfRequired(Object(n), dummy, NumberWrapper) : n;
73 for (var keys = DESCRIPTORS ? getOwnPropertyNames(NativeNumber) : (
75 'MAX_VALUE,MIN_VALUE,NaN,NEGATIVE_INFINITY,POSITIVE_INFINITY,' +
76 // ES2015 (in case, if modules with ES2015 Number statics required before):
77 'EPSILON,MAX_SAFE_INTEGER,MIN_SAFE_INTEGER,isFinite,isInteger,isNaN,isSafeInteger,parseFloat,parseInt,' +
80 ).split(','), j = 0, key; keys.length > j; j++) {
81 if (hasOwn(NativeNumber, key = keys[j]) && !hasOwn(NumberWrapper, key)) {
82 defineProperty(NumberWrapper, key, getOwnPropertyDescriptor(NativeNumber, key));
85 NumberWrapper.prototype = NumberPrototype;
86 NumberPrototype.constructor = NumberWrapper;
87 redefine(global, NUMBER, NumberWrapper);