source: imaps-frontend/node_modules/es-abstract/2024/StringToNumber.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

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