regex-partial-match 0.4.0 → 1.1.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.
- package/README.md +207 -70
- package/lib/compilePartial.d.ts +16 -0
- package/lib/compilePartial.js +334 -0
- package/lib/escapeAtom.d.ts +1 -0
- package/lib/escapeAtom.js +5 -0
- package/lib/extend.d.ts +3 -4
- package/lib/extend.js +2 -2
- package/lib/partialMatchRegExp.d.ts +35 -0
- package/lib/partialMatchRegExp.js +114 -0
- package/package.json +13 -7
- package/lib/index.d.ts +0 -31
- package/lib/index.js +0 -199
package/lib/index.js
DELETED
|
@@ -1,199 +0,0 @@
|
|
|
1
|
-
var occurrencesRegex = /\{\d+,?\d*\}/g;
|
|
2
|
-
var notNumbersRegex = /\D/g;
|
|
3
|
-
var createPartialMatchRegex = function (regex) {
|
|
4
|
-
var source = regex.source;
|
|
5
|
-
var isUnicode = regex.unicode || regex.unicodeSets;
|
|
6
|
-
var i = 0;
|
|
7
|
-
function extractSubstring(length) {
|
|
8
|
-
return source.substring(i, (i += length));
|
|
9
|
-
}
|
|
10
|
-
function process() {
|
|
11
|
-
var result = "";
|
|
12
|
-
function appendOptional(length) {
|
|
13
|
-
result += "(?:" + extractSubstring(length) + "|$)";
|
|
14
|
-
}
|
|
15
|
-
function appendRaw(length) {
|
|
16
|
-
result += extractSubstring(length);
|
|
17
|
-
}
|
|
18
|
-
while (i < source.length) {
|
|
19
|
-
switch (source[i]) {
|
|
20
|
-
case "\\":
|
|
21
|
-
switch (source[i + 1]) {
|
|
22
|
-
case "c":
|
|
23
|
-
appendOptional(3);
|
|
24
|
-
break;
|
|
25
|
-
case "k":
|
|
26
|
-
appendOptional(source.indexOf(">", i) - i + 1);
|
|
27
|
-
break;
|
|
28
|
-
case "u":
|
|
29
|
-
if (isUnicode && source[i + 2] === "{") {
|
|
30
|
-
appendOptional(source.indexOf("}", i) - i + 1);
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
appendOptional(6);
|
|
34
|
-
}
|
|
35
|
-
break;
|
|
36
|
-
case "p":
|
|
37
|
-
case "P":
|
|
38
|
-
if (isUnicode) {
|
|
39
|
-
appendOptional(source.indexOf("}", i) - i + 1);
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
42
|
-
appendOptional(2);
|
|
43
|
-
}
|
|
44
|
-
break;
|
|
45
|
-
case "x":
|
|
46
|
-
appendOptional(4);
|
|
47
|
-
break;
|
|
48
|
-
case "1":
|
|
49
|
-
case "2":
|
|
50
|
-
case "3":
|
|
51
|
-
case "4":
|
|
52
|
-
case "5":
|
|
53
|
-
case "6":
|
|
54
|
-
case "7":
|
|
55
|
-
case "8":
|
|
56
|
-
case "9":
|
|
57
|
-
notNumbersRegex.lastIndex = i + 1;
|
|
58
|
-
var nextNonDigit = notNumbersRegex.exec(source);
|
|
59
|
-
appendOptional((nextNonDigit ? nextNonDigit.index : source.length) - i);
|
|
60
|
-
break;
|
|
61
|
-
default:
|
|
62
|
-
appendOptional(2);
|
|
63
|
-
break;
|
|
64
|
-
}
|
|
65
|
-
break;
|
|
66
|
-
case "[": {
|
|
67
|
-
var depth = 1, escaped = false, j = i + 1;
|
|
68
|
-
while (depth) {
|
|
69
|
-
switch (source[j++]) {
|
|
70
|
-
case "\\":
|
|
71
|
-
escaped = !escaped;
|
|
72
|
-
continue;
|
|
73
|
-
case "[":
|
|
74
|
-
if (!escaped)
|
|
75
|
-
depth++;
|
|
76
|
-
break;
|
|
77
|
-
case "]":
|
|
78
|
-
if (!escaped)
|
|
79
|
-
depth--;
|
|
80
|
-
break;
|
|
81
|
-
}
|
|
82
|
-
escaped = false;
|
|
83
|
-
}
|
|
84
|
-
appendOptional(j - i);
|
|
85
|
-
break;
|
|
86
|
-
}
|
|
87
|
-
case "|":
|
|
88
|
-
case "^":
|
|
89
|
-
case "*":
|
|
90
|
-
case "+":
|
|
91
|
-
case "?":
|
|
92
|
-
case "$":
|
|
93
|
-
appendRaw(1);
|
|
94
|
-
break;
|
|
95
|
-
case "{": {
|
|
96
|
-
occurrencesRegex.lastIndex = i;
|
|
97
|
-
var regExpExecArray = occurrencesRegex.exec(source);
|
|
98
|
-
if (regExpExecArray) {
|
|
99
|
-
appendRaw(regExpExecArray[0].length);
|
|
100
|
-
}
|
|
101
|
-
else {
|
|
102
|
-
appendOptional(1);
|
|
103
|
-
}
|
|
104
|
-
break;
|
|
105
|
-
}
|
|
106
|
-
case "(":
|
|
107
|
-
if (source[i + 1] == "?") {
|
|
108
|
-
switch (source[i + 2]) {
|
|
109
|
-
case ":":
|
|
110
|
-
result += "(?:";
|
|
111
|
-
i += 3;
|
|
112
|
-
result += process() + "|$)";
|
|
113
|
-
break;
|
|
114
|
-
case "=":
|
|
115
|
-
result += "(?=";
|
|
116
|
-
i += 3;
|
|
117
|
-
result += process() + ")";
|
|
118
|
-
break;
|
|
119
|
-
case "-":
|
|
120
|
-
case "i":
|
|
121
|
-
case "s":
|
|
122
|
-
case "m":
|
|
123
|
-
var temp = i + 2, temp2 = source.indexOf(":", temp);
|
|
124
|
-
result += "(?" + source.substring(temp, temp2) + ":";
|
|
125
|
-
i = temp2 + 1;
|
|
126
|
-
result += process() + ")";
|
|
127
|
-
break;
|
|
128
|
-
case "!": {
|
|
129
|
-
var temp_1 = i;
|
|
130
|
-
i += 3;
|
|
131
|
-
process();
|
|
132
|
-
result += source.substring(temp_1, i);
|
|
133
|
-
break;
|
|
134
|
-
}
|
|
135
|
-
case "<":
|
|
136
|
-
switch (source[i + 3]) {
|
|
137
|
-
case "=":
|
|
138
|
-
case "!": {
|
|
139
|
-
var temp_2 = i;
|
|
140
|
-
i += 4;
|
|
141
|
-
process();
|
|
142
|
-
result += source.substring(temp_2, i);
|
|
143
|
-
break;
|
|
144
|
-
}
|
|
145
|
-
default:
|
|
146
|
-
appendRaw(source.indexOf(">", i) - i + 1);
|
|
147
|
-
result += process() + "|$)";
|
|
148
|
-
break;
|
|
149
|
-
}
|
|
150
|
-
break;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
else {
|
|
154
|
-
appendRaw(1);
|
|
155
|
-
result += process() + "|$)";
|
|
156
|
-
}
|
|
157
|
-
break;
|
|
158
|
-
case ")":
|
|
159
|
-
++i;
|
|
160
|
-
return result;
|
|
161
|
-
default:
|
|
162
|
-
appendOptional(1);
|
|
163
|
-
break;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
return result;
|
|
167
|
-
}
|
|
168
|
-
return new RegExp(process(), regex.flags);
|
|
169
|
-
};
|
|
170
|
-
/**
|
|
171
|
-
* Transforms a regular expression to support partial matching.
|
|
172
|
-
*
|
|
173
|
-
* This function wraps each atomic element of the regex pattern in a non-capturing group
|
|
174
|
-
* with an alternation to end-of-input (`$`), allowing the pattern to match prefixes
|
|
175
|
-
* of the original pattern. This enables validation of incomplete input strings.
|
|
176
|
-
*
|
|
177
|
-
* @param regex - The regular expression to transform for partial matching
|
|
178
|
-
* @returns A new RegExp that matches partial strings of the original pattern
|
|
179
|
-
*
|
|
180
|
-
* @example
|
|
181
|
-
* ```typescript
|
|
182
|
-
* const pattern = /hello world/;
|
|
183
|
-
* const partial = createPartialMatchRegex(pattern);
|
|
184
|
-
*
|
|
185
|
-
* partial.test('h'); // true - could match
|
|
186
|
-
* partial.test('hello'); // true - could match
|
|
187
|
-
* partial.test('hello world'); // true - full match
|
|
188
|
-
* partial.test('goodbye'); // false - cannot match
|
|
189
|
-
* ```
|
|
190
|
-
*
|
|
191
|
-
* @remarks
|
|
192
|
-
* - The transformed regex will always match an empty string at the end of input
|
|
193
|
-
* - Backreferences cannot be partially matched as they are atomic
|
|
194
|
-
* - Use with a start anchor (`^`) to prevent false positives from empty string matches
|
|
195
|
-
* - The `y` (sticky) flag may not behave as expected in partial matching scenarios
|
|
196
|
-
*
|
|
197
|
-
* @see {@link https://github.com/TomStrepsil/regex-partial-match#readme | Documentation}
|
|
198
|
-
*/
|
|
199
|
-
export default createPartialMatchRegex;
|