regex-partial-match 1.2.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. package/README.md +169 -178
  2. package/lib/core/corePartialMatchRegExp.d.ts +24 -0
  3. package/lib/core/corePartialMatchRegExp.js +22 -0
  4. package/lib/core/index.d.ts +4 -0
  5. package/lib/core/index.js +2 -0
  6. package/lib/{extend.d.ts → extend/index.d.ts} +6 -7
  7. package/lib/{extend.js → extend/index.js} +1 -1
  8. package/lib/features/index.d.ts +20 -0
  9. package/lib/features/index.js +23 -0
  10. package/lib/hitEnd/index.d.ts +45 -0
  11. package/lib/hitEnd/index.js +57 -0
  12. package/lib/hitEnd/matchHitEnd.d.ts +4 -0
  13. package/lib/hitEnd/matchHitEnd.js +51 -0
  14. package/lib/hitEnd/partRole.d.ts +3 -0
  15. package/lib/hitEnd/partRole.js +17 -0
  16. package/lib/hitEnd/truncationProbe.d.ts +8 -0
  17. package/lib/hitEnd/truncationProbe.js +187 -0
  18. package/lib/hitEnd/truncationProbeCache.d.ts +14 -0
  19. package/lib/hitEnd/truncationProbeCache.js +1 -0
  20. package/lib/index.d.ts +4 -0
  21. package/lib/index.js +4 -0
  22. package/lib/modules/backreferences/asNativeAtom.d.ts +2 -0
  23. package/lib/modules/backreferences/asNativeAtom.js +10 -0
  24. package/lib/modules/backreferences/asPreScanPart.d.ts +2 -0
  25. package/lib/modules/backreferences/asPreScanPart.js +8 -0
  26. package/lib/modules/backreferences/backreferenceRecorder.d.ts +2 -0
  27. package/lib/modules/backreferences/backreferenceRecorder.js +38 -0
  28. package/lib/modules/backreferences/caseFoldFlags.d.ts +1 -0
  29. package/lib/modules/backreferences/caseFoldFlags.js +3 -0
  30. package/lib/modules/backreferences/compileDynamic.d.ts +4 -0
  31. package/lib/modules/backreferences/compileDynamic.js +53 -0
  32. package/lib/modules/backreferences/compiledBackreferences.d.ts +9 -0
  33. package/lib/modules/backreferences/compiledBackreferences.js +9 -0
  34. package/lib/modules/backreferences/constants.d.ts +2 -0
  35. package/lib/modules/backreferences/constants.js +2 -0
  36. package/lib/modules/backreferences/execDynamic.d.ts +2 -0
  37. package/lib/modules/backreferences/execDynamic.js +43 -0
  38. package/lib/modules/backreferences/flagsAtBackreference.d.ts +2 -0
  39. package/lib/modules/backreferences/flagsAtBackreference.js +4 -0
  40. package/lib/modules/backreferences/groupName.d.ts +2 -0
  41. package/lib/modules/backreferences/groupName.js +8 -0
  42. package/lib/modules/backreferences/index.d.ts +10 -0
  43. package/lib/modules/backreferences/index.js +20 -0
  44. package/lib/modules/backreferences/longestBakedPrefixEndingInput.d.ts +1 -0
  45. package/lib/modules/backreferences/longestBakedPrefixEndingInput.js +46 -0
  46. package/lib/modules/backreferences/renderParts.d.ts +2 -0
  47. package/lib/modules/backreferences/renderParts.js +9 -0
  48. package/lib/modules/backreferences/resolvedFromScan.d.ts +2 -0
  49. package/lib/modules/backreferences/resolvedFromScan.js +10 -0
  50. package/lib/modules/backreferences/startsWithUnderFlags.d.ts +1 -0
  51. package/lib/modules/backreferences/startsWithUnderFlags.js +7 -0
  52. package/lib/modules/carets/appendMultilineCaret.d.ts +7 -0
  53. package/lib/modules/carets/appendMultilineCaret.js +181 -0
  54. package/lib/modules/carets/caretRecorder.d.ts +2 -0
  55. package/lib/modules/carets/caretRecorder.js +164 -0
  56. package/lib/modules/carets/caretSyntax.d.ts +3 -0
  57. package/lib/modules/carets/caretSyntax.js +11 -0
  58. package/lib/modules/carets/index.d.ts +9 -0
  59. package/lib/modules/carets/index.js +18 -0
  60. package/lib/modules/carets/lineTerminator.d.ts +1 -0
  61. package/lib/modules/carets/lineTerminator.js +31 -0
  62. package/lib/modules/carets/needsCaretRules.d.ts +1 -0
  63. package/lib/modules/carets/needsCaretRules.js +15 -0
  64. package/lib/modules/carets/quantifierBounds.d.ts +3 -0
  65. package/lib/modules/carets/quantifierBounds.js +18 -0
  66. package/lib/modules/index.d.ts +3 -0
  67. package/lib/modules/index.js +2 -0
  68. package/lib/partialMatchRegExp/atomSyntax.d.ts +17 -0
  69. package/lib/partialMatchRegExp/atomSyntax.js +27 -0
  70. package/lib/partialMatchRegExp/backreferenceExpansion.d.ts +5 -0
  71. package/lib/partialMatchRegExp/backreferenceExpansion.js +1 -0
  72. package/lib/partialMatchRegExp/backreferenceHook.d.ts +11 -0
  73. package/lib/partialMatchRegExp/backreferenceHook.js +1 -0
  74. package/lib/partialMatchRegExp/caretHook.d.ts +17 -0
  75. package/lib/partialMatchRegExp/caretHook.js +5 -0
  76. package/lib/partialMatchRegExp/compilePartial/compileWith.d.ts +3 -0
  77. package/lib/partialMatchRegExp/compilePartial/compileWith.js +36 -0
  78. package/lib/partialMatchRegExp/compilePartial/compiled.d.ts +29 -0
  79. package/lib/partialMatchRegExp/compilePartial/compiled.js +14 -0
  80. package/lib/partialMatchRegExp/compilePartial/constants.d.ts +4 -0
  81. package/lib/partialMatchRegExp/compilePartial/constants.js +4 -0
  82. package/lib/partialMatchRegExp/compilePartial/groupShape.d.ts +4 -0
  83. package/lib/partialMatchRegExp/compilePartial/groupShape.js +9 -0
  84. package/lib/partialMatchRegExp/compilePartial/toStatic.d.ts +3 -0
  85. package/lib/partialMatchRegExp/compilePartial/toStatic.js +4 -0
  86. package/lib/partialMatchRegExp/constants.d.ts +5 -0
  87. package/lib/partialMatchRegExp/constants.js +5 -0
  88. package/lib/partialMatchRegExp/createPartialMatchRegExp.d.ts +3 -0
  89. package/lib/partialMatchRegExp/createPartialMatchRegExp.js +21 -0
  90. package/lib/partialMatchRegExp/execFrom.d.ts +1 -0
  91. package/lib/partialMatchRegExp/execFrom.js +4 -0
  92. package/lib/partialMatchRegExp/index.d.ts +49 -0
  93. package/lib/partialMatchRegExp/index.js +31 -0
  94. package/lib/partialMatchRegExp/legacyEscape.d.ts +2 -0
  95. package/lib/partialMatchRegExp/legacyEscape.js +18 -0
  96. package/lib/partialMatchRegExp/moduleHooks.d.ts +14 -0
  97. package/lib/partialMatchRegExp/moduleHooks.js +15 -0
  98. package/lib/partialMatchRegExp/opaqueModule.d.ts +9 -0
  99. package/lib/partialMatchRegExp/opaqueModule.js +1 -0
  100. package/lib/partialMatchRegExp/part.d.ts +15 -0
  101. package/lib/partialMatchRegExp/part.js +2 -0
  102. package/lib/partialMatchRegExp/partialMatchInternals.d.ts +3 -0
  103. package/lib/partialMatchRegExp/partialMatchInternals.js +4 -0
  104. package/lib/partialMatchRegExp/quantifier.d.ts +6 -0
  105. package/lib/partialMatchRegExp/quantifier.js +9 -0
  106. package/lib/partialMatchRegExp/rawLookaroundInfo.d.ts +11 -0
  107. package/lib/partialMatchRegExp/rawLookaroundInfo.js +1 -0
  108. package/lib/partialMatchRegExp/regexFeatures.d.ts +2 -0
  109. package/lib/partialMatchRegExp/regexFeatures.js +41 -0
  110. package/lib/partialMatchRegExp/scope.d.ts +12 -0
  111. package/lib/partialMatchRegExp/scope.js +45 -0
  112. package/lib/partialMatchRegExp/walk.d.ts +16 -0
  113. package/lib/partialMatchRegExp/walk.js +412 -0
  114. package/lib/partialMatchRegExp/withModules.d.ts +13 -0
  115. package/lib/partialMatchRegExp/withModules.js +27 -0
  116. package/package.json +46 -53
  117. package/lib/compilePartial.d.ts +0 -26
  118. package/lib/compilePartial.js +0 -397
  119. package/lib/partialMatchRegExp.d.ts +0 -52
  120. package/lib/partialMatchRegExp.js +0 -126
  121. /package/lib/{escapeAtom.d.ts → modules/backreferences/escapeAtom.d.ts} +0 -0
  122. /package/lib/{escapeAtom.js → modules/backreferences/escapeAtom.js} +0 -0
