UNPKG

4.61 kBJavaScriptView Raw
1'use strict';
2var $ = require('../internals/export');
3var IS_PURE = require('../internals/is-pure');
4var DESCRIPTORS = require('../internals/descriptors');
5var global = require('../internals/global');
6var path = require('../internals/path');
7var uncurryThis = require('../internals/function-uncurry-this');
8var isForced = require('../internals/is-forced');
9var hasOwn = require('../internals/has-own-property');
10var inheritIfRequired = require('../internals/inherit-if-required');
11var isPrototypeOf = require('../internals/object-is-prototype-of');
12var isSymbol = require('../internals/is-symbol');
13var toPrimitive = require('../internals/to-primitive');
14var fails = require('../internals/fails');
15var getOwnPropertyNames = require('../internals/object-get-own-property-names').f;
16var getOwnPropertyDescriptor = require('../internals/object-get-own-property-descriptor').f;
17var defineProperty = require('../internals/object-define-property').f;
18var thisNumberValue = require('../internals/this-number-value');
19var trim = require('../internals/string-trim').trim;
20
21var NUMBER = 'Number';
22var NativeNumber = global[NUMBER];
23var PureNumberNamespace = path[NUMBER];
24var NumberPrototype = NativeNumber.prototype;
25var TypeError = global.TypeError;
26var stringSlice = uncurryThis(''.slice);
27var charCodeAt = uncurryThis(''.charCodeAt);
28
29// `ToNumeric` abstract operation
30// https://tc39.es/ecma262/#sec-tonumeric
31var toNumeric = function (value) {
32 var primValue = toPrimitive(value, 'number');
33 return typeof primValue == 'bigint' ? primValue : toNumber(primValue);
34};
35
36// `ToNumber` abstract operation
37// https://tc39.es/ecma262/#sec-tonumber
38var toNumber = function (argument) {
39 var it = toPrimitive(argument, 'number');
40 var first, third, radix, maxCode, digits, length, index, code;
41 if (isSymbol(it)) throw new TypeError('Cannot convert a Symbol value to a number');
42 if (typeof it == 'string' && it.length > 2) {
43 it = trim(it);
44 first = charCodeAt(it, 0);
45 if (first === 43 || first === 45) {
46 third = charCodeAt(it, 2);
47 if (third === 88 || third === 120) return NaN; // Number('+0x1') should be NaN, old V8 fix
48 } else if (first === 48) {
49 switch (charCodeAt(it, 1)) {
50 // fast equal of /^0b[01]+$/i
51 case 66:
52 case 98:
53 radix = 2;
54 maxCode = 49;
55 break;
56 // fast equal of /^0o[0-7]+$/i
57 case 79:
58 case 111:
59 radix = 8;
60 maxCode = 55;
61 break;
62 default:
63 return +it;
64 }
65 digits = stringSlice(it, 2);
66 length = digits.length;
67 for (index = 0; index < length; index++) {
68 code = charCodeAt(digits, index);
69 // parseInt parses a string to a first unavailable symbol
70 // but ToNumber should return NaN if a string contains unavailable symbols
71 if (code < 48 || code > maxCode) return NaN;
72 } return parseInt(digits, radix);
73 }
74 } return +it;
75};
76
77var FORCED = isForced(NUMBER, !NativeNumber(' 0o1') || !NativeNumber('0b1') || NativeNumber('+0x1'));
78
79var calledWithNew = function (dummy) {
80 // includes check on 1..constructor(foo) case
81 return isPrototypeOf(NumberPrototype, dummy) && fails(function () { thisNumberValue(dummy); });
82};
83
84// `Number` constructor
85// https://tc39.es/ecma262/#sec-number-constructor
86var NumberWrapper = function Number(value) {
87 var n = arguments.length < 1 ? 0 : NativeNumber(toNumeric(value));
88 return calledWithNew(this) ? inheritIfRequired(Object(n), this, NumberWrapper) : n;
89};
90
91NumberWrapper.prototype = NumberPrototype;
92if (FORCED && !IS_PURE) NumberPrototype.constructor = NumberWrapper;
93
94$({ global: true, constructor: true, wrap: true, forced: FORCED }, {
95 Number: NumberWrapper
96});
97
98// Use `internal/copy-constructor-properties` helper in `core-js@4`
99var copyConstructorProperties = function (target, source) {
100 for (var keys = DESCRIPTORS ? getOwnPropertyNames(source) : (
101 // ES3:
102 'MAX_VALUE,MIN_VALUE,NaN,NEGATIVE_INFINITY,POSITIVE_INFINITY,' +
103 // ES2015 (in case, if modules with ES2015 Number statics required before):
104 'EPSILON,MAX_SAFE_INTEGER,MIN_SAFE_INTEGER,isFinite,isInteger,isNaN,isSafeInteger,parseFloat,parseInt,' +
105 // ESNext
106 'fromString,range'
107 ).split(','), j = 0, key; keys.length > j; j++) {
108 if (hasOwn(source, key = keys[j]) && !hasOwn(target, key)) {
109 defineProperty(target, key, getOwnPropertyDescriptor(source, key));
110 }
111 }
112};
113
114if (IS_PURE && PureNumberNamespace) copyConstructorProperties(path[NUMBER], PureNumberNamespace);
115if (FORCED || IS_PURE) copyConstructorProperties(path[NUMBER], NativeNumber);