UNPKG

21.6 kBTypeScriptView Raw
1declare namespace libphonenumber {
2 export enum PhoneNumberFormat {
3 E164,
4 INTERNATIONAL,
5 NATIONAL,
6 RFC3966,
7 }
8
9 export enum PhoneNumberType {
10 FIXED_LINE = 0,
11 MOBILE = 1,
12 FIXED_LINE_OR_MOBILE = 2,
13 TOLL_FREE = 3,
14 PREMIUM_RATE = 4,
15 SHARED_COST = 5,
16 VOIP = 6,
17 PERSONAL_NUMBER = 7,
18 PAGER = 8,
19 UAN = 9,
20 VOICEMAIL = 10,
21 UNKNOWN = -1,
22 }
23
24 export namespace PhoneNumber {
25 export enum CountryCodeSource {
26 FROM_NUMBER_WITH_PLUS_SIGN = 1,
27 FROM_NUMBER_WITH_IDD = 5,
28 FROM_NUMBER_WITHOUT_PLUS_SIGN = 10,
29 FROM_DEFAULT_COUNTRY = 20,
30 }
31 }
32
33 export class PhoneNumber {
34 getCountryCode(): number | undefined;
35 getCountryCodeOrDefault(): number;
36 setCountryCode(value: number): void;
37 hasCountryCode(): boolean;
38 countryCodeCount(): number;
39 clearCountryCode(): void;
40 getNationalNumber(): number | undefined;
41 getNationalNumberOrDefault(): number;
42 setNationalNumber(value: number): number;
43 hasNationalNumber(): boolean;
44 nationalNumberCount(): number;
45 clearNationalNumber(): void;
46
47 getExtension(): string | undefined;
48 getExtensionOrDefault(): string;
49 setExtension(value: string): void;
50 hasExtension(): boolean;
51 extensionCount(): number;
52 clearExtension(): void;
53
54 getItalianLeadingZero(): boolean | undefined;
55 getItalianLeadingZeroOrDefault(): boolean;
56 setItalianLeadingZero(value: boolean): void;
57 hasItalianLeadingZero(): boolean;
58 italianLeadingZeroCount(): number;
59 clearItalianLeadingZero(): void;
60
61 getNumberOfLeadingZeros(): number | undefined;
62 getNumberOfLeadingZerosOrDefault(): number;
63 setNumberOfLeadingZeros(value: number): void;
64 hasNumberOfLeadingZeros(): boolean;
65 numberOfLeadingZerosCount(): number;
66 clearNumberOfLeadingZeros(): void;
67
68 getRawInput(): string | undefined;
69 getRawInputOrDefault(): string;
70 setRawInput(value: string): void;
71 hasRawInput(): boolean;
72 rawInputCount(): number;
73 clearRawInput(): void;
74
75 getCountryCodeSource(): PhoneNumber.CountryCodeSource | undefined;
76 getCountryCodeSourceOrDefault(): PhoneNumber.CountryCodeSource;
77 setCountryCodeSource(value: PhoneNumber.CountryCodeSource): void;
78 hasCountryCodeSource(): boolean;
79 countryCodeSourceCount(): number;
80 clearCountryCodeSource(): void;
81
82 getPreferredDomesticCarrierCode(): string | undefined;
83 getPreferredDomesticCarrierCodeOrDefault(): string;
84 setPreferredDomesticCarrierCode(value: string): void;
85 hasPreferredDomesticCarrierCode(): boolean;
86 preferredDomesticCarrierCodeCount(): number;
87 clearPreferredDomesticCarrierCode(): void;
88 }
89
90 export namespace PhoneNumberUtil {
91 export enum ValidationResult {
92 /** The number length matches that of valid numbers for this region. =0 */
93 IS_POSSIBLE,
94 /** The number has an invalid country calling code. =1 */
95 INVALID_COUNTRY_CODE,
96 /** The number is shorter than all valid numbers for this region. =2 */
97 TOO_SHORT,
98 /** The number is longer than all valid numbers for this region. =3 */
99 TOO_LONG,
100 /**
101 * The number length matches that of local numbers for this region only (i.e.
102 * numbers that may be able to be dialled within an area, but do not have all
103 * the information to be dialled from anywhere inside or outside the country).
104 * =4
105 */
106 IS_POSSIBLE_LOCAL_ONLY,
107 /**
108 * The number is longer than the shortest valid numbers for this region,
109 * shorter than the longest valid numbers for this region, and does not itself
110 * have a number length that matches valid numbers for this region.
111 * This can also be returned in the case where
112 * isPossibleNumberForTypeWithReason was called, and there are no numbers of
113 * this type at all for this region.
114 * =5
115 */
116 INVALID_LENGTH,
117 }
118
119 export enum MatchType {
120 EXACT_MATCH,
121 NO_MATCH,
122 NOT_A_NUMBER,
123 NSN_MATCH,
124 SHORT_NSN_MATCH,
125 }
126 }
127
128 class StringBuffer {
129 constructor(opt_a1?: any, ...var_args: any[]);
130 append(a1: any, opt_a2?: any, ...var_args: any[]): StringBuffer;
131 toString(): string;
132 }
133
134 export class PhoneNumberUtil {
135 static getInstance(): PhoneNumberUtil;
136 extractCountryCode(fullNumber: StringBuffer, nationalNumber: StringBuffer): number;
137 format(phoneNumber: PhoneNumber, format: PhoneNumberFormat): string;
138 formatInOriginalFormat(phoneNumber: PhoneNumber, regionDialingFrom?: string): string;
139 formatOutOfCountryCallingNumber(phoneNumber: PhoneNumber, regionDialingFrom?: string): string;
140 getNddPrefixForRegion(regionCode?: string, stripNonDigits?: boolean): string | undefined;
141 getNumberType(phoneNumber: PhoneNumber): PhoneNumberType;
142 getCountryCodeForRegion(supportedRegion: string): number;
143 getExampleNumber(regionCode: string): PhoneNumber;
144 getExampleNumberForType(regionCode: string, type: PhoneNumberType): PhoneNumber;
145 getRegionCodeForCountryCode(countryCallingCode: number): RegionCode | RegionCodeUnknown;
146 getRegionCodeForNumber(phoneNumber: PhoneNumber): RegionCode | undefined;
147 getSupportedRegions(): RegionCode[];
148 isAlphaNumber(number: string): boolean;
149 isLeadingZeroPossible(countryCallingCode: number): boolean;
150 isNANPACountry(regionCode?: string): boolean;
151 isPossibleNumber(number: PhoneNumber): boolean;
152 isPossibleNumber(phoneNumber: PhoneNumber): boolean;
153 isPossibleNumberForType(number: PhoneNumber, type: PhoneNumberType): boolean;
154 isPossibleNumberForTypeWithReason(number: PhoneNumber, type: PhoneNumberType): PhoneNumberUtil.ValidationResult;
155 isPossibleNumberString(number: string, regionDialingFrom: string): boolean;
156 isPossibleNumberWithReason(number: PhoneNumber): PhoneNumberUtil.ValidationResult;
157 isPossibleNumberWithReason(phoneNumber: PhoneNumber): PhoneNumberUtil.ValidationResult;
158 isValidNumber(phoneNumber: PhoneNumber): boolean;
159 isValidNumberForRegion(phoneNumber: PhoneNumber, region?: string): boolean;
160 parse(number?: string, region?: string): PhoneNumber;
161 parseAndKeepRawInput(number: string, regionCode?: string): PhoneNumber;
162 truncateTooLongNumber(number: PhoneNumber): boolean;
163 isNumberMatch(firstNumber: string | PhoneNumber, secondNumber: string | PhoneNumber): PhoneNumberUtil.MatchType;
164 getLengthOfGeographicalAreaCode(number: PhoneNumber): number;
165 getNationalSignificantNumber(number: PhoneNumber): string;
166 getLengthOfNationalDestinationCode(number: PhoneNumber): number;
167 }
168
169 export class AsYouTypeFormatter {
170 constructor(region: string);
171 inputDigit(digit: string): string;
172 clear(): void;
173 }
174
175 export namespace ShortNumberInfo {
176 /** Cost categories of short numbers. */
177 export enum ShortNumberCost {
178 TOLL_FREE = 0,
179 STANDARD_RATE = 1,
180 PREMIUM_RATE = 2,
181 UNKNOWN_COST = 3,
182 }
183 }
184
185 export class ShortNumberInfo {
186 /** Returns the singleton instance of the ShortNumberInfo. */
187 static getInstance(): ShortNumberInfo;
188
189 /**
190 * Check whether a short number is a possible number when dialed from the given region. This
191 * provides a more lenient check than {@link ShortNumberInfo.isValidShortNumberForRegion}.
192 *
193 * @param number the short number to check
194 * @param regionDialingFrom the region from which the number is dialed
195 * @return whether the number is a possible short number
196 */
197 isPossibleShortNumberForRegion(number: PhoneNumber, regionDialingFrom: RegionCode): boolean;
198
199 /**
200 * Check whether a short number is a possible number. If a country calling code is shared by
201 * multiple regions, this returns true if it's possible in any of them. This provides a more
202 * lenient check than {@link ShortNumberInfo.isValidShortNumber}.
203 * See {@link ShortNumberInfo.isPossibleShortNumberForRegion} for details.
204 *
205 * @param number the short number to check
206 * @return whether the number is a possible short number
207 */
208 isPossibleShortNumber(number: PhoneNumber): boolean;
209
210 /**
211 * Tests whether a short number matches a valid pattern in a region. Note that this doesn't verify
212 * the number is actually in use, which is impossible to tell by just looking at the number
213 * itself.
214 *
215 * @param number the short number for which we want to test the validity
216 * @param regionDialingFrom the region from which the number is dialed
217 * @return whether the short number matches a valid pattern
218 */
219 isValidShortNumberForRegion(number: PhoneNumber, regionDialingFrom: RegionCode): boolean;
220
221 /**
222 * Tests whether a short number matches a valid pattern. If a country calling code is shared by
223 * multiple regions, this returns true if it's valid in any of them. Note that this doesn't verify
224 * the number is actually in use, which is impossible to tell by just looking at the number
225 * itself. See {@link ShortNumberInfo.isValidShortNumberForRegion} for details.
226 *
227 * @param number the short number for which we want to test the validity
228 * @return whether the short number matches a valid pattern
229 */
230 isValidShortNumber(number: PhoneNumber): boolean;
231
232 /**
233 * Gets the expected cost category of a short number when dialed from a region (however, nothing
234 * is implied about its validity). If it is important that the number is valid, then its validity
235 * must first be checked using {@link ShortNumberInfo.isValidShortNumberForRegion}. Note that emergency numbers
236 * are always considered toll-free.
237 *
238 * @example Usage:
239 * ```
240 * // The region for which the number was parsed and the region we subsequently check against
241 * // need not be the same. Here we parse the number in the US and check it for Canada.
242 * const phoneNumber = phoneUtil.parse("110", "US");
243 * const regionCode: RegionCode = '"CA"';
244 * const shortInfo = ShortNumberInfo.getInstance();
245 * if (shortInfo.isValidShortNumberForRegion(phoneNumber, regionCode)) {
246 * const cost = shortInfo.getExpectedCostForRegion(phoneNumber, regionCode);
247 * // Do something with the cost information here.
248 * }
249 * ```
250 *
251 * @param number the short number for which we want to know the expected cost category
252 * @param regionDialingFrom the region from which the number is dialed
253 * @return the expected cost category for that region of the short number. Returns UNKNOWN_COST
254 * the number does not match a cost category. Note that an invalid number may match any cost
255 * category.
256 */
257 getExpectedCostForRegion(number: PhoneNumber, regionDialingFrom: RegionCode): ShortNumberInfo.ShortNumberCost;
258
259 /**
260 * Gets the expected cost category of a short number (however, nothing is implied about its
261 * validity). If the country calling code is unique to a region, this method behaves exactly the
262 * same as {@link ShortNumberInfo.getExpectedCostForRegion}. However, if the country
263 * calling code is shared by multiple regions, then it returns the highest cost in the sequence
264 * PREMIUM_RATE, UNKNOWN_COST, STANDARD_RATE, TOLL_FREE. The reason for the position of
265 * UNKNOWN_COST in this order is that if a number is UNKNOWN_COST in one region but STANDARD_RATE
266 * or TOLL_FREE in another, its expected cost cannot be estimated as one of the latter since it
267 * might be a PREMIUM_RATE number.
268 *
269 * For example, if a number is STANDARD_RATE in the US, but TOLL_FREE in Canada, the expected
270 * cost returned by this method will be STANDARD_RATE, since the NANPA countries share the same
271 * country calling code.
272 *
273 * Note: If the region from which the number is dialed is known, it is highly preferable to call
274 * {@link ShortNumberInfo.getExpectedCostForRegion} instead.
275 *
276 * @param number the short number for which we want to know the expected cost category
277 * @return the highest expected cost category of the short number in the region(s) with the given
278 * country calling code
279 */
280 getExpectedCost(number: PhoneNumber): ShortNumberInfo.ShortNumberCost;
281
282 /**
283 * Returns true if the given number, exactly as dialed, might be used to connect to an emergency
284 * service in the given region.
285 *
286 * This method accepts a string, rather than a PhoneNumber, because it needs to distinguish
287 * cases such as "+1 911" and "911", where the former may not connect to an emergency service in
288 * all cases but the latter would. This method takes into account cases where the number might
289 * contain formatting, or might have additional digits appended (when it is okay to do that in
290 * the specified region).
291 *
292 * @param number the phone number to test
293 * @param regionCode the region where the phone number is being dialed
294 * @return whether the number might be used to connect to an emergency service in the given region
295 */
296 connectsToEmergencyNumber(number: string, regionDialing: RegionCode): boolean;
297
298 /**
299 * Returns true if the given number exactly matches an emergency service number in the given
300 * region.
301 *
302 * This method takes into account cases where the number might contain formatting, but doesn't
303 * allow additional digits to be appended. Note that `isEmergencyNumber(number, region)`
304 * implies `connectsToEmergencyNumber(number, region)`.
305 *
306 * @param number the phone number to test
307 * @param regionCode the region where the phone number is being dialed
308 * @return whether the number exactly matches an emergency services number in the given region
309 */
310 isEmergencyNumber(number: string, regionCode: RegionCode): boolean;
311
312 /**
313 * Given a valid short number, determines whether it is carrier-specific (however, nothing is
314 * implied about its validity). Carrier-specific numbers may connect to a different end-point, or
315 * not connect at all, depending on the user's carrier. If it is important that the number is
316 * valid, then its validity must first be checked using {@link ShortNumberInfo.isValidShortNumber} or
317 * {@link ShortNumberInfo.isValidShortNumberForRegion}.
318 *
319 * @param number the valid short number to check
320 * @return whether the short number is carrier-specific, assuming the input was a valid short
321 * number
322 */
323 isCarrierSpecific(number: PhoneNumber): boolean;
324
325 /**
326 * Given a valid short number, determines whether it is carrier-specific when dialed from the
327 * given region (however, nothing is implied about its validity). Carrier-specific numbers may
328 * connect to a different end-point, or not connect at all, depending on the user's carrier. If
329 * it is important that the number is valid, then its validity must first be checked using
330 * {@link ShortNumberInfo.isValidShortNumber} or {@link ShortNumberInfo.isValidShortNumberForRegion}.
331 * Returns false if the number doesn't match the region provided.
332 *
333 * @param number the valid short number to check
334 * @param regionDialingFrom the region from which the number is dialed
335 * @return whether the short number is carrier-specific in the provided region, assuming the
336 * input was a valid short number
337 */
338 isCarrierSpecificForRegion(number: PhoneNumber, regionDialingFrom: RegionCode): boolean;
339
340 /**
341 * Given a valid short number, determines whether it is an SMS service (however, nothing is
342 * implied about its validity). An SMS service is where the primary or only intended usage is to
343 * receive and/or send text messages (SMSs). This includes MMS as MMS numbers downgrade to SMS if
344 * the other party isn't MMS-capable. If it is important that the number is valid, then its
345 * validity must first be checked using {@link ShortNumberInfo.isValidShortNumber} or
346 * {@link ShortNumberInfo.isValidShortNumberForRegion}.
347 * Returns false if the number doesn't match the region provided.
348 *
349 * @param number the valid short number to check
350 * @param regionDialingFrom the region from which the number is dialed
351 * @return whether the short number is an SMS service in the provided region, assuming the input
352 * was a valid short number
353 */
354 isSmsServiceForRegion(number: PhoneNumber, regionDialingFrom: string): boolean;
355 }
356
357 export type RegionCodeUnknown = "ZZ";
358 export type RegionCode =
359 | "AC"
360 | "AD"
361 | "AE"
362 | "AF"
363 | "AG"
364 | "AI"
365 | "AL"
366 | "AM"
367 | "AO"
368 | "AR"
369 | "AS"
370 | "AT"
371 | "AU"
372 | "AW"
373 | "AX"
374 | "AZ"
375 | "BA"
376 | "BB"
377 | "BD"
378 | "BE"
379 | "BF"
380 | "BG"
381 | "BH"
382 | "BI"
383 | "BJ"
384 | "BL"
385 | "BM"
386 | "BN"
387 | "BO"
388 | "BQ"
389 | "BR"
390 | "BS"
391 | "BT"
392 | "BW"
393 | "BY"
394 | "BZ"
395 | "CA"
396 | "CC"
397 | "CD"
398 | "CF"
399 | "CG"
400 | "CH"
401 | "CI"
402 | "CK"
403 | "CL"
404 | "CM"
405 | "CN"
406 | "CO"
407 | "CR"
408 | "CU"
409 | "CV"
410 | "CW"
411 | "CX"
412 | "CY"
413 | "CZ"
414 | "DE"
415 | "DJ"
416 | "DK"
417 | "DM"
418 | "DO"
419 | "DZ"
420 | "EC"
421 | "EE"
422 | "EG"
423 | "EH"
424 | "ER"
425 | "ES"
426 | "ET"
427 | "FI"
428 | "FJ"
429 | "FK"
430 | "FM"
431 | "FO"
432 | "FR"
433 | "GA"
434 | "GB"
435 | "GD"
436 | "GE"
437 | "GF"
438 | "GG"
439 | "GH"
440 | "GI"
441 | "GL"
442 | "GM"
443 | "GN"
444 | "GP"
445 | "GQ"
446 | "GR"
447 | "GT"
448 | "GU"
449 | "GW"
450 | "GY"
451 | "HK"
452 | "HN"
453 | "HR"
454 | "HT"
455 | "HU"
456 | "ID"
457 | "IE"
458 | "IL"
459 | "IM"
460 | "IN"
461 | "IO"
462 | "IQ"
463 | "IR"
464 | "IS"
465 | "IT"
466 | "JE"
467 | "JM"
468 | "JO"
469 | "JP"
470 | "KE"
471 | "KG"
472 | "KH"
473 | "KI"
474 | "KM"
475 | "KN"
476 | "KP"
477 | "KR"
478 | "KW"
479 | "KY"
480 | "KZ"
481 | "LA"
482 | "LB"
483 | "LC"
484 | "LI"
485 | "LK"
486 | "LR"
487 | "LS"
488 | "LT"
489 | "LU"
490 | "LV"
491 | "LY"
492 | "MA"
493 | "MC"
494 | "MD"
495 | "ME"
496 | "MF"
497 | "MG"
498 | "MH"
499 | "MK"
500 | "ML"
501 | "MM"
502 | "MN"
503 | "MO"
504 | "MP"
505 | "MQ"
506 | "MR"
507 | "MS"
508 | "MT"
509 | "MU"
510 | "MV"
511 | "MW"
512 | "MX"
513 | "MY"
514 | "MZ"
515 | "NA"
516 | "NC"
517 | "NE"
518 | "NF"
519 | "NG"
520 | "NI"
521 | "NL"
522 | "NO"
523 | "NP"
524 | "NR"
525 | "NU"
526 | "NZ"
527 | "OM"
528 | "PA"
529 | "PE"
530 | "PF"
531 | "PG"
532 | "PH"
533 | "PK"
534 | "PL"
535 | "PM"
536 | "PR"
537 | "PS"
538 | "PT"
539 | "PW"
540 | "PY"
541 | "QA"
542 | "RE"
543 | "RO"
544 | "RS"
545 | "RU"
546 | "RW"
547 | "SA"
548 | "SB"
549 | "SC"
550 | "SD"
551 | "SE"
552 | "SG"
553 | "SH"
554 | "SI"
555 | "SJ"
556 | "SK"
557 | "SL"
558 | "SM"
559 | "SN"
560 | "SO"
561 | "SR"
562 | "SS"
563 | "ST"
564 | "SV"
565 | "SX"
566 | "SY"
567 | "SZ"
568 | "TA"
569 | "TC"
570 | "TD"
571 | "TG"
572 | "TH"
573 | "TJ"
574 | "TK"
575 | "TL"
576 | "TM"
577 | "TN"
578 | "TO"
579 | "TR"
580 | "TT"
581 | "TV"
582 | "TW"
583 | "TZ"
584 | "UA"
585 | "UG"
586 | "US"
587 | "UY"
588 | "UZ"
589 | "VA"
590 | "VC"
591 | "VE"
592 | "VG"
593 | "VI"
594 | "VN"
595 | "VU"
596 | "WF"
597 | "WS"
598 | "XK"
599 | "YE"
600 | "YT"
601 | "ZA"
602 | "ZM"
603 | "ZW";
604}
605
606declare module "google-libphonenumber" {
607 export = libphonenumber;
608}