number-parse-int.js 605 B

123456789101112131415
  1. var global = require('../internals/global');
  2. var toString = require('../internals/to-string');
  3. var trim = require('../internals/string-trim').trim;
  4. var whitespaces = require('../internals/whitespaces');
  5. var $parseInt = global.parseInt;
  6. var hex = /^[+-]?0[Xx]/;
  7. var FORCED = $parseInt(whitespaces + '08') !== 8 || $parseInt(whitespaces + '0x16') !== 22;
  8. // `parseInt` method
  9. // https://tc39.es/ecma262/#sec-parseint-string-radix
  10. module.exports = FORCED ? function parseInt(string, radix) {
  11. var S = trim(toString(string));
  12. return $parseInt(S, (radix >>> 0) || (hex.test(S) ? 16 : 10));
  13. } : $parseInt;