MakeMatchIndicesIndexPairArray.js 2.7 KB

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