libphonenumber-js 1.9.39 → 1.9.43
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/CHANGELOG.md +10 -0
- package/README.md +14 -8
- package/build/AsYouType.test.js +25 -3
- package/build/AsYouType.test.js.map +1 -1
- package/build/AsYouTypeFormatter.PatternMatcher.js +435 -0
- package/build/AsYouTypeFormatter.PatternMatcher.js.map +1 -0
- package/build/AsYouTypeFormatter.PatternMatcher.test.js +447 -0
- package/build/AsYouTypeFormatter.PatternMatcher.test.js.map +1 -0
- package/build/AsYouTypeFormatter.js +57 -10
- package/build/AsYouTypeFormatter.js.map +1 -1
- package/build/PhoneNumber.test.js +9 -9
- package/build/PhoneNumber.test.js.map +1 -1
- package/build/findNumbers.test.js +23 -23
- package/build/findNumbers.test.js.map +1 -1
- package/build/getExampleNumber.test.js +4 -4
- package/build/getExampleNumber.test.js.map +1 -1
- package/build/getNumberType.test.js +3 -3
- package/build/getNumberType.test.js.map +1 -1
- package/build/helpers/checkNumberLength.test.js +3 -3
- package/build/helpers/checkNumberLength.test.js.map +1 -1
- package/build/helpers/extractCountryCallingCode.test.js +4 -4
- package/build/helpers/extractCountryCallingCode.test.js.map +1 -1
- package/build/parsePhoneNumber.test.js +2 -2
- package/build/parsePhoneNumber.test.js.map +1 -1
- package/bundle/libphonenumber-js.min.js +1 -1
- package/bundle/libphonenumber-js.min.js.map +1 -1
- package/bundle/libphonenumber-max.js +1 -1
- package/bundle/libphonenumber-max.js.map +1 -1
- package/bundle/libphonenumber-min.js +1 -1
- package/bundle/libphonenumber-min.js.map +1 -1
- package/bundle/libphonenumber-mobile.js +1 -1
- package/bundle/libphonenumber-mobile.js.map +1 -1
- package/es6/AsYouType.test.js +25 -3
- package/es6/AsYouType.test.js.map +1 -1
- package/es6/AsYouTypeFormatter.PatternMatcher.js +428 -0
- package/es6/AsYouTypeFormatter.PatternMatcher.js.map +1 -0
- package/es6/AsYouTypeFormatter.PatternMatcher.test.js +442 -0
- package/es6/AsYouTypeFormatter.PatternMatcher.test.js.map +1 -0
- package/es6/AsYouTypeFormatter.js +56 -10
- package/es6/AsYouTypeFormatter.js.map +1 -1
- package/es6/PhoneNumber.test.js +1 -1
- package/es6/PhoneNumber.test.js.map +1 -1
- package/es6/findNumbers.test.js +1 -1
- package/es6/findNumbers.test.js.map +1 -1
- package/es6/getExampleNumber.test.js +2 -2
- package/es6/getExampleNumber.test.js.map +1 -1
- package/es6/getNumberType.test.js +1 -1
- package/es6/getNumberType.test.js.map +1 -1
- package/es6/helpers/checkNumberLength.test.js +1 -1
- package/es6/helpers/checkNumberLength.test.js.map +1 -1
- package/es6/helpers/extractCountryCallingCode.test.js +1 -1
- package/es6/helpers/extractCountryCallingCode.test.js.map +1 -1
- package/es6/parsePhoneNumber.test.js +1 -1
- package/es6/parsePhoneNumber.test.js.map +1 -1
- package/exports/README.md +98 -0
- package/exports/examples.mobile.json/index.js +1 -0
- package/exports/metadata.max.json/index.js +1 -0
- package/exports/metadata.min.json/index.js +1 -0
- package/exports/metadata.mobile.json/index.js +1 -0
- package/exports/package.exports.json +55 -0
- package/max/index.commonjs.js +1 -1
- package/max/metadata.js +1 -1
- package/metadata.full.json +1 -1
- package/metadata.full.json.js +1 -1
- package/metadata.max.json +1 -0
- package/metadata.max.json.d.ts +4 -0
- package/metadata.max.json.js +4 -0
- package/metadata.min.json +1 -1
- package/metadata.min.json.js +1 -1
- package/metadata.mobile.json +1 -1
- package/metadata.mobile.json.js +1 -1
- package/package.json +4 -2
- package/source/AsYouType.test.js +26 -3
- package/source/AsYouTypeFormatter.PatternMatcher.js +343 -0
- package/source/AsYouTypeFormatter.PatternMatcher.test.js +596 -0
- package/source/AsYouTypeFormatter.js +58 -9
- package/source/PhoneNumber.test.js +1 -1
- package/source/findNumbers.test.js +1 -1
- package/source/getExampleNumber.test.js +2 -2
- package/source/getNumberType.test.js +1 -1
- package/source/helpers/checkNumberLength.test.js +1 -1
- package/source/helpers/extractCountryCallingCode.test.js +1 -1
- package/source/parsePhoneNumber.test.js +1 -1
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports["default"] = void 0;
|
|
7
|
+
|
|
8
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
9
|
+
|
|
10
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
11
|
+
|
|
12
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
|
13
|
+
|
|
14
|
+
var PatternMatcher =
|
|
15
|
+
/*#__PURE__*/
|
|
16
|
+
function () {
|
|
17
|
+
function PatternMatcher(pattern) {
|
|
18
|
+
_classCallCheck(this, PatternMatcher);
|
|
19
|
+
|
|
20
|
+
this.matchTree = new PatternParser().parse(pattern); // console.log(JSON.stringify(this.matchTree, null, 2))
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
_createClass(PatternMatcher, [{
|
|
24
|
+
key: "match",
|
|
25
|
+
value: function match(string) {
|
|
26
|
+
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
|
|
27
|
+
allowOverflow = _ref.allowOverflow;
|
|
28
|
+
|
|
29
|
+
if (!string) {
|
|
30
|
+
throw new Error('String is required');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
var result = _match(string.split(''), this.matchTree, true);
|
|
34
|
+
|
|
35
|
+
if (result && result.match) {
|
|
36
|
+
delete result.matchedChars;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (result && result.overflow) {
|
|
40
|
+
if (!allowOverflow) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
}]);
|
|
48
|
+
|
|
49
|
+
return PatternMatcher;
|
|
50
|
+
}();
|
|
51
|
+
|
|
52
|
+
exports["default"] = PatternMatcher;
|
|
53
|
+
|
|
54
|
+
function _match(characters, tree, last) {
|
|
55
|
+
if (typeof tree === 'string') {
|
|
56
|
+
if (last) {
|
|
57
|
+
if (characters.length > tree.length) {
|
|
58
|
+
return {
|
|
59
|
+
overflow: true
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
var characterString = characters.join('');
|
|
65
|
+
|
|
66
|
+
if (tree.indexOf(characterString) === 0) {
|
|
67
|
+
if (characters.length === tree.length) {
|
|
68
|
+
return {
|
|
69
|
+
match: true,
|
|
70
|
+
matchedChars: characters
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
partialMatch: true // matchedChars: characters
|
|
76
|
+
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (characterString.indexOf(tree) === 0) {
|
|
81
|
+
return {
|
|
82
|
+
match: true,
|
|
83
|
+
matchedChars: characters.slice(0, tree.length)
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (Array.isArray(tree)) {
|
|
91
|
+
var restCharacters = characters.slice();
|
|
92
|
+
var i = 0;
|
|
93
|
+
|
|
94
|
+
while (i < tree.length) {
|
|
95
|
+
var subtree = tree[i];
|
|
96
|
+
|
|
97
|
+
var result = _match(restCharacters, subtree, last && i === tree.length - 1);
|
|
98
|
+
|
|
99
|
+
if (!result || result.overflow) {
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (result.match) {
|
|
104
|
+
// Continue with the next subtree with the rest of the characters.
|
|
105
|
+
restCharacters = restCharacters.slice(result.matchedChars.length);
|
|
106
|
+
|
|
107
|
+
if (restCharacters.length === 0) {
|
|
108
|
+
if (i === tree.length - 1) {
|
|
109
|
+
return {
|
|
110
|
+
match: true,
|
|
111
|
+
matchedChars: characters
|
|
112
|
+
};
|
|
113
|
+
} else {
|
|
114
|
+
return {
|
|
115
|
+
partialMatch: true // matchedChars: characters
|
|
116
|
+
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
} else if (result.partialMatch) {
|
|
121
|
+
return {
|
|
122
|
+
partialMatch: true // matchedChars: characters
|
|
123
|
+
|
|
124
|
+
};
|
|
125
|
+
} else {
|
|
126
|
+
throw new Error("Unsupported match result:\n".concat(JSON.stringify(result, null, 2)));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
i++;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (last) {
|
|
133
|
+
return {
|
|
134
|
+
overflow: true
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
match: true,
|
|
140
|
+
matchedChars: characters.slice(0, characters.length - restCharacters.length)
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
switch (tree.op) {
|
|
145
|
+
case '|':
|
|
146
|
+
var partialMatch;
|
|
147
|
+
|
|
148
|
+
for (var _iterator = tree.args, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
|
|
149
|
+
var _ref2;
|
|
150
|
+
|
|
151
|
+
if (_isArray) {
|
|
152
|
+
if (_i >= _iterator.length) break;
|
|
153
|
+
_ref2 = _iterator[_i++];
|
|
154
|
+
} else {
|
|
155
|
+
_i = _iterator.next();
|
|
156
|
+
if (_i.done) break;
|
|
157
|
+
_ref2 = _i.value;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
var branch = _ref2;
|
|
161
|
+
|
|
162
|
+
var _result = _match(characters, branch, last);
|
|
163
|
+
|
|
164
|
+
if (_result) {
|
|
165
|
+
if (_result.overflow) {
|
|
166
|
+
return _result;
|
|
167
|
+
} else if (_result.match) {
|
|
168
|
+
return {
|
|
169
|
+
match: true,
|
|
170
|
+
matchedChars: _result.matchedChars
|
|
171
|
+
};
|
|
172
|
+
} else if (_result.partialMatch) {
|
|
173
|
+
partialMatch = true;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (partialMatch) {
|
|
179
|
+
return {
|
|
180
|
+
partialMatch: true // matchedChars: ...
|
|
181
|
+
|
|
182
|
+
};
|
|
183
|
+
} // Not even a partial match.
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
return;
|
|
187
|
+
|
|
188
|
+
case '[]':
|
|
189
|
+
for (var _iterator2 = tree.args, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
|
|
190
|
+
var _ref3;
|
|
191
|
+
|
|
192
|
+
if (_isArray2) {
|
|
193
|
+
if (_i2 >= _iterator2.length) break;
|
|
194
|
+
_ref3 = _iterator2[_i2++];
|
|
195
|
+
} else {
|
|
196
|
+
_i2 = _iterator2.next();
|
|
197
|
+
if (_i2.done) break;
|
|
198
|
+
_ref3 = _i2.value;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
var _char = _ref3;
|
|
202
|
+
|
|
203
|
+
if (characters[0] === _char) {
|
|
204
|
+
if (characters.length === 1) {
|
|
205
|
+
return {
|
|
206
|
+
match: true,
|
|
207
|
+
matchedChars: characters
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (last) {
|
|
212
|
+
return {
|
|
213
|
+
overflow: true
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return {
|
|
218
|
+
match: true,
|
|
219
|
+
matchedChars: [_char]
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
} // No character matches.
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
return;
|
|
226
|
+
|
|
227
|
+
default:
|
|
228
|
+
throw new Error("Unsupported instruction tree: ".concat(tree));
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
var OPERATOR = new RegExp( // any of:
|
|
233
|
+
'(' + // or operator
|
|
234
|
+
'\\|' + // or
|
|
235
|
+
'|' + // or group start
|
|
236
|
+
'\\(\\?\\:' + // or
|
|
237
|
+
'|' + // or group end
|
|
238
|
+
'\\)' + // or
|
|
239
|
+
'|' + // one-of set start
|
|
240
|
+
'\\[' + // or
|
|
241
|
+
'|' + // one-of set end
|
|
242
|
+
'\\]' + ')');
|
|
243
|
+
|
|
244
|
+
var PatternParser =
|
|
245
|
+
/*#__PURE__*/
|
|
246
|
+
function () {
|
|
247
|
+
function PatternParser() {
|
|
248
|
+
_classCallCheck(this, PatternParser);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
_createClass(PatternParser, [{
|
|
252
|
+
key: "parse",
|
|
253
|
+
value: function parse(pattern) {
|
|
254
|
+
this.context = [{
|
|
255
|
+
or: true,
|
|
256
|
+
instructions: []
|
|
257
|
+
}];
|
|
258
|
+
this.parsePattern(pattern);
|
|
259
|
+
|
|
260
|
+
if (this.context.length !== 1) {
|
|
261
|
+
throw new Error('Non-finalized contexts left when pattern parse ended');
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
var _this$context$ = this.context[0],
|
|
265
|
+
branches = _this$context$.branches,
|
|
266
|
+
instructions = _this$context$.instructions;
|
|
267
|
+
|
|
268
|
+
if (branches) {
|
|
269
|
+
return [{
|
|
270
|
+
op: '|',
|
|
271
|
+
args: branches.concat([instructions])
|
|
272
|
+
}];
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (instructions.length === 0) {
|
|
276
|
+
throw new Error('Pattern is required');
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
return instructions;
|
|
280
|
+
}
|
|
281
|
+
}, {
|
|
282
|
+
key: "startContext",
|
|
283
|
+
value: function startContext(context) {
|
|
284
|
+
this.context.push(context);
|
|
285
|
+
}
|
|
286
|
+
}, {
|
|
287
|
+
key: "endContext",
|
|
288
|
+
value: function endContext() {
|
|
289
|
+
this.context.pop();
|
|
290
|
+
}
|
|
291
|
+
}, {
|
|
292
|
+
key: "getContext",
|
|
293
|
+
value: function getContext() {
|
|
294
|
+
return this.context[this.context.length - 1];
|
|
295
|
+
}
|
|
296
|
+
}, {
|
|
297
|
+
key: "parsePattern",
|
|
298
|
+
value: function parsePattern(pattern) {
|
|
299
|
+
if (!pattern) {
|
|
300
|
+
throw new Error('Empty pattern passed');
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
var match = pattern.match(OPERATOR);
|
|
304
|
+
|
|
305
|
+
if (!match) {
|
|
306
|
+
this.getContext().instructions = this.getContext().instructions.concat(pattern.split(''));
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
var operator = match[1];
|
|
311
|
+
var before = pattern.slice(0, match.index);
|
|
312
|
+
var rightPart = pattern.slice(match.index + operator.length);
|
|
313
|
+
|
|
314
|
+
switch (operator) {
|
|
315
|
+
case '(?:':
|
|
316
|
+
if (before) {
|
|
317
|
+
this.parsePattern(before);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
this.startContext({
|
|
321
|
+
or: true,
|
|
322
|
+
instructions: [],
|
|
323
|
+
branches: []
|
|
324
|
+
});
|
|
325
|
+
break;
|
|
326
|
+
|
|
327
|
+
case ')':
|
|
328
|
+
if (!this.getContext().or) {
|
|
329
|
+
throw new Error('")" operator must be preceded by "(?:" operator');
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
if (before) {
|
|
333
|
+
this.parsePattern(before);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (this.getContext().instructions.length === 0) {
|
|
337
|
+
throw new Error('No instructions found after "|" operator in an "or" group');
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
var _this$getContext = this.getContext(),
|
|
341
|
+
branches = _this$getContext.branches;
|
|
342
|
+
|
|
343
|
+
branches.push(this.getContext().instructions);
|
|
344
|
+
this.endContext();
|
|
345
|
+
this.getContext().instructions.push({
|
|
346
|
+
op: '|',
|
|
347
|
+
args: branches
|
|
348
|
+
});
|
|
349
|
+
break;
|
|
350
|
+
|
|
351
|
+
case '|':
|
|
352
|
+
if (!this.getContext().or) {
|
|
353
|
+
throw new Error('"|" operator can only be used inside "or" groups');
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
if (before) {
|
|
357
|
+
this.parsePattern(before);
|
|
358
|
+
} // The top-level is an implicit "or" group, if required.
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
if (!this.getContext().branches) {
|
|
362
|
+
if (this.context.length === 1) {
|
|
363
|
+
this.getContext().branches = [];
|
|
364
|
+
} else {
|
|
365
|
+
throw new Error('"branches" not found in an "or" group context');
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
this.getContext().branches.push(this.getContext().instructions);
|
|
370
|
+
this.getContext().instructions = [];
|
|
371
|
+
break;
|
|
372
|
+
|
|
373
|
+
case '[':
|
|
374
|
+
if (before) {
|
|
375
|
+
this.parsePattern(before);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
this.startContext({
|
|
379
|
+
oneOfSet: true
|
|
380
|
+
});
|
|
381
|
+
break;
|
|
382
|
+
|
|
383
|
+
case ']':
|
|
384
|
+
if (!this.getContext().oneOfSet) {
|
|
385
|
+
throw new Error('"]" operator must be preceded by "[" operator');
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
this.endContext();
|
|
389
|
+
this.getContext().instructions.push({
|
|
390
|
+
op: '[]',
|
|
391
|
+
args: parseOneOfSet(before)
|
|
392
|
+
});
|
|
393
|
+
break;
|
|
394
|
+
|
|
395
|
+
default:
|
|
396
|
+
throw new Error("Unknown operator: ".concat(operator));
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
if (rightPart) {
|
|
400
|
+
this.parsePattern(rightPart);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
}]);
|
|
404
|
+
|
|
405
|
+
return PatternParser;
|
|
406
|
+
}();
|
|
407
|
+
|
|
408
|
+
function parseOneOfSet(pattern) {
|
|
409
|
+
var values = [];
|
|
410
|
+
var i = 0;
|
|
411
|
+
|
|
412
|
+
while (i < pattern.length) {
|
|
413
|
+
if (pattern[i] === '-') {
|
|
414
|
+
if (i === 0 || i === pattern.length - 1) {
|
|
415
|
+
throw new Error("Couldn't parse a one-of set pattern: ".concat(pattern));
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
var prevValue = pattern[i - 1].charCodeAt(0) + 1;
|
|
419
|
+
var nextValue = pattern[i + 1].charCodeAt(0) - 1;
|
|
420
|
+
var value = prevValue;
|
|
421
|
+
|
|
422
|
+
while (value <= nextValue) {
|
|
423
|
+
values.push(String.fromCharCode(value));
|
|
424
|
+
value++;
|
|
425
|
+
}
|
|
426
|
+
} else {
|
|
427
|
+
values.push(pattern[i]);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
i++;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
return values;
|
|
434
|
+
}
|
|
435
|
+
//# sourceMappingURL=AsYouTypeFormatter.PatternMatcher.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../source/AsYouTypeFormatter.PatternMatcher.js"],"names":["PatternMatcher","pattern","matchTree","PatternParser","parse","string","allowOverflow","Error","result","match","split","matchedChars","overflow","characters","tree","last","length","characterString","join","indexOf","partialMatch","slice","Array","isArray","restCharacters","i","subtree","JSON","stringify","op","args","branch","char","OPERATOR","RegExp","context","or","instructions","parsePattern","branches","concat","push","pop","getContext","operator","before","index","rightPart","startContext","endContext","oneOfSet","parseOneOfSet","values","prevValue","charCodeAt","nextValue","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AACwB,CAAC,GAAG,CAAL,CAAP,CAAe6B,UAAf,CAA0B,CAA1B,IAA+B,CAAjD;AACA,UAAIE,KAAK,GAAGH,SAAZ;;AACA,aAAOG,KAAK,IAAID,SAAhB,EAA2B;AAC1BH,QAAAA,MAAM,CAACX,IAAP,CAAYgB,MAAM,CAACC,YAAP,CAAoBF,KAApB,CAAZ;AACAA,QAAAA,KAAK;AACL;AACD,KAXD,MAWO;AACNJ,MAAAA,MAAM,CAACX,IAAP,CAAYxC,OAAO,CAACwB,CAAD,CAAnB;AACA;;AACDA,IAAAA,CAAC;AACD;;AACD,SAAO2B,MAAP;AACA","sourcesContent":["export default class PatternMatcher {\r\n\tconstructor(pattern) {\r\n\t\tthis.matchTree = new PatternParser().parse(pattern)\r\n\t\t// console.log(JSON.stringify(this.matchTree, null, 2))\r\n\t}\r\n\r\n\tmatch(string, { allowOverflow } = {}) {\r\n\t\tif (!string) {\r\n\t\t\tthrow new Error('String is required')\r\n\t\t}\r\n\t\tconst result = match(string.split(''), this.matchTree, true)\r\n\t\tif (result && result.match) {\r\n\t\t\tdelete result.matchedChars\r\n\t\t}\r\n\t\tif (result && result.overflow) {\r\n\t\t\tif (!allowOverflow) {\r\n\t\t\t\treturn\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn result\r\n\t}\r\n}\r\n\r\nfunction match(characters, tree, last) {\r\n\tif (typeof tree === 'string') {\r\n\t\tif (last) {\r\n\t\t\tif (characters.length > tree.length) {\r\n\t\t\t\treturn {\r\n\t\t\t\t\toverflow: true\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\tconst characterString = characters.join('')\r\n\t\tif (tree.indexOf(characterString) === 0) {\r\n\t\t\tif (characters.length === tree.length) {\r\n\t\t\t\treturn {\r\n\t\t\t\t\tmatch: true,\r\n\t\t\t\t\tmatchedChars: characters\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\treturn {\r\n\t\t\t\tpartialMatch: true,\r\n\t\t\t\t// matchedChars: characters\r\n\t\t\t}\r\n\t\t}\r\n\t\tif (characterString.indexOf(tree) === 0) {\r\n\t\t\treturn {\r\n\t\t\t\tmatch: true,\r\n\t\t\t\tmatchedChars: characters.slice(0, tree.length)\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn\r\n\t}\r\n\r\n\tif (Array.isArray(tree)) {\r\n\t\tlet restCharacters = characters.slice()\r\n\t\tlet i = 0\r\n\t\twhile (i < tree.length) {\r\n\t\t\tconst subtree = tree[i]\r\n\t\t\tconst result = match(restCharacters, subtree, last && (i === tree.length - 1))\r\n\t\t\tif (!result || result.overflow) {\r\n\t\t\t\treturn result\r\n\t\t\t}\r\n\t\t\tif (result.match) {\r\n\t\t\t\t// Continue with the next subtree with the rest of the characters.\r\n\t\t\t\trestCharacters = restCharacters.slice(result.matchedChars.length)\r\n\t\t\t\tif (restCharacters.length === 0) {\r\n\t\t\t\t\tif (i === tree.length - 1) {\r\n\t\t\t\t\t\treturn {\r\n\t\t\t\t\t\t\tmatch: true,\r\n\t\t\t\t\t\t\tmatchedChars: characters\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t} else {\r\n\t\t\t\t\t\treturn {\r\n\t\t\t\t\t\t\tpartialMatch: true,\r\n\t\t\t\t\t\t\t// matchedChars: characters\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t} else if (result.partialMatch) {\r\n\t\t\t\treturn {\r\n\t\t\t\t\tpartialMatch: true,\r\n\t\t\t\t\t// matchedChars: characters\r\n\t\t\t\t}\r\n\t\t\t} else {\r\n\t\t\t\tthrow new Error(`Unsupported match result:\\n${JSON.stringify(result, null, 2)}`)\r\n\t\t\t}\r\n\t\t\ti++\r\n\t\t}\r\n\t\tif (last) {\r\n\t\t\treturn {\r\n\t\t\t\toverflow: true\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn {\r\n\t\t\tmatch: true,\r\n\t\t\tmatchedChars: characters.slice(0, characters.length - restCharacters.length)\r\n\t\t}\r\n\t}\r\n\r\n\tswitch (tree.op) {\r\n\t\tcase '|':\r\n\t\t\tlet partialMatch\r\n\t\t\tfor (const branch of tree.args) {\r\n\t\t\t\tconst result = match(characters, branch, last)\r\n\t\t\t\tif (result) {\r\n\t\t\t\t\tif (result.overflow) {\r\n\t\t\t\t\t\treturn result\r\n\t\t\t\t\t} else if (result.match) {\r\n\t\t\t\t\t\treturn {\r\n\t\t\t\t\t\t\tmatch: true,\r\n\t\t\t\t\t\t\tmatchedChars: result.matchedChars\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t} else if (result.partialMatch) {\r\n\t\t\t\t\t\tpartialMatch = true\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tif (partialMatch) {\r\n\t\t\t\treturn {\r\n\t\t\t\t\tpartialMatch: true,\r\n\t\t\t\t\t// matchedChars: ...\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t// Not even a partial match.\r\n\t\t\treturn\r\n\r\n\t\tcase '[]':\r\n\t\t\tfor (const char of tree.args) {\r\n\t\t\t\tif (characters[0] === char) {\r\n\t\t\t\t\tif (characters.length === 1) {\r\n\t\t\t\t\t\treturn {\r\n\t\t\t\t\t\t\tmatch: true,\r\n\t\t\t\t\t\t\tmatchedChars: characters\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\t\t\tif (last) {\r\n\t\t\t\t\t\treturn {\r\n\t\t\t\t\t\t\toverflow: true\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\t\t\treturn {\r\n\t\t\t\t\t\tmatch: true,\r\n\t\t\t\t\t\tmatchedChars: [char]\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t// No character matches.\r\n\t\t\treturn\r\n\r\n\t\tdefault:\r\n\t\t\tthrow new Error(`Unsupported instruction tree: ${tree}`)\r\n\t}\r\n}\r\n\r\nconst OPERATOR = new RegExp(\r\n\t// any of:\r\n\t'(' +\r\n\t\t// or operator\r\n\t\t'\\\\|' +\r\n\t\t// or\r\n\t\t'|' +\r\n\t\t// or group start\r\n\t\t'\\\\(\\\\?\\\\:' +\r\n\t\t// or\r\n\t\t'|' +\r\n\t\t// or group end\r\n\t\t'\\\\)' +\r\n\t\t// or\r\n\t\t'|' +\r\n\t\t// one-of set start\r\n\t\t'\\\\[' +\r\n\t\t// or\r\n\t\t'|' +\r\n\t\t// one-of set end\r\n\t\t'\\\\]' +\r\n\t')'\r\n)\r\n\r\nclass PatternParser {\r\n\tparse(pattern) {\r\n\t\tthis.context = [{\r\n\t\t\tor: true,\r\n\t\t\tinstructions: []\r\n\t\t}]\r\n\r\n\t\tthis.parsePattern(pattern)\r\n\r\n\t\tif (this.context.length !== 1) {\r\n\t\t\tthrow new Error('Non-finalized contexts left when pattern parse ended')\r\n\t\t}\r\n\r\n\t\tconst { branches, instructions } = this.context[0]\r\n\r\n\t\tif (branches) {\r\n\t\t\treturn [{\r\n\t\t\t\top: '|',\r\n\t\t\t\targs: branches.concat([instructions])\r\n\t\t\t}]\r\n\t\t}\r\n\r\n\t\tif (instructions.length === 0) {\r\n\t\t\tthrow new Error('Pattern is required')\r\n\t\t}\r\n\r\n\t\treturn instructions\r\n\t}\r\n\r\n\tstartContext(context) {\r\n\t\tthis.context.push(context)\r\n\t}\r\n\r\n\tendContext() {\r\n\t\tthis.context.pop()\r\n\t}\r\n\r\n\tgetContext() {\r\n\t\treturn this.context[this.context.length - 1]\r\n\t}\r\n\r\n\tparsePattern(pattern) {\r\n\t\tif (!pattern) {\r\n\t\t\tthrow new Error('Empty pattern passed')\r\n\t\t}\r\n\r\n\t\tconst match = pattern.match(OPERATOR)\r\n\t\tif (!match) {\r\n\t\t\tthis.getContext().instructions = this.getContext().instructions.concat(\r\n\t\t\t\tpattern.split('')\r\n\t\t\t)\r\n\t\t\treturn\r\n\t\t}\r\n\r\n\t\tconst operator = match[1]\r\n\t\tconst before = pattern.slice(0, match.index)\r\n\t\tconst rightPart = pattern.slice(match.index + operator.length)\r\n\r\n\t\tswitch (operator) {\r\n\t\t\tcase '(?:':\r\n\t\t\t\tif (before) {\r\n\t\t\t\t\tthis.parsePattern(before)\r\n\t\t\t\t}\r\n\t\t\t\tthis.startContext({\r\n\t\t\t\t\tor: true,\r\n\t\t\t\t\tinstructions: [],\r\n\t\t\t\t\tbranches: []\r\n\t\t\t\t})\r\n\t\t\t\tbreak\r\n\r\n\t\t\tcase ')':\r\n\t\t\t\tif (!this.getContext().or) {\r\n\t\t\t\t\tthrow new Error('\")\" operator must be preceded by \"(?:\" operator')\r\n\t\t\t\t}\r\n\t\t\t\tif (before) {\r\n\t\t\t\t\tthis.parsePattern(before)\r\n\t\t\t\t}\r\n\t\t\t\tif (this.getContext().instructions.length === 0) {\r\n\t\t\t\t\tthrow new Error('No instructions found after \"|\" operator in an \"or\" group')\r\n\t\t\t\t}\r\n\t\t\t\tconst { branches } = this.getContext()\r\n\t\t\t\tbranches.push(\r\n\t\t\t\t\tthis.getContext().instructions\r\n\t\t\t\t)\r\n\t\t\t\tthis.endContext()\r\n\t\t\t\tthis.getContext().instructions.push({\r\n\t\t\t\t\top: '|',\r\n\t\t\t\t\targs: branches\r\n\t\t\t\t})\r\n\t\t\t\tbreak\r\n\r\n\t\t\tcase '|':\r\n\t\t\t\tif (!this.getContext().or) {\r\n\t\t\t\t\tthrow new Error('\"|\" operator can only be used inside \"or\" groups')\r\n\t\t\t\t}\r\n\t\t\t\tif (before) {\r\n\t\t\t\t\tthis.parsePattern(before)\r\n\t\t\t\t}\r\n\t\t\t\t// The top-level is an implicit \"or\" group, if required.\r\n\t\t\t\tif (!this.getContext().branches) {\r\n\t\t\t\t\tif (this.context.length === 1) {\r\n\t\t\t\t\t\tthis.getContext().branches = []\r\n\t\t\t\t\t} else {\r\n\t\t\t\t\t\tthrow new Error('\"branches\" not found in an \"or\" group context')\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t\tthis.getContext().branches.push(\r\n\t\t\t\t\tthis.getContext().instructions\r\n\t\t\t\t)\r\n\t\t\t\tthis.getContext().instructions = []\r\n\t\t\t\tbreak\r\n\r\n\t\t\tcase '[':\r\n\t\t\t\tif (before) {\r\n\t\t\t\t\tthis.parsePattern(before)\r\n\t\t\t\t}\r\n\t\t\t\tthis.startContext({\r\n\t\t\t\t\toneOfSet: true\r\n\t\t\t\t})\r\n\t\t\t\tbreak\r\n\r\n\t\t\tcase ']':\r\n\t\t\t\tif (!this.getContext().oneOfSet) {\r\n\t\t\t\t\tthrow new Error('\"]\" operator must be preceded by \"[\" operator')\r\n\t\t\t\t}\r\n\t\t\t\tthis.endContext()\r\n\t\t\t\tthis.getContext().instructions.push({\r\n\t\t\t\t\top: '[]',\r\n\t\t\t\t\targs: parseOneOfSet(before)\r\n\t\t\t\t})\r\n\t\t\t\tbreak\r\n\r\n\t\t\tdefault:\r\n\t\t\t\tthrow new Error(`Unknown operator: ${operator}`)\r\n\t\t}\r\n\r\n\t\tif (rightPart) {\r\n\t\t\tthis.parsePattern(rightPart)\r\n\t\t}\r\n\t}\r\n}\r\n\r\nfunction parseOneOfSet(pattern) {\r\n\tconst values = []\r\n\tlet i = 0\r\n\twhile (i < pattern.length) {\r\n\t\tif (pattern[i] === '-') {\r\n\t\t\tif (i === 0 || i === pattern.length - 1) {\r\n\t\t\t\tthrow new Error(`Couldn't parse a one-of set pattern: ${pattern}`)\r\n\t\t\t}\r\n\t\t\tconst prevValue = pattern[i - 1].charCodeAt(0) + 1\r\n\t\t\tconst nextValue = pattern[i + 1].charCodeAt(0) - 1\r\n\t\t\tlet value = prevValue\r\n\t\t\twhile (value <= nextValue) {\r\n\t\t\t\tvalues.push(String.fromCharCode(value))\r\n\t\t\t\tvalue++\r\n\t\t\t}\r\n\t\t} else {\r\n\t\t\tvalues.push(pattern[i])\r\n\t\t}\r\n\t\ti++\r\n\t}\r\n\treturn values\r\n}"],"file":"AsYouTypeFormatter.PatternMatcher.js"}
|