@@ -0,0 +1,412 @@
1
+ import { NOT_NUMBERS_REGEX, LITERAL_BACKSLASH, LITERAL_K } from "./constants.js";
2
+ import { asOptionalAtom, isWordBoundaryAtom, DISJUNCTION_TO_END_OF_INPUT, END_ANCHOR, isRawLookaround, GROUP_CLOSING, LOOKAHEAD_OPENING, START_ANCHOR } from "./atomSyntax.js";
3
+ import { legacyEscapeAtoms } from "./legacyEscape.js";
4
+ import { ON_ALTERNATIVE, ON_CARET, ON_END, ON_GROUP, ON_LOOKAHEAD } from "./caretHook.js";
5
+ import { BACKREFERENCES_MODULE, CARETS_MODULE } from "./moduleHooks.js";
6
+ import { OCCURRENCES_REGEX } from "./quantifier.js";
7
+ import { MULTILINE, UNICODE, UNICODE_SETS, WITHIN_GROUP, WITHIN_LOOKAROUND, WITHIN_RAW_LOOKAROUND, scopeOf } from "./scope.js";
8
+ const ASCII_LETTER = /[a-z]/iy;
9
+ const TWO_HEX_DIGITS = /[0-9a-f]{2}/iy;
10
+ const FOUR_HEX_DIGITS = /[0-9a-f]{4}/iy;
11
+ function matchesAt(source, start, pattern) {
12
+ pattern.lastIndex = start;
13
+ return pattern.test(source);
14
+ }
15
+ const isZeroWidth = (part) => typeof part === "string" &&
16
+ (part === START_ANCHOR ||
17
+ part === END_ANCHOR ||
18
+ isWordBoundaryAtom(part) ||
19
+ isRawLookaround(part));
20
+ export function walk(regex, declaresNamedGroup, groupLimit, caretRecorder, backreferenceRecorder, withModifiers) {
21
+ const source = regex.source;
22
+ let i = 0;
23
+ let groupCount = 0;
24
+ let featureMask = 0;
25
+ let needs = 0;
26
+ let rawLookaroundCount = 0;
27
+ let outermostRawLookaroundCount = 0;
28
+ let rawLookarounds;
29
+ let rawReferences;
30
+ let lastBodyRunsOut = false;
31
+ function extractSlice(length) {
32
+ return source.slice(i, (i += length));
33
+ }
34
+ function process(scope) {
35
+ var _a;
36
+ const result = [];
37
+ const caretFrame = caretRecorder === null || caretRecorder === void 0 ? void 0 : caretRecorder(result, scope);
38
+ let alternativeStart = 0;
39
+ let alternativeRunsOut = false;
40
+ let everyAlternativeRunsOut = true;
41
+ function appendOptional(length) {
42
+ if (result.length === alternativeStart)
43
+ alternativeRunsOut = true;
44
+ result.push(asOptionalAtom(extractSlice(length)));
45
+ }
46
+ function appendRaw(length) {
47
+ result.push(extractSlice(length));
48
+ }
49
+ function appendBackreference(backreference) {
50
+ if (backreferenceRecorder)
51
+ backreferenceRecorder.backreference(backreference, scope);
52
+ else if (!(scope & WITHIN_RAW_LOOKAROUND))
53
+ needs |= BACKREFERENCES_MODULE;
54
+ result.push(backreference);
55
+ }
56
+ function appendDigitRun(forcedRef) {
57
+ if (result.length === alternativeStart)
58
+ alternativeRunsOut = true;
59
+ featureMask |= 512 /* Feature.backreference */;
60
+ NOT_NUMBERS_REGEX.lastIndex = i + 1;
61
+ const nextNonDigit = NOT_NUMBERS_REGEX.exec(source);
62
+ const start = i;
63
+ const end = nextNonDigit ? nextNonDigit.index : source.length;
64
+ const ref = forcedRef !== null && forcedRef !== void 0 ? forcedRef : Number(source.slice(start + 1, end));
65
+ i = end;
66
+ if (scope & WITHIN_RAW_LOOKAROUND)
67
+ (rawReferences !== null && rawReferences !== void 0 ? rawReferences : (rawReferences = [])).push({ ref, start, end });
68
+ if (ref >= 1 && ref <= groupLimit) {
69
+ appendBackreference({ ref });
70
+ return;
71
+ }
72
+ for (const atom of legacyEscapeAtoms(source.slice(start + 1, end))) {
73
+ result.push(asOptionalAtom(atom));
74
+ }
75
+ }
76
+ function appendRawLookaround(prefixLength) {
77
+ const start = i;
78
+ i += prefixLength;
79
+ const groupCountBefore = groupCount;
80
+ rawLookaroundCount++;
81
+ process(scope | WITHIN_LOOKAROUND | WITHIN_RAW_LOOKAROUND);
82
+ if (!(scope & WITHIN_RAW_LOOKAROUND)) {
83
+ const capturingGroupsOpened = groupCount - groupCountBefore;
84
+ if (rawReferences || capturingGroupsOpened) {
85
+ (rawLookarounds !== null && rawLookarounds !== void 0 ? rawLookarounds : (rawLookarounds = []))[outermostRawLookaroundCount] = {
86
+ sourceStart: start,
87
+ capturingGroupsOpened,
88
+ references: rawReferences !== null && rawReferences !== void 0 ? rawReferences : []
89
+ };
90
+ rawReferences = undefined;
91
+ }
92
+ outermostRawLookaroundCount++;
93
+ }
94
+ result.push(source.slice(start, i));
95
+ }
96
+ while (i < source.length) {
97
+ switch (source[i]) {
98
+ case "\\":
99
+ switch (source[i + 1]) {
100
+ case "c":
101
+ if (scope & UNICODE || matchesAt(source, i + 2, ASCII_LETTER)) {
102
+ featureMask |= 67108864 /* Feature.controlLetterEscape */;
103
+ appendOptional(3);
104
+ }
105
+ else {
106
+ featureMask |= 536870912 /* Feature.otherEscape */;
107
+ i++;
108
+ if (result.length === alternativeStart)
109
+ alternativeRunsOut = true;
110
+ result.push(asOptionalAtom(LITERAL_BACKSLASH));
111
+ }
112
+ break;
113
+ case "k": {
114
+ const referenceEnd = source[i + 2] === "<" ? source.indexOf(">", i) : -1;
115
+ if (referenceEnd !== -1 && declaresNamedGroup) {
116
+ featureMask |= 1024 /* Feature.namedBackreference */;
117
+ const ref = source.slice(i + 3, referenceEnd);
118
+ i = referenceEnd + 1;
119
+ if (result.length === alternativeStart)
120
+ alternativeRunsOut = true;
121
+ appendBackreference({ ref });
122
+ }
123
+ else {
124
+ i += 2;
125
+ if (result.length === alternativeStart)
126
+ alternativeRunsOut = true;
127
+ result.push(asOptionalAtom(LITERAL_K));
128
+ }
129
+ break;
130
+ }
131
+ case "u":
132
+ if (scope & UNICODE ||
133
+ matchesAt(source, i + 2, FOUR_HEX_DIGITS)) {
134
+ featureMask |= 268435456 /* Feature.unicodeEscapeSequence */;
135
+ appendOptional(source[i + 2] === "{" ? source.indexOf("}", i) - i + 1 : 6);
136
+ }
137
+ else {
138
+ featureMask |= 536870912 /* Feature.otherEscape */;
139
+ appendOptional(2);
140
+ }
141
+ break;
142
+ case "p":
143
+ case "P":
144
+ if (scope & UNICODE) {
145
+ featureMask |= 8388608 /* Feature.unicodePropertyEscape */;
146
+ appendOptional(source.indexOf("}", i) - i + 1);
147
+ }
148
+ else {
149
+ appendOptional(2);
150
+ }
151
+ break;
152
+ case "x":
153
+ if (scope & UNICODE || matchesAt(source, i + 2, TWO_HEX_DIGITS)) {
154
+ featureMask |= 134217728 /* Feature.hexEscapeSequence */;
155
+ appendOptional(4);
156
+ }
157
+ else {
158
+ featureMask |= 536870912 /* Feature.otherEscape */;
159
+ appendOptional(2);
160
+ }
161
+ break;
162
+ case "b":
163
+ featureMask |= 8 /* Feature.wordBoundary */;
164
+ appendOptional(2);
165
+ break;
166
+ case "B":
167
+ featureMask |= 16 /* Feature.nonWordBoundary */;
168
+ appendOptional(2);
169
+ break;
170
+ case "f":
171
+ case "n":
172
+ case "r":
173
+ case "t":
174
+ case "v":
175
+ featureMask |= 33554432 /* Feature.controlEscape */;
176
+ appendOptional(2);
177
+ break;
178
+ case "0":
179
+ if (scope & UNICODE) {
180
+ featureMask |= 536870912 /* Feature.otherEscape */;
181
+ appendOptional(2);
182
+ }
183
+ else {
184
+ appendDigitRun(0);
185
+ }
186
+ break;
187
+ case "1":
188
+ case "2":
189
+ case "3":
190
+ case "4":
191
+ case "5":
192
+ case "6":
193
+ case "7":
194
+ case "8":
195
+ case "9":
196
+ appendDigitRun();
197
+ break;
198
+ case "d":
199
+ case "D":
200
+ case "w":
201
+ case "W":
202
+ case "s":
203
+ case "S":
204
+ featureMask |= 16777216 /* Feature.characterClassEscape */;
205
+ appendOptional(2);
206
+ break;
207
+ default:
208
+ featureMask |= 536870912 /* Feature.otherEscape */;
209
+ appendOptional(2);
210
+ break;
211
+ }
212
+ break;
213
+ case "[": {
214
+ featureMask |= 131072 /* Feature.characterClass */;
215
+ let depth = 1, j = i + 1, previousSetOperatorCharacter;
216
+ while (depth) {
217
+ const character = source[j];
218
+ switch (character) {
219
+ case "\\":
220
+ j += 2;
221
+ previousSetOperatorCharacter = undefined;
222
+ continue;
223
+ case "[":
224
+ if (scope & UNICODE_SETS) {
225
+ featureMask |= 262144 /* Feature.nestedCharacterClass */;
226
+ depth++;
227
+ }
228
+ break;
229
+ case "]":
230
+ depth--;
231
+ break;
232
+ case "&":
233
+ if (scope & UNICODE_SETS &&
234
+ previousSetOperatorCharacter === "&") {
235
+ featureMask |= 524288 /* Feature.classIntersection */;
236
+ }
237
+ break;
238
+ case "-":
239
+ if (scope & UNICODE_SETS &&
240
+ previousSetOperatorCharacter === "-") {
241
+ featureMask |= 1048576 /* Feature.classSubtraction */;
242
+ }
243
+ break;
244
+ }
245
+ previousSetOperatorCharacter = character;
246
+ j++;
247
+ }
248
+ appendOptional(j - i);
249
+ break;
250
+ }
251
+ case "^": {
252
+ featureMask |= 2 /* Feature.startAnchor */;
253
+ i++;
254
+ const leadsAlternative = result.length === alternativeStart;
255
+ if (caretFrame)
256
+ caretFrame(ON_CARET);
257
+ else {
258
+ if (!(scope & WITHIN_RAW_LOOKAROUND) &&
259
+ (scope & (WITHIN_GROUP | WITHIN_LOOKAROUND) ||
260
+ (scope & MULTILINE &&
261
+ !result.slice(alternativeStart).every(isZeroWidth))))
262
+ needs |= CARETS_MODULE;
263
+ result.push(START_ANCHOR);
264
+ }
265
+ if (leadsAlternative)
266
+ alternativeStart = result.length;
267
+ break;
268
+ }
269
+ case "$":
270
+ featureMask |= 4 /* Feature.endAnchor */;
271
+ appendRaw(1);
272
+ break;
273
+ case "|":
274
+ featureMask |= 2097152 /* Feature.disjunction */;
275
+ caretFrame === null || caretFrame === void 0 ? void 0 : caretFrame(ON_ALTERNATIVE);
276
+ everyAlternativeRunsOut && (everyAlternativeRunsOut = alternativeRunsOut || result.length === alternativeStart);
277
+ alternativeRunsOut = false;
278
+ alternativeStart = result.length + 1;
279
+ appendRaw(1);
280
+ break;
281
+ case "*":
282
+ case "+":
283
+ case "?":
284
+ featureMask |= 4194304 /* Feature.quantifier */;
285
+ appendRaw(1);
286
+ break;
287
+ case "{": {
288
+ OCCURRENCES_REGEX.lastIndex = i;
289
+ const regExpExecArray = OCCURRENCES_REGEX.exec(source);
290
+ if (regExpExecArray) {
291
+ featureMask |= 4194304 /* Feature.quantifier */;
292
+ appendRaw(regExpExecArray[0].length);
293
+ }
294
+ else {
295
+ appendOptional(1);
296
+ }
297
+ break;
298
+ }
299
+ case "(": {
300
+ let opening;
301
+ let groupScope = scope;
302
+ let closing = DISJUNCTION_TO_END_OF_INPUT;
303
+ let groupNumber = 0;
304
+ if (source[i + 1] == "?") {
305
+ switch (source[i + 2]) {
306
+ case ":":
307
+ featureMask |= 16384 /* Feature.nonCapturingGroup */;
308
+ opening = extractSlice(3);
309
+ break;
310
+ case "=": {
311
+ featureMask |= 32 /* Feature.lookahead */;
312
+ i += 3;
313
+ const body = process(scope | WITHIN_LOOKAROUND);
314
+ caretFrame === null || caretFrame === void 0 ? void 0 : caretFrame(ON_LOOKAHEAD, body, scope, source, i);
315
+ result.push(LOOKAHEAD_OPENING, ...body, GROUP_CLOSING);
316
+ break;
317
+ }
318
+ case "-":
319
+ case "i":
320
+ case "s":
321
+ case "m": {
322
+ const flagsStart = i + 2, colonIndex = source.indexOf(":", flagsStart);
323
+ const modifiers = source.slice(flagsStart, colonIndex);
324
+ const removalIndex = modifiers.indexOf("-");
325
+ featureMask |=
326
+ removalIndex === -1 || removalIndex === modifiers.length - 1
327
+ ? 32768 /* Feature.modifierGroup */
328
+ : 65536 /* Feature.modifierGroupWithRemoval */;
329
+ groupScope = (_a = withModifiers === null || withModifiers === void 0 ? void 0 : withModifiers(scope, modifiers)) !== null && _a !== void 0 ? _a : scope;
330
+ closing = GROUP_CLOSING;
331
+ opening = extractSlice(colonIndex + 1 - i);
332
+ break;
333
+ }
334
+ case "!":
335
+ featureMask |= 64 /* Feature.negativeLookahead */;
336
+ appendRawLookaround(3);
337
+ break;
338
+ case "<":
339
+ switch (source[i + 3]) {
340
+ case "=":
341
+ featureMask |= 128 /* Feature.lookbehind */;
342
+ appendRawLookaround(4);
343
+ break;
344
+ case "!":
345
+ featureMask |= 256 /* Feature.negativeLookbehind */;
346
+ appendRawLookaround(4);
347
+ break;
348
+ default:
349
+ featureMask |= 2048 /* Feature.namedGroup */;
350
+ featureMask |= 4096 /* Feature.capturingGroup */;
351
+ if (scope & WITHIN_LOOKAROUND)
352
+ featureMask |= 8192 /* Feature.lookaroundCapture */;
353
+ groupNumber = ++groupCount;
354
+ opening = extractSlice(source.indexOf(">", i) - i + 1);
355
+ break;
356
+ }
357
+ break;
358
+ }
359
+ }
360
+ else {
361
+ featureMask |= 4096 /* Feature.capturingGroup */;
362
+ if (scope & WITHIN_LOOKAROUND)
363
+ featureMask |= 8192 /* Feature.lookaroundCapture */;
364
+ groupNumber = ++groupCount;
365
+ opening = extractSlice(1);
366
+ }
367
+ if (opening === undefined)
368
+ break;
369
+ const opensAlternative = result.length === alternativeStart;
370
+ const rawLookaroundsBefore = rawLookaroundCount;
371
+ const body = process(groupScope | WITHIN_GROUP);
372
+ const runsOut = lastBodyRunsOut;
373
+ const containsRawLookaround = rawLookaroundCount !== rawLookaroundsBefore;
374
+ const caretGroup = caretFrame === null || caretFrame === void 0 ? void 0 : caretFrame(ON_GROUP, body, groupScope, source, i, closing, containsRawLookaround);
375
+ if (caretGroup)
376
+ closing = caretGroup.closing;
377
+ if (!containsRawLookaround && runsOut && !(caretGroup === null || caretGroup === void 0 ? void 0 : caretGroup.forced)) {
378
+ closing = GROUP_CLOSING;
379
+ if (opensAlternative && !(caretGroup === null || caretGroup === void 0 ? void 0 : caretGroup.stays))
380
+ alternativeRunsOut = true;
381
+ }
382
+ else if (containsRawLookaround)
383
+ closing = DISJUNCTION_TO_END_OF_INPUT;
384
+ result.push(opening, ...body, closing);
385
+ if (groupNumber)
386
+ backreferenceRecorder === null || backreferenceRecorder === void 0 ? void 0 : backreferenceRecorder.groupClosed(groupNumber, opening);
387
+ break;
388
+ }
389
+ case ")":
390
+ ++i;
391
+ caretFrame === null || caretFrame === void 0 ? void 0 : caretFrame(ON_END);
392
+ lastBodyRunsOut =
393
+ everyAlternativeRunsOut &&
394
+ (alternativeRunsOut || result.length === alternativeStart);
395
+ return result;
396
+ default:
397
+ featureMask |= 1 /* Feature.patternCharacter */;
398
+ appendOptional(
399
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- i < source.length is the loop invariant, so codePointAt(i) is always defined
400
+ scope & UNICODE && source.codePointAt(i) > 0xffff ? 2 : 1);
401
+ break;
402
+ }
403
+ }
404
+ caretFrame === null || caretFrame === void 0 ? void 0 : caretFrame(ON_END);
405
+ lastBodyRunsOut =
406
+ everyAlternativeRunsOut &&
407
+ (alternativeRunsOut || result.length === alternativeStart);
408
+ return result;
409
+ }
410
+ const parts = process(scopeOf(regex));
411
+ return { parts, featureMask, needs, rawLookarounds };
412
+ }
@@ -0,0 +1,13 @@
1
+ import type { Module } from "./opaqueModule.ts";
2
+ import type { PartialMatchRegExpConstructor } from "./index.ts";
3
+ /**
4
+ * The `PartialMatchRegExp` class that applies the rules the given modules
5
+ * supply.
6
+ *
7
+ * The same set of modules, in any order, returns the same class.
8
+ *
9
+ * @param modules - The modules to bind, from `regex-partial-match/modules`
10
+ * @returns The `PartialMatchRegExp` class that binds `modules`
11
+ * @throws `TypeError` if any argument is not a module
12
+ */
13
+ export default function withModules(...modules: Module[]): PartialMatchRegExpConstructor;
@@ -0,0 +1,27 @@
1
+ import createPartialMatchRegExp from "./createPartialMatchRegExp.js";
2
+ import { definedModuleHooks, moduleHooks } from "./moduleHooks.js";
3
+ const boundByModuleMask = [];
4
+ const isDefinedHooks = (value) => definedModuleHooks.has(value);
5
+ /**
6
+ * The `PartialMatchRegExp` class that applies the rules the given modules
7
+ * supply.
8
+ *
9
+ * The same set of modules, in any order, returns the same class.
10
+ *
11
+ * @param modules - The modules to bind, from `regex-partial-match/modules`
12
+ * @returns The `PartialMatchRegExp` class that binds `modules`
13
+ * @throws `TypeError` if any argument is not a module
14
+ */
15
+ export default function withModules(...modules) {
16
+ const hooks = {};
17
+ let moduleMask = 0;
18
+ for (const module of modules) {
19
+ const bound = Reflect.get(Object(module), moduleHooks);
20
+ if (!isDefinedHooks(bound))
21
+ throw new TypeError("Not a module");
22
+ moduleMask |= bound.bit;
23
+ Object.assign(hooks, bound);
24
+ }
25
+ return (boundByModuleMask[moduleMask] ||
26
+ (boundByModuleMask[moduleMask] = createPartialMatchRegExp(Object.freeze(hooks))));
27
+ }
package/package.json CHANGED
@@ -1,17 +1,7 @@
1
1
  {
2
2
  "name": "regex-partial-match",
3
- "version": "1.2.0",
3
+ "version": "2.0.0",
4
4
  "description": "A regular expression transform for partial matching",
5
- "devEngines": {
6
- "runtime": {
7
- "name": "node",
8
- "version": ">=23"
9
- },
10
- "packageManager": {
11
- "name": "npm",
12
- "version": "11.17.0+sha512.3eeaf18997b11070d313849268b23766b9db0068997dec9471073170fe43fa17f2b4d0337bf0f52330ee2274e7f5754b21b01052742e48f5c9c74d8b1e32ef43"
13
- }
14
- },
15
5
  "keywords": [
16
6
  "regexp",
17
7
  "regular expressions",
@@ -33,17 +23,52 @@
33
23
  }
34
24
  ],
