15815213711
2024-08-26 67b8b6731811983447e053d4396b3708c14dfe3c
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
import metadata from '../../metadata.min.json' assert { type: 'json' }
import _parseNumber from './parse.js'
import Metadata from '../metadata.js'
 
function parseNumber(...parameters) {
    parameters.push(metadata)
    return _parseNumber.apply(this, parameters)
}
 
const USE_NON_GEOGRAPHIC_COUNTRY_CODE = false
 
describe('parse', () => {
    it('should not parse invalid phone numbers', () => {
        // Too short.
        parseNumber('+7 (800) 55-35-35').should.deep.equal({})
        // Too long.
        parseNumber('+7 (800) 55-35-35-55').should.deep.equal({})
 
        parseNumber('+7 (800) 55-35-35', 'US').should.deep.equal({})
        parseNumber('(800) 55 35 35', { defaultCountry: 'RU' }).should.deep.equal({})
        parseNumber('+1 187 215 5230', 'US').should.deep.equal({})
 
        parseNumber('911231231', 'BE').should.deep.equal({})
    })
 
    it('should parse valid phone numbers', () => {
        // Instant loans
        // https://www.youtube.com/watch?v=6e1pMrYH5jI
        //
        // Restrict to RU
        parseNumber('Phone: 8 (800) 555 35 35.', 'RU').should.deep.equal({ country: 'RU', phone: '8005553535' })
        // International format
        parseNumber('Phone: +7 (800) 555-35-35.').should.deep.equal({ country: 'RU', phone: '8005553535' })
        // // Restrict to US, but not a US country phone code supplied
        // parseNumber('+7 (800) 555-35-35', 'US').should.deep.equal({})
        // Restrict to RU
        parseNumber('(800) 555 35 35', 'RU').should.deep.equal({ country: 'RU', phone: '8005553535' })
        // Default to RU
        parseNumber('8 (800) 555 35 35', { defaultCountry: 'RU' }).should.deep.equal({ country: 'RU', phone: '8005553535' })
 
        // Gangster partyline
        parseNumber('+1-213-373-4253').should.deep.equal({ country: 'US', phone: '2133734253' })
 
        // Switzerland (just in case)
        parseNumber('044 668 18 00', 'CH').should.deep.equal({ country: 'CH', phone: '446681800' })
 
        // China, Beijing
        parseNumber('010-852644821', 'CN').should.deep.equal({ country: 'CN', phone: '10852644821' })
 
        // France
        parseNumber('+33169454850').should.deep.equal({ country: 'FR', phone: '169454850' })
 
        // UK (Jersey)
        parseNumber('+44 7700 300000').should.deep.equal({ country: 'JE', phone: '7700300000' })
 
        // KZ
        parseNumber('+7 702 211 1111').should.deep.equal({ country: 'KZ', phone: '7022111111' })
 
        // Brazil
        parseNumber('11987654321', 'BR').should.deep.equal({ country: 'BR', phone: '11987654321' })
 
        // Long country phone code.
        parseNumber('+212659777777').should.deep.equal({ country: 'MA', phone: '659777777' })
 
        // No country could be derived.
        // parseNumber('+212569887076').should.deep.equal({ countryPhoneCode: '212', phone: '569887076' })
 
        // GB. Moible numbers starting 07624* are Isle of Man.
        parseNumber('07624369230', 'GB').should.deep.equal({ country: 'IM', phone: '7624369230' })
    })
 
    it('should parse possible numbers', () => {
        // Invalid phone number for a given country.
        parseNumber('1112223344', 'RU', { extended: true }).should.deep.equal({
            country            : 'RU',
            countryCallingCode : '7',
            phone              : '1112223344',
            carrierCode        : undefined,
            ext                : undefined,
            valid              : false,
            possible           : true
        })
 
        // International phone number.
        // Several countries with the same country phone code.
        parseNumber('+71112223344').should.deep.equal({})
        parseNumber('+71112223344', { extended: true }).should.deep.equal({
            country            : undefined,
            countryCallingCode : '7',
            phone              : '1112223344',
            carrierCode        : undefined,
            ext                : undefined,
            valid              : false,
            possible           : true
        })
 
        // International phone number.
        // Single country with the given country phone code.
        parseNumber('+33011222333', { extended: true }).should.deep.equal({
            country            : 'FR',
            countryCallingCode : '33',
            phone              : '011222333',
            carrierCode        : undefined,
            ext                : undefined,
            valid              : false,
            possible           : true
        })
 
        // Too short.
        // Won't strip national prefix `8` because otherwise the number would be too short.
        parseNumber('+7 (800) 55-35-35', { extended: true }).should.deep.equal({
            country            : 'RU',
            countryCallingCode : '7',
            phone              : '800553535',
            carrierCode        : undefined,
            ext                : undefined,
            valid              : false,
            possible           : false
        })
 
        // Too long.
        parseNumber('+1 213 37342530', { extended: true }).should.deep.equal({
            country            : undefined,
            countryCallingCode : '1',
            phone              : '21337342530',
            carrierCode        : undefined,
            ext                : undefined,
            valid              : false,
            possible           : false
        })
 
        // No national number to be parsed.
        parseNumber('+996', { extended: true }).should.deep.equal({
            // countryCallingCode : '996'
        })
 
        // Valid number.
        parseNumber('+78005553535', { extended: true }).should.deep.equal({
            country            : 'RU',
            countryCallingCode : '7',
            phone              : '8005553535',
            carrierCode        : undefined,
            ext                : undefined,
            valid              : true,
            possible           : true
        })
 
        // https://github.com/catamphetamine/libphonenumber-js/issues/211
        parseNumber('+966', { extended: true }).should.deep.equal({})
        parseNumber('+9664', { extended: true }).should.deep.equal({})
        parseNumber('+96645', { extended: true }).should.deep.equal({
            carrierCode        : undefined,
            phone              : '45',
            ext                : undefined,
            country            : 'SA',
            countryCallingCode : '966',
            possible           : false,
            valid              : false
        })
    })
 
    it('should parse non-European digits', () => {
        parseNumber('+١٢١٢٢٣٢٣٢٣٢').should.deep.equal({ country: 'US', phone: '2122323232' })
    })
 
    it('should work in edge cases', () => {
        let thrower
 
        // No input
        parseNumber('').should.deep.equal({})
 
        // No country phone code
        parseNumber('+').should.deep.equal({})
 
        // No country at all (non international number and no explicit country code)
        parseNumber('123').should.deep.equal({})
 
        // No country metadata for this `require` country code
        thrower = () => parseNumber('123', 'ZZ')
        thrower.should.throw('Unknown country')
 
        // No country metadata for this `default` country code
        thrower = () => parseNumber('123', { defaultCountry: 'ZZ' })
        thrower.should.throw('Unknown country')
 
        // Invalid country phone code
        parseNumber('+210').should.deep.equal({})
 
        // Invalid country phone code (extended parsing mode)
        parseNumber('+210', { extended: true }).should.deep.equal({})
 
        // Too short of a number.
        parseNumber('1', 'US', { extended: true }).should.deep.equal({})
 
        // Too long of a number.
        parseNumber('1111111111111111111', 'RU', { extended: true }).should.deep.equal({})
 
        // Not a number.
        parseNumber('abcdefg', 'US', { extended: true }).should.deep.equal({})
 
        // Country phone code beginning with a '0'
        parseNumber('+0123').should.deep.equal({})
 
        // Barbados NANPA phone number
        parseNumber('+12460000000').should.deep.equal({ country: 'BB', phone: '2460000000' })
 
        // // A case when country (restricted to) is not equal
        // // to the one parsed out of an international number.
        // parseNumber('+1-213-373-4253', 'RU').should.deep.equal({})
 
        // National (significant) number too short
        parseNumber('2', 'US').should.deep.equal({})
 
        // National (significant) number too long
        parseNumber('222222222222222222', 'US').should.deep.equal({})
 
        // No `national_prefix_for_parsing`
        parseNumber('41111', 'AC').should.deep.equal({ country: 'AC', phone: '41111'})
 
        // https://github.com/catamphetamine/libphonenumber-js/issues/235
        // `matchesEntirely()` bug fix.
        parseNumber('+4915784846111‬').should.deep.equal({ country: 'DE', phone: '15784846111' })
 
        // No metadata
        thrower = () => _parseNumber('')
        thrower.should.throw('`metadata` argument not passed')
 
        // Numerical `value`
        thrower = () => parseNumber(2141111111, 'US')
        thrower.should.throw('A text for parsing must be a string.')
 
        // Input string too long.
        parseNumber('8005553535                                                                                                                                                                                                                                                 ', 'RU').should.deep.equal({})
    })
 
    it('should parse phone number extensions', () => {
        // "ext"
        parseNumber('2134567890 ext 123', 'US').should.deep.equal({
            country : 'US',
            phone   : '2134567890',
            ext     : '123'
        })
 
        // "ext."
        parseNumber('+12134567890 ext. 12345', 'US').should.deep.equal({
            country : 'US',
            phone   : '2134567890',
            ext     : '12345'
        })
 
        // "доб."
        parseNumber('+78005553535 доб. 1234', 'RU').should.deep.equal({
            country : 'RU',
            phone   : '8005553535',
            ext     : '1234'
        })
 
        // "#"
        parseNumber('+12134567890#1234').should.deep.equal({
            country : 'US',
            phone   : '2134567890',
            ext     : '1234'
        })
 
        // "x"
        parseNumber('+78005553535 x1234').should.deep.equal({
            country : 'RU',
            phone   : '8005553535',
            ext     : '1234'
        })
 
        // Not a valid extension
        parseNumber('2134567890 ext. abc', 'US').should.deep.equal({
            country : 'US',
            phone   : '2134567890'
        })
    })
 
    it('should parse RFC 3966 phone numbers', () => {
        parseNumber('tel:+78005553535;ext=123').should.deep.equal({
            country : 'RU',
            phone   : '8005553535',
            ext     : '123'
        })
 
        // Should parse "visual separators".
        parseNumber('tel:+7(800)555-35.35;ext=123').should.deep.equal({
            country : 'RU',
            phone   : '8005553535',
            ext     : '123'
        })
 
        // Invalid number.
        parseNumber('tel:+7x8005553535;ext=123').should.deep.equal({})
    })
 
    it('should parse invalid international numbers even if they are invalid', () => {
        parseNumber('+7(8)8005553535', 'RU').should.deep.equal({
            country : 'RU',
            phone   : '8005553535'
        })
    })
 
    it('should parse carrier codes', () => {
        parseNumber('0 15 21 5555-5555', 'BR', { extended: true }).should.deep.equal({
            country            : 'BR',
            countryCallingCode : '55',
            phone              : '2155555555',
            carrierCode        : '15',
            ext                : undefined,
            valid              : true,
            possible           : true
        })
    })
 
    it('should parse IDD prefixes', () => {
        parseNumber('011 61 2 3456 7890', 'US').should.deep.equal({
            phone   : '234567890',
            country : 'AU'
        })
 
        parseNumber('011 61 2 3456 7890', 'FR').should.deep.equal({})
 
        parseNumber('00 61 2 3456 7890', 'US').should.deep.equal({})
 
        parseNumber('810 61 2 3456 7890', 'RU').should.deep.equal({
            phone   : '234567890',
            country : 'AU'
        })
    })
 
    it('should work with v2 API', () => {
        parseNumber('+99989160151539')
    })
 
    it('should work with Argentina numbers', () => {
        // The same mobile number is written differently
        // in different formats in Argentina:
        // `9` gets prepended in international format.
        parseNumber('+54 9 3435 55 1212').should.deep.equal({
            country: 'AR',
            phone: '93435551212'
        })
        parseNumber('0343 15-555-1212', 'AR').should.deep.equal({
            country: 'AR',
            phone: '93435551212'
        })
    })
 
    it('should work with Mexico numbers', () => {
        // Fixed line.
        parseNumber('+52 449 978 0001').should.deep.equal({
            country: 'MX',
            phone: '4499780001'
        })
        // "Dialling tokens 01, 02, 044, 045 and 1 are removed as they are
        //  no longer valid since August 2019."
        //
        // parseNumber('01 (449)978-0001', 'MX').should.deep.equal({
        //     country: 'MX',
        //     phone: '4499780001'
        // })
        parseNumber('(449)978-0001', 'MX').should.deep.equal({
            country: 'MX',
            phone: '4499780001'
        })
        // "Dialling tokens 01, 02, 044, 045 and 1 are removed as they are
        //  no longer valid since August 2019."
        //
        // // Mobile.
        // // `1` is prepended before area code to mobile numbers in international format.
        // parseNumber('+52 1 33 1234-5678', 'MX').should.deep.equal({
        //     country: 'MX',
        //     phone: '3312345678'
        // })
        parseNumber('+52 33 1234-5678', 'MX').should.deep.equal({
            country: 'MX',
            phone: '3312345678'
        })
        // "Dialling tokens 01, 02, 044, 045 and 1 are removed as they are
        //  no longer valid since August 2019."
        //
        // parseNumber('044 (33) 1234-5678', 'MX').should.deep.equal({
        //     country: 'MX',
        //     phone: '3312345678'
        // })
        // parseNumber('045 33 1234-5678', 'MX').should.deep.equal({
        //     country: 'MX',
        //     phone: '3312345678'
        // })
    })
 
    it('should parse non-geographic numbering plan phone numbers', () => {
        parseNumber('+870773111632').should.deep.equal(
            USE_NON_GEOGRAPHIC_COUNTRY_CODE ?
            {
                country: '001',
                phone: '773111632'
            } :
            {}
        )
    })
 
    it('should parse non-geographic numbering plan phone numbers (default country code)', () => {
        parseNumber('773111632', { defaultCallingCode: '870' }).should.deep.equal(
            USE_NON_GEOGRAPHIC_COUNTRY_CODE ?
            {
                country: '001',
                phone: '773111632'
            } :
            {}
        )
    })
 
    it('should parse non-geographic numbering plan phone numbers (extended)', () => {
        parseNumber('+870773111632', { extended: true }).should.deep.equal({
            country: USE_NON_GEOGRAPHIC_COUNTRY_CODE ? '001' : undefined,
            countryCallingCode: '870',
            phone: '773111632',
            carrierCode: undefined,
            ext: undefined,
            possible: true,
            valid: true
        })
    })
 
    it('should parse non-geographic numbering plan phone numbers (default country code) (extended)', () => {
        parseNumber('773111632', { defaultCallingCode: '870', extended: true }).should.deep.equal({
            country: USE_NON_GEOGRAPHIC_COUNTRY_CODE ? '001' : undefined,
            countryCallingCode: '870',
            phone: '773111632',
            carrierCode: undefined,
            ext: undefined,
            possible: true,
            valid: true
        })
    })
 
    it('shouldn\'t crash when invalid `defaultCallingCode` is passed', () => {
        expect(() => parseNumber('773111632', { defaultCallingCode: '999' }))
            .to.throw('Unknown calling code')
    })
 
    it('shouldn\'t set `country` when there\'s no `defaultCountry` and `defaultCallingCode` is not of a "non-geographic entity"', () => {
        parseNumber('88005553535', { defaultCallingCode: '7' }).should.deep.equal({
            country: 'RU',
            phone: '8005553535'
        })
    })
 
    it('should correctly parse numbers starting with the same digit as the national prefix', () => {
        // https://github.com/catamphetamine/libphonenumber-js/issues/373
        // `BY`'s `national_prefix` is `8`.
        parseNumber('+37582004910060').should.deep.equal({
            country: 'BY',
            phone: '82004910060'
        });
    })
 
    it('should autocorrect numbers without a leading +', () => {
        // https://github.com/catamphetamine/libphonenumber-js/issues/376
        parseNumber('375447521111', 'BY').should.deep.equal({
            country: 'BY',
            phone: '447521111'
        });
        // https://github.com/catamphetamine/libphonenumber-js/issues/316
        parseNumber('33612902554', 'FR').should.deep.equal({
            country: 'FR',
            phone: '612902554'
        });
        // https://github.com/catamphetamine/libphonenumber-js/issues/375
        parseNumber('61438331999', 'AU').should.deep.equal({
            country: 'AU',
            phone: '438331999'
        });
        // A case when `49` is a country calling code of a number without a leading `+`.
        parseNumber('4930123456', 'DE').should.deep.equal({
            country: 'DE',
            phone: '30123456'
        });
        // A case when `49` is a valid area code.
        parseNumber('4951234567890', 'DE').should.deep.equal({
            country: 'DE',
            phone: '4951234567890'
        });
    })
 
    it('should parse extensions (long extensions with explicitl abels)', () => {
        // Test lower and upper limits of extension lengths for each type of label.
 
        // Firstly, when in RFC format: PhoneNumberUtil.extLimitAfterExplicitLabel
        parseNumber('33316005 ext 0', 'NZ').ext.should.equal('0')
        parseNumber('33316005 ext 01234567890123456789', 'NZ').ext.should.equal('01234567890123456789')
        // Extension too long.
        expect(parseNumber('33316005 ext 012345678901234567890', 'NZ').ext).to.be.undefined
 
        // Explicit extension label.
        parseNumber('03 3316005ext:1', 'NZ').ext.should.equal('1')
        parseNumber('03 3316005 xtn:12345678901234567890', 'NZ').ext.should.equal('12345678901234567890')
        parseNumber('03 3316005 extension\t12345678901234567890', 'NZ').ext.should.equal('12345678901234567890')
        parseNumber('03 3316005 xtensio:12345678901234567890', 'NZ').ext.should.equal('12345678901234567890')
        parseNumber('03 3316005 xtensión, 12345678901234567890#', 'NZ').ext.should.equal('12345678901234567890')
        parseNumber('03 3316005extension.12345678901234567890', 'NZ').ext.should.equal('12345678901234567890')
        parseNumber('03 3316005 доб:12345678901234567890', 'NZ').ext.should.equal('12345678901234567890')
 
        // Extension too long.
        expect(parseNumber('03 3316005 extension 123456789012345678901', 'NZ').ext).to.be.undefined
    })
 
    it('should parse extensions (long extensions with auto dialling labels)', () => {
        parseNumber('+12679000000,,123456789012345#').ext.should.equal('123456789012345')
        parseNumber('+12679000000;123456789012345#').ext.should.equal('123456789012345')
        parseNumber('+442034000000,,123456789#').ext.should.equal('123456789')
        // Extension too long.
        expect(parseNumber('+12679000000,,1234567890123456#').ext).to.be.undefined
    })
 
    it('should parse extensions (short extensions with ambiguous characters)', () => {
        parseNumber('03 3316005 x 123456789', 'NZ').ext.should.equal('123456789')
        parseNumber('03 3316005 x. 123456789', 'NZ').ext.should.equal('123456789')
        parseNumber('03 3316005 #123456789#', 'NZ').ext.should.equal('123456789')
        parseNumber('03 3316005 ~ 123456789', 'NZ').ext.should.equal('123456789')
        // Extension too long.
        expect(parseNumber('03 3316005 ~ 1234567890', 'NZ').ext).to.be.undefined
    })
 
    it('should parse extensions (short extensions when not sure of label)', () => {
        parseNumber('+1123-456-7890 666666#', { v2: true }).ext.should.equal('666666')
        parseNumber('+11234567890-6#', { v2: true }).ext.should.equal('6')
        // Extension too long.
        expect(() => parseNumber('+1123-456-7890 7777777#', { v2: true })).to.throw('NOT_A_NUMBER')
    })
})