libphonenumber-js 1.10.4 → 1.10.7
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 +35 -9
- package/build/AsYouType.test.js +26 -8
- package/build/AsYouType.test.js.map +1 -1
- package/build/AsYouTypeFormatter.PatternMatcher.js +36 -224
- package/build/AsYouTypeFormatter.PatternMatcher.js.map +1 -1
- package/build/AsYouTypeFormatter.PatternMatcher.test.js +9 -0
- package/build/AsYouTypeFormatter.PatternMatcher.test.js.map +1 -1
- package/build/AsYouTypeFormatter.PatternParser.js +241 -0
- package/build/AsYouTypeFormatter.PatternParser.js.map +1 -0
- package/build/AsYouTypeFormatter.PatternParser.test.js +49 -0
- package/build/AsYouTypeFormatter.PatternParser.test.js.map +1 -0
- package/build/AsYouTypeFormatter.js.map +1 -1
- package/build/package.json +5 -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/core/package.json +1 -2
- package/es6/AsYouType.test.js +26 -8
- package/es6/AsYouType.test.js.map +1 -1
- package/es6/AsYouTypeFormatter.PatternMatcher.js +33 -223
- package/es6/AsYouTypeFormatter.PatternMatcher.js.map +1 -1
- package/es6/AsYouTypeFormatter.PatternMatcher.test.js +9 -0
- package/es6/AsYouTypeFormatter.PatternMatcher.test.js.map +1 -1
- package/es6/AsYouTypeFormatter.PatternParser.js +234 -0
- package/es6/AsYouTypeFormatter.PatternParser.js.map +1 -0
- package/es6/AsYouTypeFormatter.PatternParser.test.js +44 -0
- package/es6/AsYouTypeFormatter.PatternParser.test.js.map +1 -0
- package/es6/AsYouTypeFormatter.js.map +1 -1
- package/index.cjs +9 -9
- package/index.cjs.js +9 -9
- package/index.es6.exports/PhoneNumberSearch.js +3 -1
- package/index.es6.exports/findPhoneNumbers.js +2 -2
- package/index.es6.exports/format.js +2 -2
- package/index.es6.exports/getNumberType.js +2 -2
- package/index.es6.exports/isPossibleNumber.js +2 -2
- package/index.es6.exports/isValidNumber.js +2 -2
- package/index.es6.exports/isValidNumberForRegion.js +2 -2
- package/index.es6.exports/parse.js +2 -2
- package/index.es6.exports/searchPhoneNumbers.js +2 -2
- package/max/exports/AsYouType.js +4 -1
- package/max/exports/Metadata.js +4 -1
- package/max/exports/PhoneNumberMatcher.js +4 -1
- package/max/exports/findNumbers.js +2 -2
- package/max/exports/findPhoneNumbersInText.js +2 -2
- package/max/exports/formatIncompletePhoneNumber.js +2 -2
- package/max/exports/getCountries.js +2 -2
- package/max/exports/getCountryCallingCode.js +2 -2
- package/max/exports/getExampleNumber.js +2 -2
- package/max/exports/getExtPrefix.js +2 -2
- package/max/exports/isPossiblePhoneNumber.js +2 -2
- package/max/exports/isSupportedCountry.js +2 -2
- package/max/exports/isValidPhoneNumber.js +2 -2
- package/max/exports/parsePhoneNumberFromString.js +2 -2
- package/max/exports/parsePhoneNumberWithError.js +2 -2
- package/max/exports/searchNumbers.js +2 -2
- package/max/exports/searchPhoneNumbersInText.js +2 -2
- package/max/exports/validatePhoneNumberLength.js +2 -2
- package/max/exports/withMetadataArgument.js +9 -0
- package/max/metadata/package.json +16 -0
- package/max/package.json +1 -2
- package/metadata.full.json +1 -1
- package/metadata.full.json.js +1 -1
- package/metadata.max.json +1 -1
- package/metadata.max.json.js +1 -1
- 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/min/exports/AsYouType.js +4 -1
- package/min/exports/Metadata.js +4 -1
- package/min/exports/PhoneNumberMatcher.js +4 -1
- package/min/exports/findNumbers.js +2 -2
- package/min/exports/findPhoneNumbersInText.js +2 -2
- package/min/exports/formatIncompletePhoneNumber.js +2 -2
- package/min/exports/getCountries.js +2 -2
- package/min/exports/getCountryCallingCode.js +2 -2
- package/min/exports/getExampleNumber.js +2 -2
- package/min/exports/getExtPrefix.js +2 -2
- package/min/exports/isPossiblePhoneNumber.js +2 -2
- package/min/exports/isSupportedCountry.js +2 -2
- package/min/exports/isValidPhoneNumber.js +2 -2
- package/min/exports/parsePhoneNumberFromString.js +2 -2
- package/min/exports/parsePhoneNumberWithError.js +2 -2
- package/min/exports/searchNumbers.js +2 -2
- package/min/exports/searchPhoneNumbersInText.js +2 -2
- package/min/exports/validatePhoneNumberLength.js +2 -2
- package/min/exports/withMetadataArgument.js +9 -0
- package/min/metadata/package.json +16 -0
- package/min/package.json +1 -2
- package/mobile/examples/package.json +16 -0
- package/mobile/exports/AsYouType.js +4 -1
- package/mobile/exports/Metadata.js +4 -1
- package/mobile/exports/PhoneNumberMatcher.js +4 -1
- package/mobile/exports/findNumbers.js +2 -2
- package/mobile/exports/findPhoneNumbersInText.js +2 -2
- package/mobile/exports/formatIncompletePhoneNumber.js +2 -2
- package/mobile/exports/getCountries.js +2 -2
- package/mobile/exports/getCountryCallingCode.js +2 -2
- package/mobile/exports/getExampleNumber.js +2 -2
- package/mobile/exports/getExtPrefix.js +2 -2
- package/mobile/exports/isPossiblePhoneNumber.js +2 -2
- package/mobile/exports/isSupportedCountry.js +2 -2
- package/mobile/exports/isValidPhoneNumber.js +2 -2
- package/mobile/exports/parsePhoneNumberFromString.js +2 -2
- package/mobile/exports/parsePhoneNumberWithError.js +2 -2
- package/mobile/exports/searchNumbers.js +2 -2
- package/mobile/exports/searchPhoneNumbersInText.js +2 -2
- package/mobile/exports/validatePhoneNumberLength.js +2 -2
- package/mobile/exports/withMetadataArgument.js +9 -0
- package/mobile/metadata/package.json +16 -0
- package/mobile/package.json +1 -2
- package/package.json +22 -2
- package/runnable/create-commonjs-package-json.js +1 -1
- package/source/AsYouType.test.js +25 -6
- package/source/AsYouTypeFormatter.PatternMatcher.d.ts +66 -0
- package/source/AsYouTypeFormatter.PatternMatcher.js +30 -206
- package/source/AsYouTypeFormatter.PatternMatcher.test.js +8 -0
- package/source/AsYouTypeFormatter.PatternParser.d.ts +58 -0
- package/source/AsYouTypeFormatter.PatternParser.js +214 -0
- package/source/AsYouTypeFormatter.PatternParser.test.js +67 -0
- package/source/AsYouTypeFormatter.js +2 -0
- package/max/metadata.js +0 -11
- package/min/metadata.js +0 -11
- package/mobile/metadata.js +0 -11
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import {
|
|
2
|
+
character,
|
|
3
|
+
MatchTree
|
|
4
|
+
} from './AsYouTypeFormatter.PatternParser'
|
|
5
|
+
|
|
6
|
+
interface MatchOptions {
|
|
7
|
+
allowOverflow?: boolean;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// An "overflow" match is when the string matches the pattern
|
|
11
|
+
// and there're still some characters left in it.
|
|
12
|
+
//
|
|
13
|
+
// For example, "12345" matches "12[0-5]|78" pattern with an overflow
|
|
14
|
+
// because "123" matches the "12[0-5]" variant of the pattern
|
|
15
|
+
// and there're still "45" characters left.
|
|
16
|
+
//
|
|
17
|
+
// This type of match is only returned when `allowOverflow` option is `true`.
|
|
18
|
+
// By default, `allowOverflow` is `false` and `undefined` ("no match" result)
|
|
19
|
+
// is returned in case of an "overflow" match.
|
|
20
|
+
//
|
|
21
|
+
interface MatchResultOverflow {
|
|
22
|
+
overflow: true;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// When there's a ("full") match, returns a match result.
|
|
26
|
+
//
|
|
27
|
+
// A ("full") match is when the string matches the entire pattern.
|
|
28
|
+
//
|
|
29
|
+
// For example, "123" fully matches "12[0-5]|78" pattern.
|
|
30
|
+
//
|
|
31
|
+
interface MatchResultFullMatch {
|
|
32
|
+
match: true;
|
|
33
|
+
matchedChars: character[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// When there's a "partial" match, returns a "partial" match result.
|
|
37
|
+
//
|
|
38
|
+
// A "partial" match is when the string is not long enough
|
|
39
|
+
// to match the whole matching tree.
|
|
40
|
+
//
|
|
41
|
+
// For example, "123" is a partial match for "12[0-5]4|78" pattern,
|
|
42
|
+
// because "123" matched the "12[0-5]" part and the "4" part of the pattern
|
|
43
|
+
// is left uninvolved.
|
|
44
|
+
//
|
|
45
|
+
interface MatchResultPartialMatch {
|
|
46
|
+
partialMatch: true;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// When there's no match, returns `undefined`.
|
|
50
|
+
//
|
|
51
|
+
// For example, "123" doesn't match "456|789" pattern.
|
|
52
|
+
//
|
|
53
|
+
type MatchResultNoMatch = undefined;
|
|
54
|
+
|
|
55
|
+
type MatchResult =
|
|
56
|
+
MatchResultOverflow |
|
|
57
|
+
MatchResultFullMatch |
|
|
58
|
+
MatchResultPartialMatch |
|
|
59
|
+
MatchResultNoMatch;
|
|
60
|
+
|
|
61
|
+
export default class PatternMatcher {
|
|
62
|
+
constructor(pattern: string);
|
|
63
|
+
match(string: string, options?: MatchOptions): MatchResult;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function match(characters: character[], tree: MatchTree, last?: boolean);
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import PatternParser from './AsYouTypeFormatter.PatternParser.js'
|
|
2
|
+
|
|
1
3
|
export default class PatternMatcher {
|
|
2
4
|
constructor(pattern) {
|
|
3
5
|
this.matchTree = new PatternParser().parse(pattern)
|
|
@@ -20,16 +22,21 @@ export default class PatternMatcher {
|
|
|
20
22
|
}
|
|
21
23
|
}
|
|
22
24
|
|
|
25
|
+
/**
|
|
26
|
+
* Matches `characters` against a pattern compiled into a `tree`.
|
|
27
|
+
* @param {string[]} characters
|
|
28
|
+
* @param {Tree} tree — A pattern compiled into a `tree`. See the `*.d.ts` file for the description of the `tree` structure.
|
|
29
|
+
* @param {boolean} last — Whether it's the last (rightmost) subtree on its level of the match tree.
|
|
30
|
+
* @return {object} See the `*.d.ts` file for the description of the result object.
|
|
31
|
+
*/
|
|
23
32
|
function match(characters, tree, last) {
|
|
33
|
+
// If `tree` is a string, then `tree` is a single character.
|
|
34
|
+
// That's because when a pattern is parsed, multi-character-string parts
|
|
35
|
+
// of a pattern are compiled into arrays of single characters.
|
|
36
|
+
// I still wrote this piece of code for a "general" hypothetical case
|
|
37
|
+
// when `tree` could be a string of several characters, even though
|
|
38
|
+
// such case is not possible with the current implementation.
|
|
24
39
|
if (typeof tree === 'string') {
|
|
25
|
-
if (last) {
|
|
26
|
-
// `tree` is always a single character.
|
|
27
|
-
if (characters.length > tree.length) {
|
|
28
|
-
return {
|
|
29
|
-
overflow: true
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
40
|
const characterString = characters.join('')
|
|
34
41
|
if (tree.indexOf(characterString) === 0) {
|
|
35
42
|
// `tree` is always a single character.
|
|
@@ -52,6 +59,21 @@ function match(characters, tree, last) {
|
|
|
52
59
|
}
|
|
53
60
|
}
|
|
54
61
|
if (characterString.indexOf(tree) === 0) {
|
|
62
|
+
if (last) {
|
|
63
|
+
// The `else` path is not possible because `tree` is always a single character.
|
|
64
|
+
// The `else` case for `characters.length > tree.length` would be
|
|
65
|
+
// `characters.length <= tree.length` which means `characters.length <= 1`.
|
|
66
|
+
// `characters` array can't be empty, so that means `characters === [tree]`,
|
|
67
|
+
// which would also mean `tree.indexOf(characterString) === 0` and that'd mean
|
|
68
|
+
// that the `if (tree.indexOf(characterString) === 0)` condition before this
|
|
69
|
+
// `if` condition would be entered, and returned from there, not reaching this code.
|
|
70
|
+
/* istanbul ignore else */
|
|
71
|
+
if (characters.length > tree.length) {
|
|
72
|
+
return {
|
|
73
|
+
overflow: true
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
55
77
|
return {
|
|
56
78
|
match: true,
|
|
57
79
|
matchedChars: characters.slice(0, tree.length)
|
|
@@ -172,202 +194,4 @@ function match(characters, tree, last) {
|
|
|
172
194
|
default:
|
|
173
195
|
throw new Error(`Unsupported instruction tree: ${tree}`)
|
|
174
196
|
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
const OPERATOR = new RegExp(
|
|
178
|
-
// any of:
|
|
179
|
-
'(' +
|
|
180
|
-
// or operator
|
|
181
|
-
'\\|' +
|
|
182
|
-
// or
|
|
183
|
-
'|' +
|
|
184
|
-
// or group start
|
|
185
|
-
'\\(\\?\\:' +
|
|
186
|
-
// or
|
|
187
|
-
'|' +
|
|
188
|
-
// or group end
|
|
189
|
-
'\\)' +
|
|
190
|
-
// or
|
|
191
|
-
'|' +
|
|
192
|
-
// one-of set start
|
|
193
|
-
'\\[' +
|
|
194
|
-
// or
|
|
195
|
-
'|' +
|
|
196
|
-
// one-of set end
|
|
197
|
-
'\\]' +
|
|
198
|
-
')'
|
|
199
|
-
)
|
|
200
|
-
|
|
201
|
-
const ILLEGAL_CHARACTER_REGEXP = /[\(\)\[\]\?\:\|]/
|
|
202
|
-
|
|
203
|
-
class PatternParser {
|
|
204
|
-
parse(pattern) {
|
|
205
|
-
this.context = [{
|
|
206
|
-
or: true,
|
|
207
|
-
instructions: []
|
|
208
|
-
}]
|
|
209
|
-
|
|
210
|
-
this.parsePattern(pattern)
|
|
211
|
-
|
|
212
|
-
if (this.context.length !== 1) {
|
|
213
|
-
throw new Error('Non-finalized contexts left when pattern parse ended')
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
const { branches, instructions } = this.context[0]
|
|
217
|
-
|
|
218
|
-
if (branches) {
|
|
219
|
-
return [{
|
|
220
|
-
op: '|',
|
|
221
|
-
args: branches.concat([instructions])
|
|
222
|
-
}]
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
/* istanbul ignore if */
|
|
226
|
-
if (instructions.length === 0) {
|
|
227
|
-
throw new Error('Pattern is required')
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
return instructions
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
startContext(context) {
|
|
234
|
-
this.context.push(context)
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
endContext() {
|
|
238
|
-
this.context.pop()
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
getContext() {
|
|
242
|
-
return this.context[this.context.length - 1]
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
parsePattern(pattern) {
|
|
246
|
-
if (!pattern) {
|
|
247
|
-
throw new Error('Pattern is required')
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
const match = pattern.match(OPERATOR)
|
|
251
|
-
if (!match) {
|
|
252
|
-
if (ILLEGAL_CHARACTER_REGEXP.test(pattern)) {
|
|
253
|
-
throw new Error(`Illegal characters found in a pattern: ${pattern}`)
|
|
254
|
-
}
|
|
255
|
-
this.getContext().instructions = this.getContext().instructions.concat(
|
|
256
|
-
pattern.split('')
|
|
257
|
-
)
|
|
258
|
-
return
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
const operator = match[1]
|
|
262
|
-
const before = pattern.slice(0, match.index)
|
|
263
|
-
const rightPart = pattern.slice(match.index + operator.length)
|
|
264
|
-
|
|
265
|
-
switch (operator) {
|
|
266
|
-
case '(?:':
|
|
267
|
-
if (before) {
|
|
268
|
-
this.parsePattern(before)
|
|
269
|
-
}
|
|
270
|
-
this.startContext({
|
|
271
|
-
or: true,
|
|
272
|
-
instructions: [],
|
|
273
|
-
branches: []
|
|
274
|
-
})
|
|
275
|
-
break
|
|
276
|
-
|
|
277
|
-
case ')':
|
|
278
|
-
if (!this.getContext().or) {
|
|
279
|
-
throw new Error('")" operator must be preceded by "(?:" operator')
|
|
280
|
-
}
|
|
281
|
-
if (before) {
|
|
282
|
-
this.parsePattern(before)
|
|
283
|
-
}
|
|
284
|
-
if (this.getContext().instructions.length === 0) {
|
|
285
|
-
throw new Error('No instructions found after "|" operator in an "or" group')
|
|
286
|
-
}
|
|
287
|
-
const { branches } = this.getContext()
|
|
288
|
-
branches.push(
|
|
289
|
-
this.getContext().instructions
|
|
290
|
-
)
|
|
291
|
-
this.endContext()
|
|
292
|
-
this.getContext().instructions.push({
|
|
293
|
-
op: '|',
|
|
294
|
-
args: branches
|
|
295
|
-
})
|
|
296
|
-
break
|
|
297
|
-
|
|
298
|
-
case '|':
|
|
299
|
-
if (!this.getContext().or) {
|
|
300
|
-
throw new Error('"|" operator can only be used inside "or" groups')
|
|
301
|
-
}
|
|
302
|
-
if (before) {
|
|
303
|
-
this.parsePattern(before)
|
|
304
|
-
}
|
|
305
|
-
// The top-level is an implicit "or" group, if required.
|
|
306
|
-
if (!this.getContext().branches) {
|
|
307
|
-
// `branches` are not defined only for the root implicit "or" operator.
|
|
308
|
-
/* istanbul ignore else */
|
|
309
|
-
if (this.context.length === 1) {
|
|
310
|
-
this.getContext().branches = []
|
|
311
|
-
} else {
|
|
312
|
-
throw new Error('"branches" not found in an "or" group context')
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
this.getContext().branches.push(
|
|
316
|
-
this.getContext().instructions
|
|
317
|
-
)
|
|
318
|
-
this.getContext().instructions = []
|
|
319
|
-
break
|
|
320
|
-
|
|
321
|
-
case '[':
|
|
322
|
-
if (before) {
|
|
323
|
-
this.parsePattern(before)
|
|
324
|
-
}
|
|
325
|
-
this.startContext({
|
|
326
|
-
oneOfSet: true
|
|
327
|
-
})
|
|
328
|
-
break
|
|
329
|
-
|
|
330
|
-
case ']':
|
|
331
|
-
if (!this.getContext().oneOfSet) {
|
|
332
|
-
throw new Error('"]" operator must be preceded by "[" operator')
|
|
333
|
-
}
|
|
334
|
-
this.endContext()
|
|
335
|
-
this.getContext().instructions.push({
|
|
336
|
-
op: '[]',
|
|
337
|
-
args: parseOneOfSet(before)
|
|
338
|
-
})
|
|
339
|
-
break
|
|
340
|
-
|
|
341
|
-
/* istanbul ignore next */
|
|
342
|
-
default:
|
|
343
|
-
throw new Error(`Unknown operator: ${operator}`)
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
if (rightPart) {
|
|
347
|
-
this.parsePattern(rightPart)
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
function parseOneOfSet(pattern) {
|
|
353
|
-
const values = []
|
|
354
|
-
let i = 0
|
|
355
|
-
while (i < pattern.length) {
|
|
356
|
-
if (pattern[i] === '-') {
|
|
357
|
-
if (i === 0 || i === pattern.length - 1) {
|
|
358
|
-
throw new Error(`Couldn't parse a one-of set pattern: ${pattern}`)
|
|
359
|
-
}
|
|
360
|
-
const prevValue = pattern[i - 1].charCodeAt(0) + 1
|
|
361
|
-
const nextValue = pattern[i + 1].charCodeAt(0) - 1
|
|
362
|
-
let value = prevValue
|
|
363
|
-
while (value <= nextValue) {
|
|
364
|
-
values.push(String.fromCharCode(value))
|
|
365
|
-
value++
|
|
366
|
-
}
|
|
367
|
-
} else {
|
|
368
|
-
values.push(pattern[i])
|
|
369
|
-
}
|
|
370
|
-
i++
|
|
371
|
-
}
|
|
372
|
-
return values
|
|
373
197
|
}
|
|
@@ -630,4 +630,12 @@ describe('AsYouTypeFormatter.PatternMatcher', function() {
|
|
|
630
630
|
match: true
|
|
631
631
|
})
|
|
632
632
|
})
|
|
633
|
+
|
|
634
|
+
it('shouldn\'t match things that shouldn\'t match', function() {
|
|
635
|
+
// There was a bug: "leading digits" `"2"` matched "leading digits pattern" `"90"`.
|
|
636
|
+
// The incorrect `.match()` function result was `{ oveflow: true }`
|
|
637
|
+
// while it should've been `undefined`.
|
|
638
|
+
// https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/66
|
|
639
|
+
expect(new PatternMatcher('2').match('90', { allowOverflow: true })).to.be.undefined
|
|
640
|
+
})
|
|
633
641
|
})
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// A string of length `1`.
|
|
2
|
+
//
|
|
3
|
+
// Example: "3".
|
|
4
|
+
//
|
|
5
|
+
export type character = string;
|
|
6
|
+
|
|
7
|
+
// Matches any character.
|
|
8
|
+
//
|
|
9
|
+
// Example:
|
|
10
|
+
//
|
|
11
|
+
// String pattern "123" compiles into 3 characters:
|
|
12
|
+
//
|
|
13
|
+
// ["1", "2", "3"]
|
|
14
|
+
//
|
|
15
|
+
type Character = character;
|
|
16
|
+
|
|
17
|
+
// Matches one of characters.
|
|
18
|
+
//
|
|
19
|
+
// Example:
|
|
20
|
+
//
|
|
21
|
+
// String pattern "[5-9]" compiles into:
|
|
22
|
+
//
|
|
23
|
+
// { op: "[]", args: ["5", "6", "7", "8", "9"] }
|
|
24
|
+
//
|
|
25
|
+
interface OneOfCharacters {
|
|
26
|
+
op: '[]';
|
|
27
|
+
args: character[];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Matches any of the subtrees.
|
|
31
|
+
//
|
|
32
|
+
// Example:
|
|
33
|
+
//
|
|
34
|
+
// String pattern "123|[5-9]0" compiles into:
|
|
35
|
+
//
|
|
36
|
+
// {
|
|
37
|
+
// op: "|",
|
|
38
|
+
// args: [
|
|
39
|
+
// // First subtree:
|
|
40
|
+
// ["1", "2", "3"],
|
|
41
|
+
// // Second subtree:
|
|
42
|
+
// [
|
|
43
|
+
// { op: "[]", args: ["5", "6", "7", "8", "9"] },
|
|
44
|
+
// "0"
|
|
45
|
+
// ]
|
|
46
|
+
// ]
|
|
47
|
+
// }
|
|
48
|
+
//
|
|
49
|
+
interface OrCondition<MatchTree> {
|
|
50
|
+
op: '|';
|
|
51
|
+
args: MatchTree[];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export type MatchTree = Character | OneOfCharacters | MatchTree[] | OrCondition<MatchTree>;
|
|
55
|
+
|
|
56
|
+
export default class PatternParser {
|
|
57
|
+
parse(pattern: string): MatchTree;
|
|
58
|
+
}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
export default class PatternParser {
|
|
2
|
+
parse(pattern) {
|
|
3
|
+
this.context = [{
|
|
4
|
+
or: true,
|
|
5
|
+
instructions: []
|
|
6
|
+
}]
|
|
7
|
+
|
|
8
|
+
this.parsePattern(pattern)
|
|
9
|
+
|
|
10
|
+
if (this.context.length !== 1) {
|
|
11
|
+
throw new Error('Non-finalized contexts left when pattern parse ended')
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const { branches, instructions } = this.context[0]
|
|
15
|
+
|
|
16
|
+
if (branches) {
|
|
17
|
+
return {
|
|
18
|
+
op: '|',
|
|
19
|
+
args: branches.concat([
|
|
20
|
+
expandSingleElementArray(instructions)
|
|
21
|
+
])
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/* istanbul ignore if */
|
|
26
|
+
if (instructions.length === 0) {
|
|
27
|
+
throw new Error('Pattern is required')
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (instructions.length === 1) {
|
|
31
|
+
return instructions[0]
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return instructions
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
startContext(context) {
|
|
38
|
+
this.context.push(context)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
endContext() {
|
|
42
|
+
this.context.pop()
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
getContext() {
|
|
46
|
+
return this.context[this.context.length - 1]
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
parsePattern(pattern) {
|
|
50
|
+
if (!pattern) {
|
|
51
|
+
throw new Error('Pattern is required')
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const match = pattern.match(OPERATOR)
|
|
55
|
+
if (!match) {
|
|
56
|
+
if (ILLEGAL_CHARACTER_REGEXP.test(pattern)) {
|
|
57
|
+
throw new Error(`Illegal characters found in a pattern: ${pattern}`)
|
|
58
|
+
}
|
|
59
|
+
this.getContext().instructions = this.getContext().instructions.concat(
|
|
60
|
+
pattern.split('')
|
|
61
|
+
)
|
|
62
|
+
return
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const operator = match[1]
|
|
66
|
+
const before = pattern.slice(0, match.index)
|
|
67
|
+
const rightPart = pattern.slice(match.index + operator.length)
|
|
68
|
+
|
|
69
|
+
switch (operator) {
|
|
70
|
+
case '(?:':
|
|
71
|
+
if (before) {
|
|
72
|
+
this.parsePattern(before)
|
|
73
|
+
}
|
|
74
|
+
this.startContext({
|
|
75
|
+
or: true,
|
|
76
|
+
instructions: [],
|
|
77
|
+
branches: []
|
|
78
|
+
})
|
|
79
|
+
break
|
|
80
|
+
|
|
81
|
+
case ')':
|
|
82
|
+
if (!this.getContext().or) {
|
|
83
|
+
throw new Error('")" operator must be preceded by "(?:" operator')
|
|
84
|
+
}
|
|
85
|
+
if (before) {
|
|
86
|
+
this.parsePattern(before)
|
|
87
|
+
}
|
|
88
|
+
if (this.getContext().instructions.length === 0) {
|
|
89
|
+
throw new Error('No instructions found after "|" operator in an "or" group')
|
|
90
|
+
}
|
|
91
|
+
const { branches } = this.getContext()
|
|
92
|
+
branches.push(
|
|
93
|
+
expandSingleElementArray(
|
|
94
|
+
this.getContext().instructions
|
|
95
|
+
)
|
|
96
|
+
)
|
|
97
|
+
this.endContext()
|
|
98
|
+
this.getContext().instructions.push({
|
|
99
|
+
op: '|',
|
|
100
|
+
args: branches
|
|
101
|
+
})
|
|
102
|
+
break
|
|
103
|
+
|
|
104
|
+
case '|':
|
|
105
|
+
if (!this.getContext().or) {
|
|
106
|
+
throw new Error('"|" operator can only be used inside "or" groups')
|
|
107
|
+
}
|
|
108
|
+
if (before) {
|
|
109
|
+
this.parsePattern(before)
|
|
110
|
+
}
|
|
111
|
+
// The top-level is an implicit "or" group, if required.
|
|
112
|
+
if (!this.getContext().branches) {
|
|
113
|
+
// `branches` are not defined only for the root implicit "or" operator.
|
|
114
|
+
/* istanbul ignore else */
|
|
115
|
+
if (this.context.length === 1) {
|
|
116
|
+
this.getContext().branches = []
|
|
117
|
+
} else {
|
|
118
|
+
throw new Error('"branches" not found in an "or" group context')
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
this.getContext().branches.push(
|
|
122
|
+
expandSingleElementArray(
|
|
123
|
+
this.getContext().instructions
|
|
124
|
+
)
|
|
125
|
+
)
|
|
126
|
+
this.getContext().instructions = []
|
|
127
|
+
break
|
|
128
|
+
|
|
129
|
+
case '[':
|
|
130
|
+
if (before) {
|
|
131
|
+
this.parsePattern(before)
|
|
132
|
+
}
|
|
133
|
+
this.startContext({
|
|
134
|
+
oneOfSet: true
|
|
135
|
+
})
|
|
136
|
+
break
|
|
137
|
+
|
|
138
|
+
case ']':
|
|
139
|
+
if (!this.getContext().oneOfSet) {
|
|
140
|
+
throw new Error('"]" operator must be preceded by "[" operator')
|
|
141
|
+
}
|
|
142
|
+
this.endContext()
|
|
143
|
+
this.getContext().instructions.push({
|
|
144
|
+
op: '[]',
|
|
145
|
+
args: parseOneOfSet(before)
|
|
146
|
+
})
|
|
147
|
+
break
|
|
148
|
+
|
|
149
|
+
/* istanbul ignore next */
|
|
150
|
+
default:
|
|
151
|
+
throw new Error(`Unknown operator: ${operator}`)
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (rightPart) {
|
|
155
|
+
this.parsePattern(rightPart)
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function parseOneOfSet(pattern) {
|
|
161
|
+
const values = []
|
|
162
|
+
let i = 0
|
|
163
|
+
while (i < pattern.length) {
|
|
164
|
+
if (pattern[i] === '-') {
|
|
165
|
+
if (i === 0 || i === pattern.length - 1) {
|
|
166
|
+
throw new Error(`Couldn't parse a one-of set pattern: ${pattern}`)
|
|
167
|
+
}
|
|
168
|
+
const prevValue = pattern[i - 1].charCodeAt(0) + 1
|
|
169
|
+
const nextValue = pattern[i + 1].charCodeAt(0) - 1
|
|
170
|
+
let value = prevValue
|
|
171
|
+
while (value <= nextValue) {
|
|
172
|
+
values.push(String.fromCharCode(value))
|
|
173
|
+
value++
|
|
174
|
+
}
|
|
175
|
+
} else {
|
|
176
|
+
values.push(pattern[i])
|
|
177
|
+
}
|
|
178
|
+
i++
|
|
179
|
+
}
|
|
180
|
+
return values
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const ILLEGAL_CHARACTER_REGEXP = /[\(\)\[\]\?\:\|]/
|
|
184
|
+
|
|
185
|
+
const OPERATOR = new RegExp(
|
|
186
|
+
// any of:
|
|
187
|
+
'(' +
|
|
188
|
+
// or operator
|
|
189
|
+
'\\|' +
|
|
190
|
+
// or
|
|
191
|
+
'|' +
|
|
192
|
+
// or group start
|
|
193
|
+
'\\(\\?\\:' +
|
|
194
|
+
// or
|
|
195
|
+
'|' +
|
|
196
|
+
// or group end
|
|
197
|
+
'\\)' +
|
|
198
|
+
// or
|
|
199
|
+
'|' +
|
|
200
|
+
// one-of set start
|
|
201
|
+
'\\[' +
|
|
202
|
+
// or
|
|
203
|
+
'|' +
|
|
204
|
+
// one-of set end
|
|
205
|
+
'\\]' +
|
|
206
|
+
')'
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
function expandSingleElementArray(array) {
|
|
210
|
+
if (array.length === 1) {
|
|
211
|
+
return array[0]
|
|
212
|
+
}
|
|
213
|
+
return array
|
|
214
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import PatternParser from './AsYouTypeFormatter.PatternParser.js'
|
|
2
|
+
|
|
3
|
+
describe('PatternParser', function() {
|
|
4
|
+
it('should parse single-character patterns', function() {
|
|
5
|
+
new PatternParser().parse('2').should.deep.equal('2')
|
|
6
|
+
})
|
|
7
|
+
|
|
8
|
+
it('should parse string patterns', function() {
|
|
9
|
+
new PatternParser().parse('123').should.deep.equal(['1', '2', '3'])
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
it('should parse "one of" patterns', function() {
|
|
13
|
+
new PatternParser().parse('[5-9]').should.deep.equal({
|
|
14
|
+
op: '[]',
|
|
15
|
+
args: ['5', '6', '7', '8', '9']
|
|
16
|
+
})
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('should parse "or" patterns', function() {
|
|
20
|
+
new PatternParser().parse('123|[5-9]').should.deep.equal({
|
|
21
|
+
op: '|',
|
|
22
|
+
args: [
|
|
23
|
+
['1', '2', '3'],
|
|
24
|
+
{
|
|
25
|
+
op: '[]',
|
|
26
|
+
args: ['5', '6', '7', '8', '9']
|
|
27
|
+
}
|
|
28
|
+
]
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
new PatternParser().parse('123|[5-9]0').should.deep.equal({
|
|
32
|
+
op: '|',
|
|
33
|
+
args: [
|
|
34
|
+
['1', '2', '3'],
|
|
35
|
+
[
|
|
36
|
+
{
|
|
37
|
+
op: '[]',
|
|
38
|
+
args: ['5', '6', '7', '8', '9']
|
|
39
|
+
},
|
|
40
|
+
'0'
|
|
41
|
+
]
|
|
42
|
+
]
|
|
43
|
+
})
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('should parse nested "or" patterns', function() {
|
|
47
|
+
new PatternParser().parse('123|(?:2|34)[5-9]').should.deep.equal({
|
|
48
|
+
op: '|',
|
|
49
|
+
args: [
|
|
50
|
+
['1', '2', '3'],
|
|
51
|
+
[
|
|
52
|
+
{
|
|
53
|
+
op: '|',
|
|
54
|
+
args: [
|
|
55
|
+
'2',
|
|
56
|
+
['3', '4']
|
|
57
|
+
]
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
op: '[]',
|
|
61
|
+
args: ['5', '6', '7', '8', '9']
|
|
62
|
+
}
|
|
63
|
+
]
|
|
64
|
+
]
|
|
65
|
+
})
|
|
66
|
+
})
|
|
67
|
+
})
|
|
@@ -189,8 +189,10 @@ export default class AsYouTypeFormatter {
|
|
|
189
189
|
// Formats the next phone number digits.
|
|
190
190
|
formatNationalNumberWithNextDigits(nextDigits, state) {
|
|
191
191
|
const previouslyChosenFormat = this.chosenFormat
|
|
192
|
+
|
|
192
193
|
// Choose a format from the list of matching ones.
|
|
193
194
|
const newlyChosenFormat = this.chooseFormat(state)
|
|
195
|
+
|
|
194
196
|
if (newlyChosenFormat) {
|
|
195
197
|
if (newlyChosenFormat === previouslyChosenFormat) {
|
|
196
198
|
// If it can format the next (current) digits
|
package/max/metadata.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
// Importing from `.json.js` a workaround for a bug in web browsers' "native"
|
|
2
|
-
// ES6 importing system which is uncapable of importing "*.json" files.
|
|
3
|
-
// https://github.com/catamphetamine/libphonenumber-js/issues/239
|
|
4
|
-
import metadata from '../metadata.max.json.js'
|
|
5
|
-
export default metadata
|
|
6
|
-
|
|
7
|
-
export function withMetadata(func, _arguments) {
|
|
8
|
-
var args = Array.prototype.slice.call(_arguments)
|
|
9
|
-
args.push(metadata)
|
|
10
|
-
return func.apply(this, args)
|
|
11
|
-
}
|