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.
Files changed (83) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/README.md +14 -8
  3. package/build/AsYouType.test.js +25 -3
  4. package/build/AsYouType.test.js.map +1 -1
  5. package/build/AsYouTypeFormatter.PatternMatcher.js +435 -0
  6. package/build/AsYouTypeFormatter.PatternMatcher.js.map +1 -0
  7. package/build/AsYouTypeFormatter.PatternMatcher.test.js +447 -0
  8. package/build/AsYouTypeFormatter.PatternMatcher.test.js.map +1 -0
  9. package/build/AsYouTypeFormatter.js +57 -10
  10. package/build/AsYouTypeFormatter.js.map +1 -1
  11. package/build/PhoneNumber.test.js +9 -9
  12. package/build/PhoneNumber.test.js.map +1 -1
  13. package/build/findNumbers.test.js +23 -23
  14. package/build/findNumbers.test.js.map +1 -1
  15. package/build/getExampleNumber.test.js +4 -4
  16. package/build/getExampleNumber.test.js.map +1 -1
  17. package/build/getNumberType.test.js +3 -3
  18. package/build/getNumberType.test.js.map +1 -1
  19. package/build/helpers/checkNumberLength.test.js +3 -3
  20. package/build/helpers/checkNumberLength.test.js.map +1 -1
  21. package/build/helpers/extractCountryCallingCode.test.js +4 -4
  22. package/build/helpers/extractCountryCallingCode.test.js.map +1 -1
  23. package/build/parsePhoneNumber.test.js +2 -2
  24. package/build/parsePhoneNumber.test.js.map +1 -1
  25. package/bundle/libphonenumber-js.min.js +1 -1
  26. package/bundle/libphonenumber-js.min.js.map +1 -1
  27. package/bundle/libphonenumber-max.js +1 -1
  28. package/bundle/libphonenumber-max.js.map +1 -1
  29. package/bundle/libphonenumber-min.js +1 -1
  30. package/bundle/libphonenumber-min.js.map +1 -1
  31. package/bundle/libphonenumber-mobile.js +1 -1
  32. package/bundle/libphonenumber-mobile.js.map +1 -1
  33. package/es6/AsYouType.test.js +25 -3
  34. package/es6/AsYouType.test.js.map +1 -1
  35. package/es6/AsYouTypeFormatter.PatternMatcher.js +428 -0
  36. package/es6/AsYouTypeFormatter.PatternMatcher.js.map +1 -0
  37. package/es6/AsYouTypeFormatter.PatternMatcher.test.js +442 -0
  38. package/es6/AsYouTypeFormatter.PatternMatcher.test.js.map +1 -0
  39. package/es6/AsYouTypeFormatter.js +56 -10
  40. package/es6/AsYouTypeFormatter.js.map +1 -1
  41. package/es6/PhoneNumber.test.js +1 -1
  42. package/es6/PhoneNumber.test.js.map +1 -1
  43. package/es6/findNumbers.test.js +1 -1
  44. package/es6/findNumbers.test.js.map +1 -1
  45. package/es6/getExampleNumber.test.js +2 -2
  46. package/es6/getExampleNumber.test.js.map +1 -1
  47. package/es6/getNumberType.test.js +1 -1
  48. package/es6/getNumberType.test.js.map +1 -1
  49. package/es6/helpers/checkNumberLength.test.js +1 -1
  50. package/es6/helpers/checkNumberLength.test.js.map +1 -1
  51. package/es6/helpers/extractCountryCallingCode.test.js +1 -1
  52. package/es6/helpers/extractCountryCallingCode.test.js.map +1 -1
  53. package/es6/parsePhoneNumber.test.js +1 -1
  54. package/es6/parsePhoneNumber.test.js.map +1 -1
  55. package/exports/README.md +98 -0
  56. package/exports/examples.mobile.json/index.js +1 -0
  57. package/exports/metadata.max.json/index.js +1 -0
  58. package/exports/metadata.min.json/index.js +1 -0
  59. package/exports/metadata.mobile.json/index.js +1 -0
  60. package/exports/package.exports.json +55 -0
  61. package/max/index.commonjs.js +1 -1
  62. package/max/metadata.js +1 -1
  63. package/metadata.full.json +1 -1
  64. package/metadata.full.json.js +1 -1
  65. package/metadata.max.json +1 -0
  66. package/metadata.max.json.d.ts +4 -0
  67. package/metadata.max.json.js +4 -0
  68. package/metadata.min.json +1 -1
  69. package/metadata.min.json.js +1 -1
  70. package/metadata.mobile.json +1 -1
  71. package/metadata.mobile.json.js +1 -1
  72. package/package.json +4 -2
  73. package/source/AsYouType.test.js +26 -3
  74. package/source/AsYouTypeFormatter.PatternMatcher.js +343 -0
  75. package/source/AsYouTypeFormatter.PatternMatcher.test.js +596 -0
  76. package/source/AsYouTypeFormatter.js +58 -9
  77. package/source/PhoneNumber.test.js +1 -1
  78. package/source/findNumbers.test.js +1 -1
  79. package/source/getExampleNumber.test.js +2 -2
  80. package/source/getNumberType.test.js +1 -1
  81. package/source/helpers/checkNumberLength.test.js +1 -1
  82. package/source/helpers/extractCountryCallingCode.test.js +1 -1
  83. package/source/parsePhoneNumber.test.js +1 -1
