source: imaps-frontend/node_modules/es-abstract/2024/ParseHexOctet.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.4 KB
Line 
1'use strict';
2
3var GetIntrinsic = require('get-intrinsic');
4
5var $Number = GetIntrinsic('%Number%');
6var $SyntaxError = require('es-errors/syntax');
7var $TypeError = require('es-errors/type');
8
9var IsIntegralNumber = require('./IsIntegralNumber');
10var substring = require('./substring');
11
12var isNaN = require('../helpers/isNaN');
13
14// https://262.ecma-international.org/14.0/#sec-parsehexoctet
15
16module.exports = function ParseHexOctet(string, position) {
17 if (typeof string !== 'string') {
18 throw new $TypeError('Assertion failed: `string` must be a String');
19 }
20 if (!IsIntegralNumber(position) || position < 0) {
21 throw new $TypeError('Assertion failed: `position` must be a nonnegative integer');
22 }
23
24 var len = string.length; // step 1
25 if ((position + 2) > len) { // step 2
26 var error = new $SyntaxError('requested a position on a string that does not contain 2 characters at that position'); // step 2.a
27 return [error]; // step 2.b
28 }
29 var hexDigits = substring(string, position, position + 2); // step 3
30
31 var n = $Number('0x' + hexDigits);
32 if (isNaN(n)) {
33 return [new $SyntaxError('Invalid hexadecimal characters')];
34 }
35 return n;
36
37 /*
38 4. Let _parseResult_ be ParseText(StringToCodePoints(_hexDigits_), |HexDigits[~Sep]|).
39 5. If _parseResult_ is not a Parse Node, return _parseResult_.
40 6. Let _n_ be the unsigned 8-bit value corresponding with the MV of _parseResult_.
41 7. Return _n_.
42 */
43};
Note: See TracBrowser for help on using the repository browser.