1 | 'use strict';
|
2 | var $ = require('../internals/export');
|
3 | var IS_PURE = require('../internals/is-pure');
|
4 | var DESCRIPTORS = require('../internals/descriptors');
|
5 | var global = require('../internals/global');
|
6 | var path = require('../internals/path');
|
7 | var uncurryThis = require('../internals/function-uncurry-this');
|
8 | var isForced = require('../internals/is-forced');
|
9 | var hasOwn = require('../internals/has-own-property');
|
10 | var inheritIfRequired = require('../internals/inherit-if-required');
|
11 | var isPrototypeOf = require('../internals/object-is-prototype-of');
|
12 | var isSymbol = require('../internals/is-symbol');
|
13 | var toPrimitive = require('../internals/to-primitive');
|
14 | var fails = require('../internals/fails');
|
15 | var getOwnPropertyNames = require('../internals/object-get-own-property-names').f;
|
16 | var getOwnPropertyDescriptor = require('../internals/object-get-own-property-descriptor').f;
|
17 | var defineProperty = require('../internals/object-define-property').f;
|
18 | var thisNumberValue = require('../internals/this-number-value');
|
19 | var trim = require('../internals/string-trim').trim;
|
20 |
|
21 | var NUMBER = 'Number';
|
22 | var NativeNumber = global[NUMBER];
|
23 | var PureNumberNamespace = path[NUMBER];
|
24 | var NumberPrototype = NativeNumber.prototype;
|
25 | var TypeError = global.TypeError;
|
26 | var stringSlice = uncurryThis(''.slice);
|
27 | var charCodeAt = uncurryThis(''.charCodeAt);
|
28 |
|
29 |
|
30 |
|
31 | var toNumeric = function (value) {
|
32 | var primValue = toPrimitive(value, 'number');
|
33 | return typeof primValue == 'bigint' ? primValue : toNumber(primValue);
|
34 | };
|
35 |
|
36 |
|
37 |
|
38 | var 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;
|
48 | } else if (first === 48) {
|
49 | switch (charCodeAt(it, 1)) {
|
50 |
|
51 | case 66:
|
52 | case 98:
|
53 | radix = 2;
|
54 | maxCode = 49;
|
55 | break;
|
56 |
|
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 |
|
70 |
|
71 | if (code < 48 || code > maxCode) return NaN;
|
72 | } return parseInt(digits, radix);
|
73 | }
|
74 | } return +it;
|
75 | };
|
76 |
|
77 | var FORCED = isForced(NUMBER, !NativeNumber(' 0o1') || !NativeNumber('0b1') || NativeNumber('+0x1'));
|
78 |
|
79 | var calledWithNew = function (dummy) {
|
80 |
|
81 | return isPrototypeOf(NumberPrototype, dummy) && fails(function () { thisNumberValue(dummy); });
|
82 | };
|
83 |
|
84 |
|
85 |
|
86 | var 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 |
|
91 | NumberWrapper.prototype = NumberPrototype;
|
92 | if (FORCED && !IS_PURE) NumberPrototype.constructor = NumberWrapper;
|
93 |
|
94 | $({ global: true, constructor: true, wrap: true, forced: FORCED }, {
|
95 | Number: NumberWrapper
|
96 | });
|
97 |
|
98 |
|
99 | var copyConstructorProperties = function (target, source) {
|
100 | for (var keys = DESCRIPTORS ? getOwnPropertyNames(source) : (
|
101 |
|
102 | 'MAX_VALUE,MIN_VALUE,NaN,NEGATIVE_INFINITY,POSITIVE_INFINITY,' +
|
103 |
|
104 | 'EPSILON,MAX_SAFE_INTEGER,MIN_SAFE_INTEGER,isFinite,isInteger,isNaN,isSafeInteger,parseFloat,parseInt,' +
|
105 |
|
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 |
|
114 | if (IS_PURE && PureNumberNamespace) copyConstructorProperties(path[NUMBER], PureNumberNamespace);
|
115 | if (FORCED || IS_PURE) copyConstructorProperties(path[NUMBER], NativeNumber);
|