es.number.constructor.js 3.6 KB

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