@@ -0,0 +1,596 @@
1
+ import PatternMatcher from './AsYouTypeFormatter.PatternMatcher'
2
+
3
+ describe('AsYouTypeFormatter.PatternMatcher', function() {
4
+ it('should match a one-digit pattern', function() {
5
+ const matcher = new PatternMatcher('4')
6
+
7
+ expect(matcher.match('1')).to.be.undefined
8
+
9
+ matcher.match('4').should.deep.equal({
10
+ match: true
11
+ })
12
+
13
+ expect(matcher.match('44')).to.be.undefined
14
+
15
+ matcher.match('44', { allowOverflow: true }).should.deep.equal({
16
+ overflow: true
17
+ })
18
+ })
19
+
20
+ it('should match a two-digit pattern', function() {
21
+ const matcher = new PatternMatcher('44')
22
+
23
+ expect(matcher.match('1')).to.be.undefined
24
+
25
+ matcher.match('4').should.deep.equal({
26
+ partialMatch: true
27
+ })
28
+
29
+ matcher.match('44').should.deep.equal({
30
+ match: true
31
+ })
32
+
33
+ expect(matcher.match('444')).to.be.undefined
34
+
35
+ matcher.match('444', { allowOverflow: true }).should.deep.equal({
36
+ overflow: true
37
+ })
38
+
39
+ expect(matcher.match('55')).to.be.undefined
40
+ })
41
+
42
+ it('should match a one-digit one-of set (single digit)', function() {
43
+ const matcher = new PatternMatcher('[4]')
44
+
45
+ expect(matcher.match('1')).to.be.undefined
46
+
47
+ matcher.match('4').should.deep.equal({
48
+ match: true
49
+ })
50
+
51
+ expect(matcher.match('44')).to.be.undefined
52
+
53
+ matcher.match('44', { allowOverflow: true }).should.deep.equal({
54
+ overflow: true
55
+ })
56
+ })
57
+
58
+ it('should match a one-digit one-of set (multiple digits)', function() {
59
+ const matcher = new PatternMatcher('[479]')
60
+
61
+ expect(matcher.match('1')).to.be.undefined
62
+
63
+ matcher.match('4').should.deep.equal({
64
+ match: true
65
+ })
66
+
67
+ expect(matcher.match('44')).to.be.undefined
68
+
69
+ matcher.match('44', { allowOverflow: true }).should.deep.equal({
70
+ overflow: true
71
+ })
72
+ })
73
+
74
+ it('should match a one-digit one-of set using a dash notation (not inclusive)', function() {
75
+ const matcher = new PatternMatcher('[2-5]')
76
+
77
+ expect(matcher.match('1')).to.be.undefined
78
+
79
+ matcher.match('4').should.deep.equal({
80
+ match: true
81
+ })
82
+
83
+ expect(matcher.match('44')).to.be.undefined
84
+
85
+ matcher.match('44', { allowOverflow: true }).should.deep.equal({
86
+ overflow: true
87
+ })
88
+ })
89
+
90
+ it('should match a one-digit one-of set using a dash notation (inclusive)', function() {
91
+ const matcher = new PatternMatcher('[3-4]')
92
+
93
+ expect(matcher.match('1')).to.be.undefined
94
+
95
+ matcher.match('4').should.deep.equal({
96
+ match: true
97
+ })
98
+
99
+ expect(matcher.match('44')).to.be.undefined
100
+
101
+ matcher.match('44', { allowOverflow: true }).should.deep.equal({
102
+ overflow: true
103
+ })
104
+ })
105
+
106
+ it('should match a one-digit one-of set including a dash notation', function() {
107
+ const matcher = new PatternMatcher('[124-68]')
108
+
109
+ expect(matcher.match('0')).to.be.undefined
110
+
111
+ matcher.match('1').should.deep.equal({
112
+ match: true
113
+ })
114
+
115
+ matcher.match('2').should.deep.equal({
116
+ match: true
117
+ })
118
+
119
+ expect(matcher.match('3')).to.be.undefined
120
+
121
+ matcher.match('4').should.deep.equal({
122
+ match: true
123
+ })
124
+
125
+ matcher.match('5').should.deep.equal({
126
+ match: true
127
+ })
128
+
129
+ matcher.match('6').should.deep.equal({
130
+ match: true
131
+ })
132
+
133
+ expect(matcher.match('7')).to.be.undefined
134
+
135
+ matcher.match('8').should.deep.equal({
136
+ match: true
137
+ })
138
+
139
+ expect(matcher.match('9')).to.be.undefined
140
+
141
+ expect(matcher.match('88')).to.be.undefined
142
+
143
+ matcher.match('88', { allowOverflow: true }).should.deep.equal({
144
+ overflow: true
145
+ })
146
+ })
147
+
148
+ it('should match a two-digit one-of set', function() {
149
+ const matcher = new PatternMatcher('[479][45]')
150
+
151
+ expect(matcher.match('1')).to.be.undefined
152
+
153
+ matcher.match('4').should.deep.equal({
154
+ partialMatch: true
155
+ })
156
+
157
+ expect(matcher.match('5')).to.be.undefined
158
+ expect(matcher.match('55')).to.be.undefined
159
+
160
+ matcher.match('44').should.deep.equal({
161
+ match: true
162
+ })
163
+
164
+ expect(matcher.match('444')).to.be.undefined
165
+
166
+ matcher.match('444', { allowOverflow: true }).should.deep.equal({
167
+ overflow: true
168
+ })
169
+ })
170
+
171
+ it('should match a two-digit one-of set (regular digit and a one-of set)', function() {
172
+ const matcher = new PatternMatcher('1[45]')
173
+
174
+ expect(matcher.match('0')).to.be.undefined
175
+
176
+ matcher.match('1').should.deep.equal({
177
+ partialMatch: true
178
+ })
179
+
180
+ matcher.match('15').should.deep.equal({
181
+ match: true
182
+ })
183
+
184
+ expect(matcher.match('16')).to.be.undefined
185
+ })
186
+
187
+ it('should match a pattern with an or group', function() {
188
+ const matcher = new PatternMatcher('7(?:1[0-68]|2[1-9])')
189
+
190
+ expect(matcher.match('1')).to.be.undefined
191
+
192
+ matcher.match('7').should.deep.equal({
193
+ partialMatch: true
194
+ })
195
+
196
+ matcher.match('71').should.deep.equal({
197
+ partialMatch: true
198
+ })
199
+
200
+ expect(matcher.match('73')).to.be.undefined
201
+
202
+ matcher.match('711').should.deep.equal({
203
+ match: true
204
+ })
205
+
206
+ expect(matcher.match('717')).to.be.undefined
207
+
208
+ expect(matcher.match('720')).to.be.undefined
209
+
210
+ matcher.match('722').should.deep.equal({
211
+ match: true
212
+ })
213
+
214
+ expect(matcher.match('7222')).to.be.undefined
215
+
216
+ matcher.match('7222', { allowOverflow: true }).should.deep.equal({
217
+ overflow: true
218
+ })
219
+ })
220
+
221
+ it('should match an or pattern containing or groups', function() {
222
+ const matcher = new PatternMatcher('2(?:2[024-9]|3[0-59]|47|6[245]|9[02-8])|3(?:3[28]|4[03-9]|5[2-46-8]|7[1-578]|8[2-9])')
223
+
224
+ expect(matcher.match('1')).to.be.undefined
225
+
226
+ matcher.match('2').should.deep.equal({
227
+ partialMatch: true
228
+ })
229
+
230
+ matcher.match('3').should.deep.equal({
231
+ partialMatch: true
232
+ })
233
+
234
+ expect(matcher.match('4')).to.be.undefined
235
+
236
+ expect(matcher.match('21')).to.be.undefined
237
+
238
+ matcher.match('22').should.deep.equal({
239
+ partialMatch: true
240
+ })
241
+
242
+ expect(matcher.match('221')).to.be.undefined
243
+
244
+ matcher.match('222').should.deep.equal({
245
+ match: true
246
+ })
247
+
248
+ expect(matcher.match('2222')).to.be.undefined
249
+
250
+ matcher.match('2222', { allowOverflow: true }).should.deep.equal({
251
+ overflow: true
252
+ })
253
+
254
+ matcher.match('3').should.deep.equal({
255
+ partialMatch: true
256
+ })
257
+
258
+ matcher.match('33').should.deep.equal({
259
+ partialMatch: true
260
+ })
261
+
262
+ matcher.match('332').should.deep.equal({
263
+ match: true
264
+ })
265
+
266
+ expect(matcher.match('333')).to.be.undefined
267
+ })
268
+
269
+ it('should match an or pattern', function() {
270
+ const matcher = new PatternMatcher('6|8')
271
+
272
+ expect(matcher.match('5')).to.be.undefined
273
+
274
+ matcher.match('6').should.deep.equal({
275
+ match: true
276
+ })
277
+
278
+ expect(matcher.match('7')).to.be.undefined
279
+
280
+ matcher.match('8').should.deep.equal({
281
+ match: true
282
+ })
283
+ })
284
+
285
+ it('should match an or pattern (one-of sets)', function() {
286
+ const matcher = new PatternMatcher('[123]|[5-8]')
287
+
288
+ expect(matcher.match('0')).to.be.undefined
289
+
290
+ matcher.match('1').should.deep.equal({
291
+ match: true
292
+ })
293
+
294
+ matcher.match('2').should.deep.equal({
295
+ match: true
296
+ })
297
+
298
+ matcher.match('3').should.deep.equal({
299
+ match: true
300
+ })
301
+
302
+ expect(matcher.match('4')).to.be.undefined
303
+
304
+ matcher.match('5').should.deep.equal({
305
+ match: true
306
+ })
307
+
308
+ matcher.match('6').should.deep.equal({
309
+ match: true
310
+ })
311
+
312
+ matcher.match('7').should.deep.equal({
313
+ match: true
314
+ })
315
+
316
+ matcher.match('8').should.deep.equal({
317
+ match: true
318
+ })
319
+
320
+ expect(matcher.match('9')).to.be.undefined
321
+
322
+ expect(matcher.match('18')).to.be.undefined
323
+
324
+ matcher.match('18', { allowOverflow: true }).should.deep.equal({
325
+ overflow: true
326
+ })
327
+ })
328
+
329
+ it('should match an or pattern (different lengths)', function() {
330
+ const matcher = new PatternMatcher('60|8')
331
+
332
+ expect(matcher.match('5')).to.be.undefined
333
+
334
+ matcher.match('6').should.deep.equal({
335
+ partialMatch: true
336
+ })
337
+
338
+ matcher.match('60').should.deep.equal({
339
+ match: true
340
+ })
341
+
342
+ expect(matcher.match('61')).to.be.undefined
343
+
344
+ expect(matcher.match('7')).to.be.undefined
345
+
346
+ matcher.match('8').should.deep.equal({
347
+ match: true
348
+ })
349
+
350
+ expect(matcher.match('68')).to.be.undefined
351
+ })
352
+
353
+ it('should match an or pattern (one-of sets) (different lengths)', function() {
354
+ const matcher = new PatternMatcher('[123]|[5-8][2-8]')
355
+
356
+ expect(matcher.match('0')).to.be.undefined
357
+ })
358
+
359
+ it('should match an or pattern (one-of sets and regular digits) (different lengths)', function() {
360
+ const matcher = new PatternMatcher('[2358][2-5]|4')
361
+
362
+ expect(matcher.match('0')).to.be.undefined
363
+
364
+ matcher.match('2').should.deep.equal({
365
+ partialMatch: true
366
+ })
367
+
368
+ expect(matcher.match('21')).to.be.undefined
369
+
370
+ matcher.match('22').should.deep.equal({
371
+ match: true
372
+ })
373
+
374
+ matcher.match('25').should.deep.equal({
375
+ match: true
376
+ })
377
+
378
+ expect(matcher.match('26')).to.be.undefined
379
+
380
+ expect(matcher.match('222')).to.be.undefined
381
+
382
+ matcher.match('222', { allowOverflow: true }).should.deep.equal({
383
+ overflow: true
384
+ })
385
+
386
+ matcher.match('3').should.deep.equal({
387
+ partialMatch: true
388
+ })
389
+
390
+ matcher.match('4').should.deep.equal({
391
+ match: true
392
+ })
393
+
394
+ expect(matcher.match('6')).to.be.undefined
395
+ })
396
+
397
+ it('should match an or pattern (one-of sets and regular digits mixed) (different lengths)', function() {
398
+ const matcher = new PatternMatcher('[2358]2|4')
399
+
400
+ expect(matcher.match('0')).to.be.undefined
401
+
402
+ matcher.match('2').should.deep.equal({
403
+ partialMatch: true
404
+ })
405
+
406
+ expect(matcher.match('21')).to.be.undefined
407
+
408
+ matcher.match('22').should.deep.equal({
409
+ match: true
410
+ })
411
+
412
+ expect(matcher.match('222')).to.be.undefined
413
+
414
+ matcher.match('222', { allowOverflow: true }).should.deep.equal({
415
+ overflow: true
416
+ })
417
+
418
+ matcher.match('3').should.deep.equal({
419
+ partialMatch: true
420
+ })
421
+
422
+ matcher.match('4').should.deep.equal({
423
+ match: true
424
+ })
425
+
426
+ expect(matcher.match('6')).to.be.undefined
427
+ })
428
+
429
+ it('should match an or pattern (one-of sets groups and regular digits mixed) (different lengths)', function() {
430
+ const matcher = new PatternMatcher('1(?:11|[2-9])')
431
+
432
+ matcher.match('1').should.deep.equal({
433
+ partialMatch: true
434
+ })
435
+
436
+ expect(matcher.match('10')).to.be.undefined
437
+
438
+ matcher.match('11').should.deep.equal({
439
+ partialMatch: true
440
+ })
441
+
442
+ matcher.match('111').should.deep.equal({
443
+ match: true
444
+ })
445
+
446
+ expect(matcher.match('1111')).to.be.undefined
447
+
448
+ matcher.match('1111', { allowOverflow: true }).should.deep.equal({
449
+ overflow: true
450
+ })
451
+
452
+ matcher.match('12').should.deep.equal({
453
+ match: true
454
+ })
455
+
456
+ expect(matcher.match('122')).to.be.undefined
457
+
458
+ matcher.match('19').should.deep.equal({
459
+ match: true
460
+ })
461
+
462
+ expect(matcher.match('5')).to.be.undefined
463
+ })
464
+
465
+ it('should match nested or groups', function() {
466
+ const matcher = new PatternMatcher('1(?:2(?:3(?:4|5)|6)|7(?:8|9))0')
467
+
468
+ matcher.match('1').should.deep.equal({
469
+ partialMatch: true
470
+ })
471
+
472
+ expect(matcher.match('2')).to.be.undefined
473
+
474
+ expect(matcher.match('11')).to.be.undefined
475
+
476
+ matcher.match('12').should.deep.equal({
477
+ partialMatch: true
478
+ })
479
+
480
+ expect(matcher.match('121')).to.be.undefined
481
+
482
+ matcher.match('123').should.deep.equal({
483
+ partialMatch: true
484
+ })
485
+
486
+ expect(matcher.match('1231')).to.be.undefined
487
+
488
+ matcher.match('1234').should.deep.equal({
489
+ partialMatch: true
490
+ })
491
+
492
+ matcher.match('12340').should.deep.equal({
493
+ match: true
494
+ })
495
+
496
+ expect(matcher.match('123401')).to.be.undefined
497
+
498
+ matcher.match('123401', { allowOverflow: true }).should.deep.equal({
499
+ overflow: true
500
+ })
501
+
502
+ matcher.match('12350').should.deep.equal({
503
+ match: true
504
+ })
505
+
506
+ expect(matcher.match('12360')).to.be.undefined
507
+
508
+ matcher.match('1260').should.deep.equal({
509
+ match: true
510
+ })
511
+
512
+ expect(matcher.match('1270')).to.be.undefined
513
+
514
+ expect(matcher.match('1770')).to.be.undefined
515
+
516
+ matcher.match('1780').should.deep.equal({
517
+ match: true
518
+ })
519
+
520
+ matcher.match('1790').should.deep.equal({
521
+ match: true
522
+ })
523
+
524
+ expect(matcher.match('18')).to.be.undefined
525
+ })
526
+
527
+ it('should match complex patterns', function() {
528
+ const matcher = new PatternMatcher('(?:31|4)6|51|6(?:5[0-3579]|[6-9])|7(?:20|32|8)|[89]')
529
+
530
+ expect(matcher.match('0')).to.be.undefined
531
+
532
+ matcher.match('3').should.deep.equal({
533
+ partialMatch: true
534
+ })
535
+
536
+ matcher.match('31').should.deep.equal({
537
+ partialMatch: true
538
+ })
539
+
540
+ expect(matcher.match('32')).to.be.undefined
541
+
542
+ matcher.match('316').should.deep.equal({
543
+ match: true
544
+ })
545
+
546
+ expect(matcher.match('315')).to.be.undefined
547
+
548
+ matcher.match('4').should.deep.equal({
549
+ partialMatch: true
550
+ })
551
+
552
+ matcher.match('46').should.deep.equal({
553
+ match: true
554
+ })
555
+
556
+ expect(matcher.match('47')).to.be.undefined
557
+
558
+ matcher.match('5').should.deep.equal({
559
+ partialMatch: true
560
+ })
561
+
562
+ expect(matcher.match('50')).to.be.undefined
563
+
564
+ matcher.match('51').should.deep.equal({
565
+ match: true
566
+ })
567
+
568
+ matcher.match('6').should.deep.equal({
569
+ partialMatch: true
570
+ })
571
+
572
+ expect(matcher.match('64')).to.be.undefined
573
+
574
+ matcher.match('65').should.deep.equal({
575
+ partialMatch: true
576
+ })
577
+
578
+ matcher.match('650').should.deep.equal({
579
+ match: true
580
+ })
581
+
582
+ expect(matcher.match('654')).to.be.undefined
583
+
584
+ matcher.match('69').should.deep.equal({
585
+ match: true
586
+ })
587
+
588
+ matcher.match('8').should.deep.equal({
589
+ match: true
590
+ })
591
+
592
+ matcher.match('9').should.deep.equal({
593
+ match: true
594
+ })
595
+ })
596
+ })
@@ -12,6 +12,8 @@ import formatCompleteNumber, {
12
12
  canFormatCompleteNumber
13
13
  } from './AsYouTypeFormatter.complete'
