source: imaps-frontend/node_modules/es-abstract/2023/MakeMatchIndicesIndexPairArray.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: 2.6 KB
Line 
1'use strict';
2
3var $TypeError = require('es-errors/type');
4
5var ArrayCreate = require('./ArrayCreate');
6var CreateDataPropertyOrThrow = require('./CreateDataPropertyOrThrow');
7var GetMatchIndexPair = require('./GetMatchIndexPair');
8var IsArray = require('./IsArray');
9var OrdinaryObjectCreate = require('./OrdinaryObjectCreate');
10var ToString = require('./ToString');
11
12var every = require('../helpers/every');
13var isMatchRecord = require('../helpers/records/match-record');
14
15var isStringOrUndefined = function isStringOrUndefined(s) {
16 return typeof s === 'undefined' || typeof s === 'string';
17};
18
19var isMatchRecordOrUndefined = function isMatchRecordOrUndefined(m) {
20 return typeof m === 'undefined' || isMatchRecord(m);
21};
22
23var MAX_ARRAY_LENGTH = Math.pow(2, 32) - 1;
24
25// https://262.ecma-international.org/13.0/#sec-getmatchindexpair
26
27module.exports = function MakeMatchIndicesIndexPairArray(S, indices, groupNames, hasGroups) {
28 if (typeof S !== 'string') {
29 throw new $TypeError('Assertion failed: `S` must be a String');
30 }
31 if (!IsArray(indices) || !every(indices, isMatchRecordOrUndefined)) {
32 throw new $TypeError('Assertion failed: `indices` must be a List of either Match Records or `undefined`');
33 }
34 if (!IsArray(groupNames) || !every(groupNames, isStringOrUndefined)) {
35 throw new $TypeError('Assertion failed: `groupNames` must be a List of either Strings or `undefined`');
36 }
37 if (typeof hasGroups !== 'boolean') {
38 throw new $TypeError('Assertion failed: `hasGroups` must be a Boolean');
39 }
40
41 var n = indices.length; // step 1
42 if (!(n < MAX_ARRAY_LENGTH)) {
43 throw new $TypeError('Assertion failed: `indices` length must be less than the max array size, 2**32 - 1');
44 }
45 if (groupNames.length !== n - 1) {
46 throw new $TypeError('Assertion failed: `groupNames` must have exactly one fewer item than `indices`');
47 }
48
49 var A = ArrayCreate(n); // step 5
50 var groups = hasGroups ? OrdinaryObjectCreate(null) : void undefined; // step 6-7
51 CreateDataPropertyOrThrow(A, 'groups', groups); // step 8
52
53 for (var i = 0; i < n; i += 1) { // step 9
54 var matchIndices = indices[i]; // step 9.a
55 // eslint-disable-next-line no-negated-condition
56 var matchIndexPair = typeof matchIndices !== 'undefined' ? GetMatchIndexPair(S, matchIndices) : void undefined; // step 9.b-9.c
57 CreateDataPropertyOrThrow(A, ToString(i), matchIndexPair); // step 9.d
58 if (i > 0 && typeof groupNames[i - 1] !== 'undefined') { // step 9.e
59 if (!groups) {
60 throw new $TypeError('if `hasGroups` is `false`, `groupNames` can only contain `undefined` values');
61 }
62 CreateDataPropertyOrThrow(groups, groupNames[i - 1], matchIndexPair); // step 9.e.i
63 }
64 }
65 return A; // step 10
66};
Note: See TracBrowser for help on using the repository browser.