es.number.constructor.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 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. 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
  49. default: return +it;
  50. }
  51. digits = stringSlice(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);
  59. }
  60. } return +it;
  61. };
  62. var FORCED = isForced(NUMBER, !NativeNumber(' 0o1') || !NativeNumber('0b1') || NativeNumber('+0x1'));
  63. var calledWithNew = function (dummy) {
  64. // includes check on 1..constructor(foo) case
  65. return isPrototypeOf(NumberPrototype, dummy) && fails(function () { thisNumberValue(dummy); });
  66. };
  67. // `Number` constructor
  68. // https://tc39.es/ecma262/#sec-number-constructor
  69. var NumberWrapper = function Number(value) {
  70. var n = arguments.length < 1 ? 0 : NativeNumber(toNumeric(value));
  71. return calledWithNew(this) ? inheritIfRequired(Object(n), this, NumberWrapper) : n;
  72. };
  73. NumberWrapper.prototype = NumberPrototype;
  74. if (FORCED && !IS_PURE) NumberPrototype.constructor = NumberWrapper;
  75. $({ global: true, constructor: true, wrap: true, forced: FORCED }, {
  76. Number: NumberWrapper
  77. });
  78. // Use `internal/copy-constructor-properties` helper in `core-js@4`
  79. var copyConstructorProperties = function (target, source) {
  80. for (var keys = DESCRIPTORS ? getOwnPropertyNames(source) : (
  81. // ES3:
  82. 'MAX_VALUE,MIN_VALUE,NaN,NEGATIVE_INFINITY,POSITIVE_INFINITY,' +
  83. // ES2015 (in case, if modules with ES2015 Number statics required before):
  84. 'EPSILON,MAX_SAFE_INTEGER,MIN_SAFE_INTEGER,isFinite,isInteger,isNaN,isSafeInteger,parseFloat,parseInt,' +
  85. // ESNext
  86. 'fromString,range'
  87. ).split(','), j = 0, key; keys.length > j; j++) {
  88. if (hasOwn(source, key = keys[j]) && !hasOwn(target, key)) {
  89. defineProperty(target, key, getOwnPropertyDescriptor(source, key));
  90. }
  91. }
  92. };
  93. if (IS_PURE && PureNumberNamespace) copyConstructorProperties(path[NUMBER], PureNumberNamespace);
  94. if (FORCED || IS_PURE) copyConstructorProperties(path[NUMBER], NativeNumber);