source: imaps-frontend/node_modules/es-abstract/2022/StringToNumber.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

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