35
25
  "type": "module",
36
- "main": "lib/partialMatchRegExp.js",
26
+ "main": "lib/index.js",
37
27
  "exports": {
38
28
  ".": {
39
- "types": "./lib/partialMatchRegExp.d.ts",
40
- "import": "./lib/partialMatchRegExp.js",
41
- "default": "./lib/partialMatchRegExp.js"
29
+ "types": "./lib/index.d.ts",
30
+ "import": "./lib/index.js",
31
+ "default": "./lib/index.js"
42
32
  },
43
33
  "./extend": {
44
- "types": "./lib/extend.d.ts",
45
- "import": "./lib/extend.js",
46
- "default": "./lib/extend.js"
34
+ "types": "./lib/extend/index.d.ts",
35
+ "import": "./lib/extend/index.js",
36
+ "default": "./lib/extend/index.js"
37
+ },
38
+ "./partialMatchRegExp": {
39
+ "types": "./lib/partialMatchRegExp/index.d.ts",
40
+ "import": "./lib/partialMatchRegExp/index.js",
41
+ "default": "./lib/partialMatchRegExp/index.js"
42
+ },
43
+ "./hitEnd": {
44
+ "types": "./lib/hitEnd/index.d.ts",
45
+ "import": "./lib/hitEnd/index.js",
46
+ "default": "./lib/hitEnd/index.js"
47
+ },
48
+ "./features": {
49
+ "types": "./lib/features/index.d.ts",
50
+ "import": "./lib/features/index.js",
51
+ "default": "./lib/features/index.js"
52
+ },
53
+ "./core": {
54
+ "types": "./lib/core/index.d.ts",
55
+ "import": "./lib/core/index.js",
56
+ "default": "./lib/core/index.js"
57
+ },
58
+ "./modules": {
59
+ "types": "./lib/modules/index.d.ts",
60
+ "import": "./lib/modules/index.js",
61
+ "default": "./lib/modules/index.js"
62
+ },
63
+ "./modules/carets": {
64
+ "types": "./lib/modules/carets/index.d.ts",
65
+ "import": "./lib/modules/carets/index.js",
66
+ "default": "./lib/modules/carets/index.js"
67
+ },
68
+ "./modules/backreferences": {
69
+ "types": "./lib/modules/backreferences/index.d.ts",
70
+ "import": "./lib/modules/backreferences/index.js",
71
+ "default": "./lib/modules/backreferences/index.js"
47
72
  }
48
73
  },
