-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
phonenumberutil.js
4658 lines (4294 loc) · 180 KB
/
phonenumberutil.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @license
* Copyright (C) 2010 The Libphonenumber Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Utility for international phone numbers.
* Functionality includes formatting, parsing and validation.
* (based on the java implementation).
*
* NOTE: A lot of methods in this class require Region Code strings. These must
* be provided using CLDR two-letter region-code format. These should be in
* upper-case. The list of the codes can be found here:
* http://www.unicode.org/cldr/charts/30/supplemental/territory_information.html
*/
goog.provide('i18n.phonenumbers.Error');
goog.provide('i18n.phonenumbers.PhoneNumberFormat');
goog.provide('i18n.phonenumbers.PhoneNumberType');
goog.provide('i18n.phonenumbers.PhoneNumberUtil');
goog.provide('i18n.phonenumbers.PhoneNumberUtil.MatchType');
goog.provide('i18n.phonenumbers.PhoneNumberUtil.ValidationResult');
goog.require('goog.object');
goog.require('goog.proto2.PbLiteSerializer');
goog.require('goog.string');
goog.require('goog.string.StringBuffer');
goog.require('i18n.phonenumbers.NumberFormat');
goog.require('i18n.phonenumbers.PhoneMetadata');
goog.require('i18n.phonenumbers.PhoneNumber');
goog.require('i18n.phonenumbers.PhoneNumber.CountryCodeSource');
goog.require('i18n.phonenumbers.PhoneNumberDesc');
goog.require('i18n.phonenumbers.metadata');
/**
* @constructor
* @private
*/
i18n.phonenumbers.PhoneNumberUtil = function() {
/**
* A mapping from a region code to the PhoneMetadata for that region.
* @type {Object.<string, i18n.phonenumbers.PhoneMetadata>}
*/
this.regionToMetadataMap = {};
};
goog.addSingletonGetter(i18n.phonenumbers.PhoneNumberUtil);
/**
* Errors encountered when parsing phone numbers.
*
* @enum {string}
*/
i18n.phonenumbers.Error = {
INVALID_COUNTRY_CODE: 'Invalid country calling code',
// This generally indicates the string passed in had less than 3 digits in it.
// More specifically, the number failed to match the regular expression
// VALID_PHONE_NUMBER.
NOT_A_NUMBER: 'The string supplied did not seem to be a phone number',
// This indicates the string started with an international dialing prefix, but
// after this was stripped from the number, had less digits than any valid
// phone number (including country calling code) could have.
TOO_SHORT_AFTER_IDD: 'Phone number too short after IDD',
// This indicates the string, after any country calling code has been
// stripped, had less digits than any valid phone number could have.
TOO_SHORT_NSN: 'The string supplied is too short to be a phone number',
// This indicates the string had more digits than any valid phone number could
// have.
TOO_LONG: 'The string supplied is too long to be a phone number'
};
/**
* @const
* @type {number}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.NANPA_COUNTRY_CODE_ = 1;
/**
* The minimum length of the national significant number.
*
* @const
* @type {number}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.MIN_LENGTH_FOR_NSN_ = 2;
/**
* The ITU says the maximum length should be 15, but we have found longer
* numbers in Germany.
*
* @const
* @type {number}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.MAX_LENGTH_FOR_NSN_ = 17;
/**
* The maximum length of the country calling code.
*
* @const
* @type {number}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.MAX_LENGTH_COUNTRY_CODE_ = 3;
/**
* We don't allow input strings for parsing to be longer than 250 chars. This
* prevents malicious input from consuming CPU.
*
* @const
* @type {number}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.MAX_INPUT_STRING_LENGTH_ = 250;
/**
* Region-code for the unknown region.
*
* @const
* @type {string}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.UNKNOWN_REGION_ = 'ZZ';
/**
* The prefix that needs to be inserted in front of a Colombian landline number
* when dialed from a mobile phone in Colombia.
*
* @const
* @type {string}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.COLOMBIA_MOBILE_TO_FIXED_LINE_PREFIX_ = '3';
/**
* Map of country calling codes that use a mobile token before the area code.
* One example of when this is relevant is when determining the length of the
* national destination code, which should be the length of the area code plus
* the length of the mobile token.
*
* @const
* @type {!Object.<number, string>}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.MOBILE_TOKEN_MAPPINGS_ = {
54: '9'
};
/**
* Set of country calling codes that have geographically assigned mobile
* numbers. This may not be complete; we add calling codes case by case, as we
* find geographical mobile numbers or hear from user reports.
*
* @const
* @type {!Array.<number>}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.GEO_MOBILE_COUNTRIES_ = [
52, // Mexico
54, // Argentina
55 // Brazil
];
/**
* The PLUS_SIGN signifies the international prefix.
*
* @const
* @type {string}
*/
i18n.phonenumbers.PhoneNumberUtil.PLUS_SIGN = '+';
/**
* @const
* @type {string}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.STAR_SIGN_ = '*';
/**
* The RFC 3966 format for extensions.
*
* @const
* @type {string}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.RFC3966_EXTN_PREFIX_ = ';ext=';
/**
* @const
* @type {string}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.RFC3966_PREFIX_ = 'tel:';
/**
* @const
* @type {string}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.RFC3966_PHONE_CONTEXT_ = ';phone-context=';
/**
* @const
* @type {string}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.RFC3966_ISDN_SUBADDRESS_ = ';isub=';
/**
* These mappings map a character (key) to a specific digit that should replace
* it for normalization purposes. Non-European digits that may be used in phone
* numbers are mapped to a European equivalent.
*
* @const
* @type {!Object.<string, string>}
*/
i18n.phonenumbers.PhoneNumberUtil.DIGIT_MAPPINGS = {
'0': '0',
'1': '1',
'2': '2',
'3': '3',
'4': '4',
'5': '5',
'6': '6',
'7': '7',
'8': '8',
'9': '9',
'\uFF10': '0', // Fullwidth digit 0
'\uFF11': '1', // Fullwidth digit 1
'\uFF12': '2', // Fullwidth digit 2
'\uFF13': '3', // Fullwidth digit 3
'\uFF14': '4', // Fullwidth digit 4
'\uFF15': '5', // Fullwidth digit 5
'\uFF16': '6', // Fullwidth digit 6
'\uFF17': '7', // Fullwidth digit 7
'\uFF18': '8', // Fullwidth digit 8
'\uFF19': '9', // Fullwidth digit 9
'\u0660': '0', // Arabic-indic digit 0
'\u0661': '1', // Arabic-indic digit 1
'\u0662': '2', // Arabic-indic digit 2
'\u0663': '3', // Arabic-indic digit 3
'\u0664': '4', // Arabic-indic digit 4
'\u0665': '5', // Arabic-indic digit 5
'\u0666': '6', // Arabic-indic digit 6
'\u0667': '7', // Arabic-indic digit 7
'\u0668': '8', // Arabic-indic digit 8
'\u0669': '9', // Arabic-indic digit 9
'\u06F0': '0', // Eastern-Arabic digit 0
'\u06F1': '1', // Eastern-Arabic digit 1
'\u06F2': '2', // Eastern-Arabic digit 2
'\u06F3': '3', // Eastern-Arabic digit 3
'\u06F4': '4', // Eastern-Arabic digit 4
'\u06F5': '5', // Eastern-Arabic digit 5
'\u06F6': '6', // Eastern-Arabic digit 6
'\u06F7': '7', // Eastern-Arabic digit 7
'\u06F8': '8', // Eastern-Arabic digit 8
'\u06F9': '9' // Eastern-Arabic digit 9
};
/**
* A map that contains characters that are essential when dialling. That means
* any of the characters in this map must not be removed from a number when
* dialling, otherwise the call will not reach the intended destination.
*
* @const
* @type {!Object.<string, string>}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.DIALLABLE_CHAR_MAPPINGS_ = {
'0': '0',
'1': '1',
'2': '2',
'3': '3',
'4': '4',
'5': '5',
'6': '6',
'7': '7',
'8': '8',
'9': '9',
'+': i18n.phonenumbers.PhoneNumberUtil.PLUS_SIGN,
'*': '*',
'#': '#'
};
/**
* Only upper-case variants of alpha characters are stored.
*
* @const
* @type {!Object.<string, string>}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.ALPHA_MAPPINGS_ = {
'A': '2',
'B': '2',
'C': '2',
'D': '3',
'E': '3',
'F': '3',
'G': '4',
'H': '4',
'I': '4',
'J': '5',
'K': '5',
'L': '5',
'M': '6',
'N': '6',
'O': '6',
'P': '7',
'Q': '7',
'R': '7',
'S': '7',
'T': '8',
'U': '8',
'V': '8',
'W': '9',
'X': '9',
'Y': '9',
'Z': '9'
};
/**
* For performance reasons, amalgamate both into one map.
*
* @const
* @type {!Object.<string, string>}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.ALL_NORMALIZATION_MAPPINGS_ = {
'0': '0',
'1': '1',
'2': '2',
'3': '3',
'4': '4',
'5': '5',
'6': '6',
'7': '7',
'8': '8',
'9': '9',
'\uFF10': '0', // Fullwidth digit 0
'\uFF11': '1', // Fullwidth digit 1
'\uFF12': '2', // Fullwidth digit 2
'\uFF13': '3', // Fullwidth digit 3
'\uFF14': '4', // Fullwidth digit 4
'\uFF15': '5', // Fullwidth digit 5
'\uFF16': '6', // Fullwidth digit 6
'\uFF17': '7', // Fullwidth digit 7
'\uFF18': '8', // Fullwidth digit 8
'\uFF19': '9', // Fullwidth digit 9
'\u0660': '0', // Arabic-indic digit 0
'\u0661': '1', // Arabic-indic digit 1
'\u0662': '2', // Arabic-indic digit 2
'\u0663': '3', // Arabic-indic digit 3
'\u0664': '4', // Arabic-indic digit 4
'\u0665': '5', // Arabic-indic digit 5
'\u0666': '6', // Arabic-indic digit 6
'\u0667': '7', // Arabic-indic digit 7
'\u0668': '8', // Arabic-indic digit 8
'\u0669': '9', // Arabic-indic digit 9
'\u06F0': '0', // Eastern-Arabic digit 0
'\u06F1': '1', // Eastern-Arabic digit 1
'\u06F2': '2', // Eastern-Arabic digit 2
'\u06F3': '3', // Eastern-Arabic digit 3
'\u06F4': '4', // Eastern-Arabic digit 4
'\u06F5': '5', // Eastern-Arabic digit 5
'\u06F6': '6', // Eastern-Arabic digit 6
'\u06F7': '7', // Eastern-Arabic digit 7
'\u06F8': '8', // Eastern-Arabic digit 8
'\u06F9': '9', // Eastern-Arabic digit 9
'A': '2',
'B': '2',
'C': '2',
'D': '3',
'E': '3',
'F': '3',
'G': '4',
'H': '4',
'I': '4',
'J': '5',
'K': '5',
'L': '5',
'M': '6',
'N': '6',
'O': '6',
'P': '7',
'Q': '7',
'R': '7',
'S': '7',
'T': '8',
'U': '8',
'V': '8',
'W': '9',
'X': '9',
'Y': '9',
'Z': '9'
};
/**
* Separate map of all symbols that we wish to retain when formatting alpha
* numbers. This includes digits, ASCII letters and number grouping symbols such
* as '-' and ' '.
*
* @const
* @type {!Object.<string, string>}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.ALL_PLUS_NUMBER_GROUPING_SYMBOLS_ = {
'0': '0',
'1': '1',
'2': '2',
'3': '3',
'4': '4',
'5': '5',
'6': '6',
'7': '7',
'8': '8',
'9': '9',
'A': 'A',
'B': 'B',
'C': 'C',
'D': 'D',
'E': 'E',
'F': 'F',
'G': 'G',
'H': 'H',
'I': 'I',
'J': 'J',
'K': 'K',
'L': 'L',
'M': 'M',
'N': 'N',
'O': 'O',
'P': 'P',
'Q': 'Q',
'R': 'R',
'S': 'S',
'T': 'T',
'U': 'U',
'V': 'V',
'W': 'W',
'X': 'X',
'Y': 'Y',
'Z': 'Z',
'a': 'A',
'b': 'B',
'c': 'C',
'd': 'D',
'e': 'E',
'f': 'F',
'g': 'G',
'h': 'H',
'i': 'I',
'j': 'J',
'k': 'K',
'l': 'L',
'm': 'M',
'n': 'N',
'o': 'O',
'p': 'P',
'q': 'Q',
'r': 'R',
's': 'S',
't': 'T',
'u': 'U',
'v': 'V',
'w': 'W',
'x': 'X',
'y': 'Y',
'z': 'Z',
'-': '-',
'\uFF0D': '-',
'\u2010': '-',
'\u2011': '-',
'\u2012': '-',
'\u2013': '-',
'\u2014': '-',
'\u2015': '-',
'\u2212': '-',
'/': '/',
'\uFF0F': '/',
' ': ' ',
'\u3000': ' ',
'\u2060': ' ',
'.': '.',
'\uFF0E': '.'
};
/**
* Pattern that makes it easy to distinguish whether a region has a single
* international dialing prefix or not. If a region has a single international
* prefix (e.g. 011 in USA), it will be represented as a string that contains
* a sequence of ASCII digits, and possibly a tilde, which signals waiting for
* the tone. If there are multiple available international prefixes in a
* region, they will be represented as a regex string that always contains one
* or more characters that are not ASCII digits or a tilde.
*
* @const
* @type {!RegExp}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.SINGLE_INTERNATIONAL_PREFIX_ =
/[\d]+(?:[~\u2053\u223C\uFF5E][\d]+)?/;
/**
* Regular expression of acceptable punctuation found in phone numbers, used to
* find numbers in text and to decide what is a viable phone number. This
* excludes diallable characters.
* This consists of dash characters, white space characters, full stops,
* slashes, square brackets, parentheses and tildes. It also includes the letter
* 'x' as that is found as a placeholder for carrier information in some phone
* numbers. Full-width variants are also present.
*
* @const
* @type {string}
*/
i18n.phonenumbers.PhoneNumberUtil.VALID_PUNCTUATION =
'-x\u2010-\u2015\u2212\u30FC\uFF0D-\uFF0F \u00A0\u00AD\u200B\u2060\u3000' +
'()\uFF08\uFF09\uFF3B\uFF3D.\\[\\]/~\u2053\u223C\uFF5E';
/**
* Digits accepted in phone numbers (ascii, fullwidth, arabic-indic, and eastern
* arabic digits).
*
* @const
* @type {string}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.VALID_DIGITS_ =
'0-9\uFF10-\uFF19\u0660-\u0669\u06F0-\u06F9';
/**
* We accept alpha characters in phone numbers, ASCII only, upper and lower
* case.
*
* @const
* @type {string}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.VALID_ALPHA_ = 'A-Za-z';
/**
* @const
* @type {string}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.PLUS_CHARS_ = '+\uFF0B';
/**
* @const
* @type {!RegExp}
*/
i18n.phonenumbers.PhoneNumberUtil.PLUS_CHARS_PATTERN =
new RegExp('[' + i18n.phonenumbers.PhoneNumberUtil.PLUS_CHARS_ + ']+');
/**
* @const
* @type {!RegExp}
* @package
*/
i18n.phonenumbers.PhoneNumberUtil.LEADING_PLUS_CHARS_PATTERN =
new RegExp('^[' + i18n.phonenumbers.PhoneNumberUtil.PLUS_CHARS_ + ']+');
/**
* @const
* @type {string}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.SEPARATOR_PATTERN_ =
'[' + i18n.phonenumbers.PhoneNumberUtil.VALID_PUNCTUATION + ']+';
/**
* @const
* @type {!RegExp}
*/
i18n.phonenumbers.PhoneNumberUtil.CAPTURING_DIGIT_PATTERN =
new RegExp('([' + i18n.phonenumbers.PhoneNumberUtil.VALID_DIGITS_ + '])');
/**
* Regular expression of acceptable characters that may start a phone number for
* the purposes of parsing. This allows us to strip away meaningless prefixes to
* phone numbers that may be mistakenly given to us. This consists of digits,
* the plus symbol and arabic-indic digits. This does not contain alpha
* characters, although they may be used later in the number. It also does not
* include other punctuation, as this will be stripped later during parsing and
* is of no information value when parsing a number.
*
* @const
* @type {!RegExp}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.VALID_START_CHAR_PATTERN_ =
new RegExp('[' + i18n.phonenumbers.PhoneNumberUtil.PLUS_CHARS_ +
i18n.phonenumbers.PhoneNumberUtil.VALID_DIGITS_ + ']');
/**
* Regular expression of characters typically used to start a second phone
* number for the purposes of parsing. This allows us to strip off parts of the
* number that are actually the start of another number, such as for:
* (530) 583-6985 x302/x2303 -> the second extension here makes this actually
* two phone numbers, (530) 583-6985 x302 and (530) 583-6985 x2303. We remove
* the second extension so that the first number is parsed correctly.
*
* @const
* @type {!RegExp}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.SECOND_NUMBER_START_PATTERN_ = /[\\\/] *x/;
/**
* Regular expression of trailing characters that we want to remove. We remove
* all characters that are not alpha or numerical characters. The hash character
* is retained here, as it may signify the previous block was an extension.
*
* @const
* @type {!RegExp}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.UNWANTED_END_CHAR_PATTERN_ =
new RegExp('[^' + i18n.phonenumbers.PhoneNumberUtil.VALID_DIGITS_ +
i18n.phonenumbers.PhoneNumberUtil.VALID_ALPHA_ + '#]+$');
/**
* We use this pattern to check if the phone number has at least three letters
* in it - if so, then we treat it as a number where some phone-number digits
* are represented by letters.
*
* @const
* @type {!RegExp}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.VALID_ALPHA_PHONE_PATTERN_ =
/(?:.*?[A-Za-z]){3}.*/;
/**
* Regular expression of viable phone numbers. This is location independent.
* Checks we have at least three leading digits, and only valid punctuation,
* alpha characters and digits in the phone number. Does not include extension
* data. The symbol 'x' is allowed here as valid punctuation since it is often
* used as a placeholder for carrier codes, for example in Brazilian phone
* numbers. We also allow multiple '+' characters at the start.
* Corresponds to the following:
* [digits]{minLengthNsn}|
* plus_sign*
* (([punctuation]|[star])*[digits]){3,}([punctuation]|[star]|[digits]|[alpha])*
*
* The first reg-ex is to allow short numbers (two digits long) to be parsed if
* they are entered as "15" etc, but only if there is no punctuation in them.
* The second expression restricts the number of digits to three or more, but
* then allows them to be in international form, and to have alpha-characters
* and punctuation. We split up the two reg-exes here and combine them when
* creating the reg-ex VALID_PHONE_NUMBER_PATTERN_ itself so we can prefix it
* with ^ and append $ to each branch.
*
* Note VALID_PUNCTUATION starts with a -, so must be the first in the range.
*
* @const
* @type {string}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.MIN_LENGTH_PHONE_NUMBER_PATTERN_ =
'[' + i18n.phonenumbers.PhoneNumberUtil.VALID_DIGITS_ + ']{' +
i18n.phonenumbers.PhoneNumberUtil.MIN_LENGTH_FOR_NSN_ + '}';
/**
* See MIN_LENGTH_PHONE_NUMBER_PATTERN_ for a full description of this reg-exp.
*
* @const
* @type {string}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.VALID_PHONE_NUMBER_ =
'[' + i18n.phonenumbers.PhoneNumberUtil.PLUS_CHARS_ + ']*(?:[' +
i18n.phonenumbers.PhoneNumberUtil.VALID_PUNCTUATION +
i18n.phonenumbers.PhoneNumberUtil.STAR_SIGN_ + ']*[' +
i18n.phonenumbers.PhoneNumberUtil.VALID_DIGITS_ + ']){3,}[' +
i18n.phonenumbers.PhoneNumberUtil.VALID_PUNCTUATION +
i18n.phonenumbers.PhoneNumberUtil.STAR_SIGN_ +
i18n.phonenumbers.PhoneNumberUtil.VALID_ALPHA_ +
i18n.phonenumbers.PhoneNumberUtil.VALID_DIGITS_ + ']*';
/**
* Default extension prefix to use when formatting. This will be put in front of
* any extension component of the number, after the main national number is
* formatted. For example, if you wish the default extension formatting to be
* ' extn: 3456', then you should specify ' extn: ' here as the default
* extension prefix. This can be overridden by region-specific preferences.
*
* @const
* @type {string}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.DEFAULT_EXTN_PREFIX_ = ' ext. ';
/**
* Helper method for constructing regular expressions for parsing. Creates
* an expression that captures up to max_length digits.
*
* @return {string} RegEx pattern to capture extension digits.
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.extnDigits_ =
function(maxLength) {
return ('([' + i18n.phonenumbers.PhoneNumberUtil.VALID_DIGITS_ + ']'
+ '{1,' + maxLength + '})');
};
/**
* Helper initialiser method to create the regular-expression pattern to match
* extensions.
*
* @return {string} RegEx pattern to capture extensions.
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.createExtnPattern_ =
function() {
// We cap the maximum length of an extension based on the ambiguity of the way
// the extension is prefixed. As per ITU, the officially allowed length for
// extensions is actually 40, but we don't support this since we haven't seen real
// examples and this introduces many false interpretations as the extension labels
// are not standardized.
/** @type {string} */
var extLimitAfterExplicitLabel = '20';
/** @type {string} */
var extLimitAfterLikelyLabel = '15';
/** @type {string} */
var extLimitAfterAmbiguousChar = '9';
/** @type {string} */
var extLimitWhenNotSure = '6';
/** @type {string} */
var possibleSeparatorsBetweenNumberAndExtLabel = "[ \u00A0\\t,]*";
// Optional full stop (.) or colon, followed by zero or more spaces/tabs/commas.
/** @type {string} */
var possibleCharsAfterExtLabel = "[:\\.\uFF0E]?[ \u00A0\\t,-]*";
/** @type {string} */
var optionalExtnSuffix = "#?";
// Here the extension is called out in more explicit way, i.e mentioning it obvious
// patterns like "ext.".
/** @type {string} */
var explicitExtLabels =
"(?:e?xt(?:ensi(?:o\u0301?|\u00F3))?n?|\uFF45?\uFF58\uFF54\uFF4E?|\u0434\u043E\u0431|anexo)";
// One-character symbols that can be used to indicate an extension, and less
// commonly used or more ambiguous extension labels.
/** @type {string} */
var ambiguousExtLabels = "(?:[x\uFF58#\uFF03~\uFF5E]|int|\uFF49\uFF4E\uFF54)";
// When extension is not separated clearly.
/** @type {string} */
var ambiguousSeparator = "[- ]+";
// This is the same as possibleSeparatorsBetweenNumberAndExtLabel, but not matching
// comma as extension label may have it.
/** @type {string} */
var possibleSeparatorsNumberExtLabelNoComma = "[ \u00A0\\t]*";
// ",," is commonly used for auto dialling the extension when connected. First
// comma is matched through possibleSeparatorsBetweenNumberAndExtLabel, so we do
// not repeat it here. Semi-colon works in Iphone and Android also to pop up a
// button with the extension number following.
/** @type {string} */
var autoDiallingAndExtLabelsFound = "(?:,{2}|;)";
/** @type {string} */
var rfcExtn = i18n.phonenumbers.PhoneNumberUtil.RFC3966_EXTN_PREFIX_
+ i18n.phonenumbers.PhoneNumberUtil.extnDigits_(extLimitAfterExplicitLabel);
/** @type {string} */
var explicitExtn = possibleSeparatorsBetweenNumberAndExtLabel + explicitExtLabels
+ possibleCharsAfterExtLabel
+ i18n.phonenumbers.PhoneNumberUtil.extnDigits_(extLimitAfterExplicitLabel)
+ optionalExtnSuffix;
/** @type {string} */
var ambiguousExtn = possibleSeparatorsBetweenNumberAndExtLabel + ambiguousExtLabels
+ possibleCharsAfterExtLabel
+ i18n.phonenumbers.PhoneNumberUtil.extnDigits_(extLimitAfterAmbiguousChar)
+ optionalExtnSuffix;
/** @type {string} */
var americanStyleExtnWithSuffix = ambiguousSeparator
+ i18n.phonenumbers.PhoneNumberUtil.extnDigits_(extLimitWhenNotSure) + "#";
/** @type {string} */
var autoDiallingExtn = possibleSeparatorsNumberExtLabelNoComma
+ autoDiallingAndExtLabelsFound + possibleCharsAfterExtLabel
+ i18n.phonenumbers.PhoneNumberUtil.extnDigits_(extLimitAfterLikelyLabel)
+ optionalExtnSuffix;
/** @type {string} */
var onlyCommasExtn = possibleSeparatorsNumberExtLabelNoComma
+ "(?:,)+" + possibleCharsAfterExtLabel
+ i18n.phonenumbers.PhoneNumberUtil.extnDigits_(extLimitAfterAmbiguousChar)
+ optionalExtnSuffix;
// The first regular expression covers RFC 3966 format, where the extension is added
// using ";ext=". The second more generic where extension is mentioned with explicit
// labels like "ext:". In both the above cases we allow more numbers in extension than
// any other extension labels. The third one captures when single character extension
// labels or less commonly used labels are used. In such cases we capture fewer
// extension digits in order to reduce the chance of falsely interpreting two
// numbers beside each other as a number + extension. The fourth one covers the
// special case of American numbers where the extension is written with a hash
// at the end, such as "- 503#". The fifth one is exclusively for extension
// autodialling formats which are used when dialling and in this case we accept longer
// extensions. The last one is more liberal on the number of commas that acts as
// extension labels, so we have a strict cap on the number of digits in such extensions.
return rfcExtn + "|"
+ explicitExtn + "|"
+ ambiguousExtn + "|"
+ americanStyleExtnWithSuffix + "|"
+ autoDiallingExtn + "|"
+ onlyCommasExtn;
};
/**
* Regexp of all known extension prefixes used by different regions followed by
* 1 or more valid digits, for use when parsing.
*
* @const
* @type {!RegExp}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.EXTN_PATTERN_ =
new RegExp('(?:' +
i18n.phonenumbers.PhoneNumberUtil.createExtnPattern_() +
')$', 'i');
/**
* We append optionally the extension pattern to the end here, as a valid phone
* number may have an extension prefix appended, followed by 1 or more digits.
*
* @const
* @type {!RegExp}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.VALID_PHONE_NUMBER_PATTERN_ =
new RegExp(
'^' +
i18n.phonenumbers.PhoneNumberUtil.MIN_LENGTH_PHONE_NUMBER_PATTERN_ +
'$|' +
'^' + i18n.phonenumbers.PhoneNumberUtil.VALID_PHONE_NUMBER_ +
'(?:' + i18n.phonenumbers.PhoneNumberUtil.createExtnPattern_() +
')?' + '$', 'i');
/**
* @const
* @type {!RegExp}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.NON_DIGITS_PATTERN_ = /\D+/;
/**
* This was originally set to $1 but there are some countries for which the
* first group is not used in the national pattern (e.g. Argentina) so the $1
* group does not match correctly. Therefore, we use \d, so that the first
* group actually used in the pattern will be matched.
* @const
* @type {!RegExp}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.FIRST_GROUP_PATTERN_ = /(\$\d)/;
/**
* @const
* @type {!RegExp}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.NP_PATTERN_ = /\$NP/;
/**
* @const
* @type {!RegExp}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.FG_PATTERN_ = /\$FG/;
/**
* @const
* @type {!RegExp}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.CC_PATTERN_ = /\$CC/;
/**
* A pattern that is used to determine if the national prefix formatting rule
* has the first group only, i.e., does not start with the national prefix.
* Note that the pattern explicitly allows for unbalanced parentheses.
* @const
* @type {!RegExp}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.FIRST_GROUP_ONLY_PREFIX_PATTERN_ =
/^\(?\$1\)?$/;
/**
* @const
* @type {string}
*/
i18n.phonenumbers.PhoneNumberUtil.REGION_CODE_FOR_NON_GEO_ENTITY = '001';
/**
* INTERNATIONAL and NATIONAL formats are consistent with the definition in
* ITU-T Recommendation E123. However we follow local conventions such as
* using '-' instead of whitespace as separators. For example, the number of the
* Google Switzerland office will be written as '+41 44 668 1800' in
* INTERNATIONAL format, and as '044 668 1800' in NATIONAL format. E164 format
* is as per INTERNATIONAL format but with no formatting applied, e.g.
* '+41446681800'. RFC3966 is as per INTERNATIONAL format, but with all spaces
* and other separating symbols replaced with a hyphen, and with any phone
* number extension appended with ';ext='. It also will have a prefix of 'tel:'
* added, e.g. 'tel:+41-44-668-1800'.
*
* Note: If you are considering storing the number in a neutral format, you are
* highly advised to use the PhoneNumber class.
* @enum {number}
*/
i18n.phonenumbers.PhoneNumberFormat = {
E164: 0,
INTERNATIONAL: 1,
NATIONAL: 2,
RFC3966: 3
};
/**
* Type of phone numbers.
*
* @enum {number}
*/
i18n.phonenumbers.PhoneNumberType = {
FIXED_LINE: 0,
MOBILE: 1,
// In some regions (e.g. the USA), it is impossible to distinguish between
// fixed-line and mobile numbers by looking at the phone number itself.
FIXED_LINE_OR_MOBILE: 2,
// Freephone lines
TOLL_FREE: 3,
PREMIUM_RATE: 4,
// The cost of this call is shared between the caller and the recipient, and
// is hence typically less than PREMIUM_RATE calls. See
// http://en.wikipedia.org/wiki/Shared_Cost_Service for more information.
SHARED_COST: 5,
// Voice over IP numbers. This includes TSoIP (Telephony Service over IP).
VOIP: 6,
// A personal number is associated with a particular person, and may be routed