14
14
 
15
+ import PatternMatcher from './AsYouTypeFormatter.PatternMatcher'
16
+
15
17
  import parseDigits from './helpers/parseDigits'
16
18
  export { DIGIT_PLACEHOLDER } from './AsYouTypeFormatter.util'
17
19
  import { FIRST_GROUP_PATTERN } from './helpers/formatNationalNumberUsingFormat'
@@ -127,6 +129,12 @@ export default class AsYouTypeFormatter {
127
129
  }
128
130
  }
129
131
 
132
+ /**
133
+ * Formats an updated phone number.
134
+ * @param {string} nextDigits — Additional phone number digits.
135
+ * @param {object} state — `AsYouType` state.
136
+ * @return {[string]} Returns undefined if the updated phone number can't be formatted using any of the available formats.
137
+ */
130
138
  format(nextDigits, state) {
131
139
  // See if the phone number digits can be formatted as a complete phone number.
132
140
  // If not, use the results from `formatNationalNumberWithNextDigits()`,
@@ -268,28 +276,69 @@ export default class AsYouTypeFormatter {
268
276
 
269
277
  formatMatches(format, leadingDigits, leadingDigitsPatternIndex) {
270
278
  const leadingDigitsPatternsCount = format.leadingDigitsPatterns().length
279
+
271
280
  // If this format is not restricted to a certain
272
281
  // leading digits pattern then it fits.
273
282
  if (leadingDigitsPatternsCount === 0) {
274
283
  return true
275
284
  }
276
- // Start excluding any non-matching formats only when the
285
+
286
+ // Start narrowing down the list of possible formats based on the leading digits.
287
+ // (only previously matched formats take part in the narrowing down process)
288
+
289
+ // `leading_digits_patterns` start with 3 digits min
290
+ // and then go up from there one digit at a time.
291
+ leadingDigitsPatternIndex = Math.min(leadingDigitsPatternIndex, leadingDigitsPatternsCount - 1)
292
+ const leadingDigitsPattern = format.leadingDigitsPatterns()[leadingDigitsPatternIndex]
293
+
294
+ // Google imposes a requirement on the leading digits
295
+ // to be minimum 3 digits long in order to be eligible
296
+ // for checking those with a leading digits pattern.
297
+ //
298
+ // Since `leading_digits_patterns` start with 3 digits min,
299
+ // Google's original `libphonenumber` library only starts
300
+ // excluding any non-matching formats only when the
277
301
  // national number entered so far is at least 3 digits long,
278
302
  // otherwise format matching would give false negatives.
303
+ //
279
304
  // For example, when the digits entered so far are `2`
280
305
  // and the leading digits pattern is `21` –
281
306
  // it's quite obvious in this case that the format could be the one
282
307
  // but due to the absence of further digits it would give false negative.
308
+ //
309
+ // Also, `leading_digits_patterns` doesn't always correspond to a single
310
+ // digits count. For example, `60|8` pattern would already match `8`
311
+ // but the `60` part would require having at least two leading digits,
312
+ // so the whole pattern would require inputting two digits first in order to
313
+ // decide on whether it matches the input, even when the input is "80".
314
+ //
315
+ // This library — `libphonenumber-js` — allows filtering by `leading_digits_patterns`
316
+ // even when there's only 1 or 2 digits of the national (significant) number.
317
+ // To do that, it uses a non-strict pattern matcher written specifically for that.
318
+ //
283
319
  if (leadingDigits.length < MIN_LEADING_DIGITS_LENGTH) {
284
- return true
320
+ // Before leading digits < 3 matching was implemented:
321
+ // return true
322
+ //
323
+ // After leading digits < 3 matching was implemented:
324
+ try {
325
+ return new PatternMatcher(leadingDigitsPattern).match(leadingDigits, { allowOverflow: true }) !== undefined
326
+ } catch (error) {
327
+ // There's a slight possibility that there could be some undiscovered bug
328
+ // in the pattern matcher code. Since the "leading digits < 3 matching"
329
+ // feature is not "essential" for operation, it can fall back to the old way
330
+ // in case of any issues rather than halting the application's execution.
331
+ console.error(error)
332
+ return true
333
+ }
285
334
  }
286
- // If at least `MIN_LEADING_DIGITS_LENGTH` digits of a national number are available
287
- // then format matching starts narrowing down the list of possible formats
288
- // (only previously matched formats are considered for next digits).
289
- leadingDigitsPatternIndex = Math.min(leadingDigitsPatternIndex, leadingDigitsPatternsCount - 1)
290
- const leadingDigitsPattern = format.leadingDigitsPatterns()[leadingDigitsPatternIndex]
291
- // Brackets are required for `^` to be applied to
292
- // all or-ed (`|`) parts, not just the first one.
335
+
336
+ // If at least `MIN_LEADING_DIGITS_LENGTH` digits of a national number are
337
+ // available then use the usual regular expression matching.
338
+ //
339
+ // The whole pattern is wrapped in round brackets (`()`) because
340
+ // the pattern can use "or" operator (`|`) at the top level of the pattern.
341
+ //
293
342
  return new RegExp(`^(${leadingDigitsPattern})`).test(leadingDigits)
294
343
  }
295
344
 
@@ -1,4 +1,4 @@
1
- import metadata from '../metadata.min'
1
+ import metadata from '../metadata.min.json'
2
2
  import PhoneNumber from './PhoneNumber'
3
3
 
4
4
  describe('PhoneNumber', () => {
@@ -1,5 +1,5 @@
1
1
  import findNumbers from './findNumbers'
2
- import metadata from '../metadata.full.json'
2
+ import metadata from '../metadata.max.json'
3
3
 
4
4
  describe('findNumbers', () => {
5
5
  it('should find numbers', () => {