UNPKG

4.82 kBTypeScriptView Raw
1// The rationale for having a separate `CountryCode` type instead of just a `string`:
2// https://github.com/catamphetamine/libphonenumber-js/issues/170#issuecomment-363156068
3export type CountryCode = 'AC' | 'AD' | 'AE' | 'AF' | 'AG' | 'AI' | 'AL' | 'AM' | 'AO' | 'AR' | 'AS' | 'AT' | 'AU' | 'AW' | 'AX' | 'AZ' | 'BA' | 'BB' | 'BD' | 'BE' | 'BF' | 'BG' | 'BH' | 'BI' | 'BJ' | 'BL' | 'BM' | 'BN' | 'BO' | 'BQ' | 'BR' | 'BS' | 'BT' | 'BW' | 'BY' | 'BZ' | 'CA' | 'CC' | 'CD' | 'CF' | 'CG' | 'CH' | 'CI' | 'CK' | 'CL' | 'CM' | 'CN' | 'CO' | 'CR' | 'CU' | 'CV' | 'CW' | 'CX' | 'CY' | 'CZ' | 'DE' | 'DJ' | 'DK' | 'DM' | 'DO' | 'DZ' | 'EC' | 'EE' | 'EG' | 'EH' | 'ER' | 'ES' | 'ET' | 'FI' | 'FJ' | 'FK' | 'FM' | 'FO' | 'FR' | 'GA' | 'GB' | 'GD' | 'GE' | 'GF' | 'GG' | 'GH' | 'GI' | 'GL' | 'GM' | 'GN' | 'GP' | 'GQ' | 'GR' | 'GT' | 'GU' | 'GW' | 'GY' | 'HK' | 'HN' | 'HR' | 'HT' | 'HU' | 'ID' | 'IE' | 'IL' | 'IM' | 'IN' | 'IO' | 'IQ' | 'IR' | 'IS' | 'IT' | 'JE' | 'JM' | 'JO' | 'JP' | 'KE' | 'KG' | 'KH' | 'KI' | 'KM' | 'KN' | 'KP' | 'KR' | 'KW' | 'KY' | 'KZ' | 'LA' | 'LB' | 'LC' | 'LI' | 'LK' | 'LR' | 'LS' | 'LT' | 'LU' | 'LV' | 'LY' | 'MA' | 'MC' | 'MD' | 'ME' | 'MF' | 'MG' | 'MH' | 'MK' | 'ML' | 'MM' | 'MN' | 'MO' | 'MP' | 'MQ' | 'MR' | 'MS' | 'MT' | 'MU' | 'MV' | 'MW' | 'MX' | 'MY' | 'MZ' | 'NA' | 'NC' | 'NE' | 'NF' | 'NG' | 'NI' | 'NL' | 'NO' | 'NP' | 'NR' | 'NU' | 'NZ' | 'OM' | 'PA' | 'PE' | 'PF' | 'PG' | 'PH' | 'PK' | 'PL' | 'PM' | 'PR' | 'PS' | 'PT' | 'PW' | 'PY' | 'QA' | 'RE' | 'RO' | 'RS' | 'RU' | 'RW' | 'SA' | 'SB' | 'SC' | 'SD' | 'SE' | 'SG' | 'SH' | 'SI' | 'SJ' | 'SK' | 'SL' | 'SM' | 'SN' | 'SO' | 'SR' | 'SS' | 'ST' | 'SV' | 'SX' | 'SY' | 'SZ' | 'TA' | 'TC' | 'TD' | 'TG' | 'TH' | 'TJ' | 'TK' | 'TL' | 'TM' | 'TN' | 'TO' | 'TR' | 'TT' | 'TV' | 'TW' | 'TZ' | 'UA' | 'UG' | 'US' | 'UY' | 'UZ' | 'VA' | 'VC' | 'VE' | 'VG' | 'VI' | 'VN' | 'VU' | 'WF' | 'WS' | 'XK' | 'YE' | 'YT' | 'ZA' | 'ZM' | 'ZW';
4
5export type CountryCallingCodes = {
6 [countryCallingCode: string]: CountryCode[];
7};
8
9export type Countries = {
10 // Metadata here is a compressed one,
11 // so a country's data is just an array of some properties
12 // instead of a JSON object of shape:
13 // {
14 // phone_code: string,
15 // idd_prefix: string,
16 // national_number_pattern: string,
17 // types: object,
18 // examples: object,
19 // formats: object[]?,
20 // possible_lengths: number[],
21 // ...
22 // }
23 [country in CountryCode]: any[];
24};
25
26export type Metadata = {
27 country_calling_codes: CountryCallingCodes;
28 countries: Countries;
29};
30
31// 'National' and 'International' formats are deprecated.
32export type NumberFormat = 'NATIONAL' | 'National' | 'INTERNATIONAL' | 'International' | 'E.164' | 'RFC3966' | 'IDD';
33export type NumberType = undefined | 'PREMIUM_RATE' | 'TOLL_FREE' | 'SHARED_COST' | 'VOIP' | 'PERSONAL_NUMBER' | 'PAGER' | 'UAN' | 'VOICEMAIL' | 'FIXED_LINE_OR_MOBILE' | 'FIXED_LINE' | 'MOBILE';
34
35// The rationale for the specific data types instead of just using `string`.
36// https://github.com/catamphetamine/libphonenumber-js/issues/170#issuecomment-363156068
37export interface E164Number extends String { }
38export interface NationalNumber extends String { }
39export interface Extension extends String { }
40export interface CarrierCode extends String { }
41export interface CountryCallingCode extends String { }
42
43type FormatExtension = (formattedNumber: string, extension: Extension, metadata: Metadata) => string
44
45type FormatNumberOptionsWithoutIDD = {
46 v2?: boolean;
47 formatExtension?: FormatExtension;
48};
49
50type FormatNumberOptions = {
51 v2?: boolean;
52 fromCountry?: CountryCode;
53 humanReadable?: boolean;
54 nationalPrefix?: boolean;
55 formatExtension?: FormatExtension;
56};
57
58export class PhoneNumber {
59 constructor(countryCallingCodeOrCountry: CountryCallingCode | CountryCode, nationalNumber: NationalNumber, metadata: Metadata);
60 countryCallingCode: CountryCallingCode;
61 country?: CountryCode;
62 nationalNumber: NationalNumber;
63 number: E164Number;
64 carrierCode?: CarrierCode;
65 ext?: Extension;
66 isPossible(): boolean;
67 isValid(): boolean;
68 getType(): NumberType;
69 format(format: NumberFormat, options?: FormatNumberOptions): string;
70 formatNational(options?: FormatNumberOptionsWithoutIDD): string;
71 formatInternational(options?: FormatNumberOptionsWithoutIDD): string;
72 getURI(options?: FormatNumberOptionsWithoutIDD): string;
73 isNonGeographic(): boolean;
74 isEqual(phoneNumber: PhoneNumber): boolean;
75}
76
77export interface NumberFound {
78 number: PhoneNumber,
79 startsAt: number,
80 endsAt: number
81}
82
83export interface NumberFoundLegacy {
84 country: CountryCode,
85 phone: NationalNumber,
86 ext?: Extension,
87 startsAt: number,
88 endsAt: number
89}
90
91export class ParseError {
92 message: string;
93}