MakeMatchIndicesIndexPairArray.js 2.6 KB

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