StringToNumber.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $Number = GetIntrinsic('%Number%');
  4. var $RegExp = GetIntrinsic('%RegExp%');
  5. var $TypeError = require('es-errors/type');
  6. var $parseInteger = GetIntrinsic('%parseInt%');
  7. var callBound = require('call-bind/callBound');
  8. var regexTester = require('safe-regex-test');
  9. var $strSlice = callBound('String.prototype.slice');
  10. var isBinary = regexTester(/^0b[01]+$/i);
  11. var isOctal = regexTester(/^0o[0-7]+$/i);
  12. var isInvalidHexLiteral = regexTester(/^[-+]0x[0-9a-f]+$/i);
  13. var nonWS = ['\u0085', '\u200b', '\ufffe'].join('');
  14. var nonWSregex = new $RegExp('[' + nonWS + ']', 'g');
  15. var hasNonWS = regexTester(nonWSregex);
  16. var $trim = require('string.prototype.trim');
  17. // https://262.ecma-international.org/13.0/#sec-stringtonumber
  18. module.exports = function StringToNumber(argument) {
  19. if (typeof argument !== 'string') {
  20. throw new $TypeError('Assertion failed: `argument` is not a String');
  21. }
  22. if (isBinary(argument)) {
  23. return $Number($parseInteger($strSlice(argument, 2), 2));
  24. }
  25. if (isOctal(argument)) {
  26. return $Number($parseInteger($strSlice(argument, 2), 8));
  27. }
  28. if (hasNonWS(argument) || isInvalidHexLiteral(argument)) {
  29. return NaN;
  30. }
  31. var trimmed = $trim(argument);
  32. if (trimmed !== argument) {
  33. return StringToNumber(trimmed);
  34. }
  35. return $Number(argument);
  36. };