number-parse-int.js 931 B

12345678910111213141516171819202122
  1. var global = require('../internals/global');
  2. var fails = require('../internals/fails');
  3. var uncurryThis = require('../internals/function-uncurry-this');
  4. var toString = require('../internals/to-string');
  5. var trim = require('../internals/string-trim').trim;
  6. var whitespaces = require('../internals/whitespaces');
  7. var $parseInt = global.parseInt;
  8. var Symbol = global.Symbol;
  9. var ITERATOR = Symbol && Symbol.iterator;
  10. var hex = /^[+-]?0x/i;
  11. var exec = uncurryThis(hex.exec);
  12. var FORCED = $parseInt(whitespaces + '08') !== 8 || $parseInt(whitespaces + '0x16') !== 22
  13. // MS Edge 18- broken with boxed symbols
  14. || (ITERATOR && !fails(function () { $parseInt(Object(ITERATOR)); }));
  15. // `parseInt` method
  16. // https://tc39.es/ecma262/#sec-parseint-string-radix
  17. module.exports = FORCED ? function parseInt(string, radix) {
  18. var S = trim(toString(string));
  19. return $parseInt(S, (radix >>> 0) || (exec(hex, S) ? 16 : 10));
  20. } : $parseInt;