-
Notifications
You must be signed in to change notification settings - Fork 54
/
index.d.ts
227 lines (197 loc) · 5.12 KB
/
index.d.ts
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
export type PhoneNumberFormat =
| 'e164'
| 'international'
| 'national'
| 'rfc3966'
| 'significant';
export type PhoneNumberTypes =
| 'fixed-line'
| 'fixed-line-or-mobile'
| 'mobile'
| 'pager'
| 'personal-number'
| 'premium-rate'
| 'shared-cost'
| 'toll-free'
| 'uan'
| 'voip'
| 'unknown';
export type PhoneNumberPossibility =
| 'is-possible'
| 'invalid'
| 'invalid-country-code'
| 'too-long'
| 'too-short'
| 'unknown';
/**
* Parse a phone number into an object describing the number.
*
* @example
* ```ts
* // Using a national phone number format
* parsePhoneNumber( '0707123456', { regionCode: 'SE' } )
* // Using an international (e164) phone number format
* parsePhoneNumber( '+46707123456' )
* ```
*
* The options object is on the form:
* ```ts
* {
* regionCode?: string;
* }
* ```
*
* @param phoneNumber Either an `e164` formatted (international) phone number
* or a _national_ phone number.
* @param options Object of type {@link PhoneNumberParseOptions}.
* @returns A {@link ParsedPhoneNumber}
*/
export function parsePhoneNumber(
phoneNumber: string,
options?: PhoneNumberParseOptions
): ParsedPhoneNumber;
export interface PhoneNumberParseOptions
{
/**
* If the phone number is on national form, this region code specifies the
* region of the phone number, e.g. "SE" for Sweden.
*/
regionCode?: string;
}
export interface ParsedPhoneNumberFull
{
number: {
input: string;
international: string;
national: string;
e164: string;
rfc3966: string;
significant: string;
};
possibility: PhoneNumberPossibility;
regionCode: string;
valid: boolean;
possible: boolean;
shortValid: boolean;
shortPossible: boolean;
canBeInternationallyDialled: boolean;
type: PhoneNumberTypes;
countryCode: number;
typeIsMobile: boolean;
typeIsFixedLine: boolean;
}
export type ParsedPhoneNumberValid =
& Omit< ParsedPhoneNumberFull, 'valid' >
& { valid: true; };
export type ParsedPhoneNumberInvalid =
& Partial< Omit< ParsedPhoneNumberFull, 'valid' | 'possible' | 'possibility' > >
& {
valid: false;
possible: boolean;
possibility: PhoneNumberPossibility;
error?: unknown;
};
export type ParsedPhoneNumber =
| ParsedPhoneNumberValid
| ParsedPhoneNumberInvalid;
export function getCountryCodeForRegionCode( regionCode: string ): number;
export function getRegionCodeForCountryCode( countryCode: number ): string;
export function getSupportedCallingCodes( ): string[ ];
export function getSupportedRegionCodes( ): string[ ];
export type FindNumbersLeniency =
/**
* Phone numbers accepted are possible, but not necessarily valid.
*/
| 'possible'
/**
* Phone numbers accepted are possible and valid.
*/
| 'valid';
export interface FindNumbersOptions
{
/**
* A default region code, to find local (non-e164) formatted phone numbers
*/
defaultRegionCode?: string;
/** Leniency options */
leniency?: FindNumbersLeniency;
/**
* The maximum number of invalid numbers to try before giving up on the text
*/
maxTries?: number;
}
export interface PhoneNumberMatch
{
/** The raw string found */
text: string;
/** The parsed phone number object */
phoneNumber: ParsedPhoneNumber;
/** Start offset of the found number */
start: number;
/** End offset of the found number */
end: number;
}
/**
* Find phone numbers in text.
*
* If the text is expected to have phone numbers for a certain region code,
* the option `defaultRegionCode` can be set. Phone numbers without the
* international prefix `+` will then be found too.
*
* Leniency can be specified using the `leniency` option, in which case
* `maxTries` needs to be set too.
*/
export function findNumbers( text: string, options?: FindNumbersOptions )
: PhoneNumberMatch[ ];
/**
* Get an example phone number, given a region code and a phone number
* {@link PhoneNumberTypes type}.
*
* @param regionCode Region code
* @param type Phone number {@link PhoneNumberTypes type}
*/
export function getExample(
regionCode: string,
type?: PhoneNumberTypes
): ParsedPhoneNumber;
/**
* Get a phonenumber string as it would be called from another country.
*
* @param parsedPhoneNumber A phone number object as returned from {@link parsePhoneNumber `parsePhoneNumber()`}
* @param regionCode Region code of the country to call from
*/
export function getNumberFrom(
parsedPhoneNumber: ParsedPhoneNumberValid,
regionCode?: string
): PhoneNumberFrom;
export type PhoneNumberFrom =
| PhoneNumberFromValid
| PhoneNumberFromInvalid;
export interface PhoneNumberFromValid
{
valid: true;
number: string;
}
export interface PhoneNumberFromInvalid
{
valid: false;
number?: string;
error?: unknown;
}
/**
* Get an instance of the AsYouType class, based on a region code.
*
* @param regionCode The region code to get an AsYouType instance for.
*/
export function getAsYouType( regionCode: string ): AsYouType;
export class AsYouType
{
private constructor( );
addChar( char: string ): string;
number( ): string;
removeChar( ): string;
reset( number?: string ): string;
getPhoneNumber( ): ParsedPhoneNumber;
}
// /** @deprecated use `parsePhoneNumber()` instead */
// export default PhoneNumber;