es.number.constructor.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. var NUMBER = 'Number';
  21. var NativeNumber = global[NUMBER];
  22. var PureNumberNamespace = path[NUMBER];
  23. var NumberPrototype = NativeNumber.prototype;
  24. var TypeError = global.TypeError;
  25. var stringSlice = uncurryThis(''.slice);
  26. var charCodeAt = uncurryThis(''.charCodeAt);
  27. // `ToNumeric` abstract operation
  28. // https://tc39.es/ecma262/#sec-tonumeric
  29. var toNumeric = function (value) {
  30. var primValue = toPrimitive(value, 'number');
  31. return typeof primValue == 'bigint' ? primValue : toNumber(primValue);
  32. };
  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 new TypeError('Cannot convert a Symbol value to a number');
  39. if (typeof it == 'string' && it.length > 2) {
  40. it = trim(it);
  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. // fast equal of /^0b[01]+$/i
  48. case 66:
  49. case 98:
  50. radix = 2;
  51. maxCode = 49;
  52. break;
  53. // fast equal of /^0o[0-7]+$/i
  54. case 79:
  55. case 111:
  56. radix = 8;
  57. maxCode = 55;
  58. break;
  59. default:
  60. return +it;
  61. }
  62. digits = stringSlice(it, 2);
  63. length = digits.length;
  64. for (index = 0; index < length; index++) {
  65. code = charCodeAt(digits, index);
  66. // parseInt parses a string to a first unavailable symbol
  67. // but ToNumber should return NaN if a string contains unavailable symbols
  68. if (code < 48 || code > maxCode) return NaN;
  69. } return parseInt(digits, radix);
  70. }
  71. } return +it;
  72. };
  73. var FORCED = isForced(NUMBER, !NativeNumber(' 0o1') || !NativeNumber('0b1') || NativeNumber('+0x1'));
  74. var calledWithNew = function (dummy) {
  75. // includes check on 1..constructor(foo) case
  76. return isPrototypeOf(NumberPrototype, dummy) && fails(function () { thisNumberValue(dummy); });
  77. };
  78. // `Number` constructor
  79. // https://tc39.es/ecma262/#sec-number-constructor
  80. var NumberWrapper = function Number(value) {
  81. var n = arguments.length < 1 ? 0 : NativeNumber(toNumeric(value));
  82. return calledWithNew(this) ? inheritIfRequired(Object(n), this, NumberWrapper) : n;
  83. };
  84. NumberWrapper.prototype = NumberPrototype;
  85. if (FORCED && !IS_PURE) NumberPrototype.constructor = NumberWrapper;
  86. $({ global: true, constructor: true, wrap: true, forced: FORCED }, {
  87. Number: NumberWrapper
  88. });
  89. // Use `internal/copy-constructor-properties` helper in `core-js@4`
  90. var copyConstructorProperties = function (target, source) {
  91. for (var keys = DESCRIPTORS ? getOwnPropertyNames(source) : (
  92. // ES3:
  93. 'MAX_VALUE,MIN_VALUE,NaN,NEGATIVE_INFINITY,POSITIVE_INFINITY,' +
  94. // ES2015 (in case, if modules with ES2015 Number statics required before):
  95. 'EPSILON,MAX_SAFE_INTEGER,MIN_SAFE_INTEGER,isFinite,isInteger,isNaN,isSafeInteger,parseFloat,parseInt,' +
  96. // ESNext
  97. 'fromString,range'
  98. ).split(','), j = 0, key; keys.length > j; j++) {
  99. if (hasOwn(source, key = keys[j]) && !hasOwn(target, key)) {
  100. defineProperty(target, key, getOwnPropertyDescriptor(source, key));
  101. }
  102. }
  103. };
  104. if (IS_PURE && PureNumberNamespace) copyConstructorProperties(path[NUMBER], PureNumberNamespace);
  105. if (FORCED || IS_PURE) copyConstructorProperties(path[NUMBER], NativeNumber);