49
74
  "files": [
@@ -53,42 +78,10 @@
53
78
  "type": "git",
54
79
  "url": "git+https://github.com/TomStrepsil/regex-partial-match.git"
55
80
  },
56
- "workspaces": [
57
- "benchmarking"
81
+ "sideEffects": [
82
+ "./lib/extend/index.js"
58
83
  ],
59
- "sideEffects": false,
60
- "scripts": {
61
- "lint": "eslint .",
62
- "lint:fix": "eslint . --fix",
63
- "prepare": "simple-git-hooks",
64
- "prepublishOnly": "tsc -p tsconfig.build.json",
65
- "ci:smoke-test-built-package": "node .github/scripts/smoke-test-built-package.ts",
66
- "test": "vitest run --config ./test/vitest.config.ts",
67
- "test:watch": "vitest --config ./test/vitest.config.ts"
68
- },
69
- "simple-git-hooks": {
70
- "pre-commit": "node_modules/.bin/lint-staged"
71
- },
72
- "lint-staged": {
73
- "*.{ts,js}": [
74
- "eslint --fix"
75
- ]
76
- },
77
84
  "publishConfig": {
78
85
  "registry": "https://registry.npmjs.org"
79
- },
80
- "devDependencies": {
81
- "@eslint/js": "^9.17.0",
82
- "@eslint/markdown": "^7.5.1",
83
- "@types/node": "^25.9.3",
84
- "@typescript/lib-es2018": "file:types/lib-es2018",
85
- "@typescript/lib-es2022": "file:types/lib-es2022",
86
- "eslint": "^9.17.0",
87
- "jiti": "^2.6.1",
88
- "lint-staged": "^15.2.11",
89
- "simple-git-hooks": "^2.12.1",
90
- "typescript": "^6.0.3",
91
- "typescript-eslint": "^8.18.1",
92
- "vitest": "^4.0.15"
93
86
  }
94
- }
87
+ }
@@ -1,26 +0,0 @@
1
- declare const REGEX_FEATURES: readonly ["patternCharacter", "startAnchor", "endAnchor", "wordBoundary", "nonWordBoundary", "lookahead", "negativeLookahead", "lookbehind", "negativeLookbehind", "backreference", "namedBackreference", "namedGroup", "capturingGroup", "lookaroundCapture", "nonCapturingGroup", "modifierGroup", "modifierGroupWithRemoval", "characterClass", "nestedCharacterClass", "classIntersection", "classSubtraction", "disjunction", "quantifier", "unicodePropertyEscape", "characterClassEscape", "controlEscape", "controlLetterEscape", "hexEscapeSequence", "unicodeEscapeSequence", "otherEscape"];
2
- export type RegexFeature = (typeof REGEX_FEATURES)[number];
3
- export interface DynamicPath {
4
- originalCaptureScan: RegExp;
5
- preScan: RegExp;
6
- expand: (capture: RegExpExecArray) => string;
7
- }
8
- declare abstract class Compiled {
9
- private readonly _featureMask;
10
- private _features?;
11
- constructor(_featureMask: number);
12
- get features(): ReadonlySet<RegexFeature>;
13
- }
14
- declare class CompiledStatic extends Compiled {
15
- readonly regex: RegExp;
16
- readonly kind = "static";
17
- constructor(regex: RegExp, featureMask: number);
18
- }
19
- declare class CompiledDynamic extends Compiled {
20
- readonly dynamic: DynamicPath;
21
- readonly kind = "dynamic";
22
- constructor(dynamic: DynamicPath, featureMask: number);
23
- }
24
- export type CompiledPartial = CompiledStatic | CompiledDynamic;
25
- export declare const compilePartial: (regex: RegExp) => CompiledPartial;
26
- export {};