es6.number.constructor.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. 'use strict';
  2. var global = require('./_global');
  3. var has = require('./_has');
  4. var cof = require('./_cof');
  5. var inheritIfRequired = require('./_inherit-if-required');
  6. var toPrimitive = require('./_to-primitive');
  7. var fails = require('./_fails');
  8. var gOPN = require('./_object-gopn').f;
  9. var gOPD = require('./_object-gopd').f;
  10. var dP = require('./_object-dp').f;
  11. var $trim = require('./_string-trim').trim;
  12. var NUMBER = 'Number';
  13. var $Number = global[NUMBER];
  14. var Base = $Number;
  15. var proto = $Number.prototype;
  16. // Opera ~12 has broken Object#toString
  17. var BROKEN_COF = cof(require('./_object-create')(proto)) == NUMBER;
  18. var TRIM = 'trim' in String.prototype;
  19. // 7.1.3 ToNumber(argument)
  20. var toNumber = function (argument) {
  21. var it = toPrimitive(argument, false);
  22. if (typeof it == 'string' && it.length > 2) {
  23. it = TRIM ? it.trim() : $trim(it, 3);
  24. var first = it.charCodeAt(0);
  25. var third, radix, maxCode;
  26. if (first === 43 || first === 45) {
  27. third = it.charCodeAt(2);
  28. if (third === 88 || third === 120) return NaN; // Number('+0x1') should be NaN, old V8 fix
  29. } else if (first === 48) {
  30. switch (it.charCodeAt(1)) {
  31. case 66: case 98: radix = 2; maxCode = 49; break; // fast equal /^0b[01]+$/i
  32. case 79: case 111: radix = 8; maxCode = 55; break; // fast equal /^0o[0-7]+$/i
  33. default: return +it;
  34. }
  35. for (var digits = it.slice(2), i = 0, l = digits.length, code; i < l; i++) {
  36. code = digits.charCodeAt(i);
  37. // parseInt parses a string to a first unavailable symbol
  38. // but ToNumber should return NaN if a string contains unavailable symbols
  39. if (code < 48 || code > maxCode) return NaN;
  40. } return parseInt(digits, radix);
  41. }
  42. } return +it;
  43. };
  44. if (!$Number(' 0o1') || !$Number('0b1') || $Number('+0x1')) {
  45. $Number = function Number(value) {
  46. var it = arguments.length < 1 ? 0 : value;
  47. var that = this;
  48. return that instanceof $Number
  49. // check on 1..constructor(foo) case
  50. && (BROKEN_COF ? fails(function () { proto.valueOf.call(that); }) : cof(that) != NUMBER)
  51. ? inheritIfRequired(new Base(toNumber(it)), that, $Number) : toNumber(it);
  52. };
  53. for (var keys = require('./_descriptors') ? gOPN(Base) : (
  54. // ES3:
  55. 'MAX_VALUE,MIN_VALUE,NaN,NEGATIVE_INFINITY,POSITIVE_INFINITY,' +
  56. // ES6 (in case, if modules with ES6 Number statics required before):
  57. 'EPSILON,isFinite,isInteger,isNaN,isSafeInteger,MAX_SAFE_INTEGER,' +
  58. 'MIN_SAFE_INTEGER,parseFloat,parseInt,isInteger'
  59. ).split(','), j = 0, key; keys.length > j; j++) {
  60. if (has(Base, key = keys[j]) && !has($Number, key)) {
  61. dP($Number, key, gOPD(Base, key));
  62. }
  63. }
  64. $Number.prototype = proto;
  65. proto.constructor = $Number;
  66. require('./_redefine')(global, NUMBER, $Number);
  67. }