1 | import Metadata from './metadata'
|
2 | import PhoneNumber from './PhoneNumber'
|
3 | import AsYouTypeState from './AsYouTypeState'
|
4 | import AsYouTypeFormatter, { DIGIT_PLACEHOLDER } from './AsYouTypeFormatter'
|
5 | import AsYouTypeParser, { extractFormattedDigitsAndPlus } from './AsYouTypeParser'
|
6 | import getCountryByCallingCode from './helpers/getCountryByCallingCode'
|
7 |
|
8 | const USE_NON_GEOGRAPHIC_COUNTRY_CODE = false
|
9 |
|
10 | export default class AsYouType {
|
11 | |
12 |
|
13 |
|
14 |
|
15 | constructor(optionsOrDefaultCountry, metadata) {
|
16 | this.metadata = new Metadata(metadata)
|
17 | const [defaultCountry, defaultCallingCode] = this.getCountryAndCallingCode(optionsOrDefaultCountry)
|
18 | this.defaultCountry = defaultCountry
|
19 | this.defaultCallingCode = defaultCallingCode
|
20 | this.reset()
|
21 | }
|
22 |
|
23 | getCountryAndCallingCode(optionsOrDefaultCountry) {
|
24 |
|
25 | let defaultCountry
|
26 | let defaultCallingCode
|
27 |
|
28 | if (optionsOrDefaultCountry) {
|
29 | if (typeof optionsOrDefaultCountry === 'object') {
|
30 | defaultCountry = optionsOrDefaultCountry.defaultCountry
|
31 | defaultCallingCode = optionsOrDefaultCountry.defaultCallingCode
|
32 | } else {
|
33 | defaultCountry = optionsOrDefaultCountry
|
34 | }
|
35 | }
|
36 | if (defaultCountry && !this.metadata.hasCountry(defaultCountry)) {
|
37 | defaultCountry = undefined
|
38 | }
|
39 | if (defaultCallingCode) {
|
40 |
|
41 | if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
|
42 | if (this.metadata.isNonGeographicCallingCode(defaultCallingCode)) {
|
43 | defaultCountry = '001'
|
44 | }
|
45 | }
|
46 | }
|
47 | return [defaultCountry, defaultCallingCode]
|
48 | }
|
49 |
|
50 | |
51 |
|
52 |
|
53 |
|
54 |
|
55 | input(text) {
|
56 | const {
|
57 | digits,
|
58 | justLeadingPlus
|
59 | } = this.parser.input(text, this.state)
|
60 | if (justLeadingPlus) {
|
61 | this.formattedOutput = '+'
|
62 | } else if (digits) {
|
63 | this.determineTheCountryIfNeeded()
|
64 |
|
65 | if (this.state.nationalSignificantNumber) {
|
66 | this.formatter.narrowDownMatchingFormats(this.state)
|
67 | }
|
68 | let formattedNationalNumber
|
69 | if (this.metadata.hasSelectedNumberingPlan()) {
|
70 | formattedNationalNumber = this.formatter.format(digits, this.state)
|
71 | }
|
72 | if (formattedNationalNumber === undefined) {
|
73 |
|
74 | if (this.parser.reExtractNationalSignificantNumber(this.state)) {
|
75 | this.determineTheCountryIfNeeded()
|
76 |
|
77 | const nationalDigits = this.state.getNationalDigits()
|
78 | if (nationalDigits) {
|
79 | formattedNationalNumber = this.formatter.format(nationalDigits, this.state)
|
80 | }
|
81 | }
|
82 | }
|
83 | this.formattedOutput = formattedNationalNumber
|
84 | ? this.getFullNumber(formattedNationalNumber)
|
85 | : this.getNonFormattedNumber()
|
86 | }
|
87 | return this.formattedOutput
|
88 | }
|
89 |
|
90 | reset() {
|
91 | this.state = new AsYouTypeState({
|
92 | onCountryChange: (country) => {
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | this.country = country
|
99 | },
|
100 | onCallingCodeChange: (country, callingCode) => {
|
101 | this.metadata.selectNumberingPlan(country, callingCode)
|
102 | this.formatter.reset(this.metadata.numberingPlan, this.state)
|
103 | this.parser.reset(this.metadata.numberingPlan)
|
104 | }
|
105 | })
|
106 | this.formatter = new AsYouTypeFormatter({
|
107 | state: this.state,
|
108 | metadata: this.metadata
|
109 | })
|
110 | this.parser = new AsYouTypeParser({
|
111 | defaultCountry: this.defaultCountry,
|
112 | defaultCallingCode: this.defaultCallingCode,
|
113 | metadata: this.metadata,
|
114 | state: this.state,
|
115 | onNationalSignificantNumberChange: () => {
|
116 | this.determineTheCountryIfNeeded()
|
117 | this.formatter.reset(this.metadata.numberingPlan, this.state)
|
118 | }
|
119 | })
|
120 | this.state.reset(this.defaultCountry, this.defaultCallingCode)
|
121 | this.formattedOutput = ''
|
122 | return this
|
123 | }
|
124 |
|
125 | |
126 |
|
127 |
|
128 |
|
129 |
|
130 | isInternational() {
|
131 | return this.state.international
|
132 | }
|
133 |
|
134 | |
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | getCallingCode() {
|
141 | return this.state.callingCode
|
142 | }
|
143 |
|
144 |
|
145 | getCountryCallingCode() {
|
146 | return this.getCallingCode()
|
147 | }
|
148 |
|
149 | |
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | getCountry() {
|
156 | const { digits, country } = this.state
|
157 |
|
158 |
|
159 |
|
160 | if (!digits) {
|
161 | return
|
162 | }
|
163 | let countryCode = country
|
164 |
|
165 | if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
|
166 |
|
167 |
|
168 | if (countryCode === '001') {
|
169 | countryCode = undefined
|
170 | }
|
171 | }
|
172 | return countryCode
|
173 | }
|
174 |
|
175 | determineTheCountryIfNeeded() {
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 | if (!this.state.country || this.isCountryCallingCodeAmbiguous()) {
|
193 | this.determineTheCountry()
|
194 | }
|
195 | }
|
196 |
|
197 |
|
198 | getFullNumber(formattedNationalNumber) {
|
199 | if (this.isInternational()) {
|
200 | const prefix = (text) => this.formatter.getInternationalPrefixBeforeCountryCallingCode(this.state, {
|
201 | spacing: text ? true : false
|
202 | }) + text
|
203 | const { callingCode } = this.state
|
204 | if (!callingCode) {
|
205 | return prefix(`${this.state.getDigitsWithoutInternationalPrefix()}`)
|
206 | }
|
207 | if (!formattedNationalNumber) {
|
208 | return prefix(callingCode)
|
209 | }
|
210 | return prefix(`${callingCode} ${formattedNationalNumber}`)
|
211 | }
|
212 | return formattedNationalNumber
|
213 | }
|
214 |
|
215 | getNonFormattedNationalNumberWithPrefix() {
|
216 | const {
|
217 | nationalSignificantNumber,
|
218 | complexPrefixBeforeNationalSignificantNumber,
|
219 | nationalPrefix
|
220 | } = this.state
|
221 | let number = nationalSignificantNumber
|
222 | const prefix = complexPrefixBeforeNationalSignificantNumber || nationalPrefix
|
223 | if (prefix) {
|
224 | number = prefix + number
|
225 | }
|
226 | return number
|
227 | }
|
228 |
|
229 | getNonFormattedNumber() {
|
230 | const { nationalSignificantNumberMatchesInput } = this.state
|
231 | return this.getFullNumber(
|
232 | nationalSignificantNumberMatchesInput
|
233 | ? this.getNonFormattedNationalNumberWithPrefix()
|
234 | : this.state.getNationalDigits()
|
235 | )
|
236 | }
|
237 |
|
238 | getNonFormattedTemplate() {
|
239 | const number = this.getNonFormattedNumber()
|
240 | if (number) {
|
241 | return number.replace(/[\+\d]/g, DIGIT_PLACEHOLDER)
|
242 | }
|
243 | }
|
244 |
|
245 | isCountryCallingCodeAmbiguous() {
|
246 | const { callingCode } = this.state
|
247 | const countryCodes = this.metadata.getCountryCodesForCallingCode(callingCode)
|
248 | return countryCodes && countryCodes.length > 1
|
249 | }
|
250 |
|
251 |
|
252 |
|
253 |
|
254 | determineTheCountry() {
|
255 | this.state.setCountry(getCountryByCallingCode(
|
256 | this.isInternational() ? this.state.callingCode : this.defaultCallingCode,
|
257 | this.state.nationalSignificantNumber,
|
258 | this.metadata
|
259 | ))
|
260 | }
|
261 |
|
262 | |
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 | getNumber() {
|
269 | let {
|
270 | nationalSignificantNumber,
|
271 | carrierCode
|
272 | } = this.state
|
273 | if (this.isInternational()) {
|
274 | if (!this.state.callingCode) {
|
275 | return
|
276 | }
|
277 | } else {
|
278 | if (!this.state.country && !this.defaultCallingCode) {
|
279 | return
|
280 | }
|
281 | }
|
282 | if (!nationalSignificantNumber) {
|
283 | return
|
284 | }
|
285 | const countryCode = this.getCountry()
|
286 | const callingCode = this.getCountryCallingCode() || this.defaultCallingCode
|
287 | const phoneNumber = new PhoneNumber(
|
288 | countryCode || callingCode,
|
289 | nationalSignificantNumber,
|
290 | this.metadata.metadata
|
291 | )
|
292 | if (carrierCode) {
|
293 | phoneNumber.carrierCode = carrierCode
|
294 | }
|
295 |
|
296 | return phoneNumber
|
297 | }
|
298 |
|
299 | |
300 |
|
301 |
|
302 |
|
303 |
|
304 | isPossible() {
|
305 | const phoneNumber = this.getNumber()
|
306 | if (!phoneNumber) {
|
307 | return false
|
308 | }
|
309 | return phoneNumber.isPossible()
|
310 | }
|
311 |
|
312 | |
313 |
|
314 |
|
315 |
|
316 |
|
317 | isValid() {
|
318 | const phoneNumber = this.getNumber()
|
319 | if (!phoneNumber) {
|
320 | return false
|
321 | }
|
322 | return phoneNumber.isValid()
|
323 | }
|
324 |
|
325 | |
326 |
|
327 |
|
328 |
|
329 |
|
330 | getNationalNumber() {
|
331 | return this.state.nationalSignificantNumber
|
332 | }
|
333 |
|
334 | |
335 |
|
336 |
|
337 |
|
338 | getChars() {
|
339 | return (this.state.international ? '+' : '') + this.state.digits
|
340 | }
|
341 |
|
342 | |
343 |
|
344 |
|
345 |
|
346 | getTemplate() {
|
347 | return this.formatter.getTemplate(this.state) || this.getNonFormattedTemplate() || ''
|
348 | }
|
349 | } |
\ | No newline at end of file |