es.number.constructor.js 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. 'use strict';
  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;
  18. var NUMBER = 'Number';
  19. var NativeNumber = global[NUMBER];
  20. var NumberPrototype = NativeNumber.prototype;
  21. var TypeError = global.TypeError;
  22. var arraySlice = uncurryThis(''.slice);
  23. var charCodeAt = uncurryThis(''.charCodeAt);
  24. // `ToNumeric` abstract operation
  25. // https://tc39.es/ecma262/#sec-tonumeric
  26. var toNumeric = function (value) {
  27. var primValue = toPrimitive(value, 'number');
  28. return typeof primValue == 'bigint' ? primValue : toNumber(primValue);
  29. };
  30. // `ToNumber` abstract operation
  31. // https://tc39.es/ecma262/#sec-tonumber
  32. var toNumber = function (argument) {
  33. var it = toPrimitive(argument, 'number');
  34. var first, third, radix, maxCode, digits, length, index, code;
  35. if (isSymbol(it)) throw TypeError('Cannot convert a Symbol value to a number');
  36. if (typeof it == 'string' && it.length > 2) {
  37. it = trim(it);
  38. first = charCodeAt(it, 0);
  39. if (first === 43 || first === 45) {
  40. third = charCodeAt(it, 2);
  41. if (third === 88 || third === 120) return NaN; // Number('+0x1') should be NaN, old V8 fix
  42. } else if (first === 48) {
  43. switch (charCodeAt(it, 1)) {
  44. case 66: case 98: radix = 2; maxCode = 49; break; // fast equal of /^0b[01]+$/i
  45. case 79: case 111: radix = 8; maxCode = 55; break; // fast equal of /^0o[0-7]+$/i
  46. default: return +it;
  47. }
  48. digits = arraySlice(it, 2);
  49. length = digits.length;
  50. for (index = 0; index < length; index++) {
  51. code = charCodeAt(digits, index);
  52. // parseInt parses a string to a first unavailable symbol
  53. // but ToNumber should return NaN if a string contains unavailable symbols
  54. if (code < 48 || code > maxCode) return NaN;
  55. } return parseInt(digits, radix);
  56. }
  57. } return +it;
  58. };
  59. // `Number` constructor
  60. // https://tc39.es/ecma262/#sec-number-constructor
  61. if (isForced(NUMBER, !NativeNumber(' 0o1') || !NativeNumber('0b1') || NativeNumber('+0x1'))) {
  62. var NumberWrapper = function Number(value) {
  63. var n = arguments.length < 1 ? 0 : NativeNumber(toNumeric(value));
  64. var dummy = this;
  65. // check on 1..constructor(foo) case
  66. return isPrototypeOf(NumberPrototype, dummy) && fails(function () { thisNumberValue(dummy); })
  67. ? inheritIfRequired(Object(n), dummy, NumberWrapper) : n;
  68. };
  69. for (var keys = DESCRIPTORS ? getOwnPropertyNames(NativeNumber) : (
  70. // ES3:
  71. 'MAX_VALUE,MIN_VALUE,NaN,NEGATIVE_INFINITY,POSITIVE_INFINITY,' +
  72. // ES2015 (in case, if modules with ES2015 Number statics required before):
  73. 'EPSILON,MAX_SAFE_INTEGER,MIN_SAFE_INTEGER,isFinite,isInteger,isNaN,isSafeInteger,parseFloat,parseInt,' +
  74. // ESNext
  75. 'fromString,range'
  76. ).split(','), j = 0, key; keys.length > j; j++) {
  77. if (hasOwn(NativeNumber, key = keys[j]) && !hasOwn(NumberWrapper, key)) {
  78. defineProperty(NumberWrapper, key, getOwnPropertyDescriptor(NativeNumber, key));
  79. }
  80. }
  81. NumberWrapper.prototype = NumberPrototype;
  82. NumberPrototype.constructor = NumberWrapper;
  83. redefine(global, NUMBER, NumberWrapper);
  84. }