UNPKG

19.1 kBJavaScriptView Raw
1"use strict";
2
3var _metadataMin = _interopRequireDefault(require("../metadata.min.json"));
4
5var _parse = _interopRequireDefault(require("./parse"));
6
7var _metadata = _interopRequireDefault(require("./metadata"));
8
9function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
10
11function parseNumber() {
12 for (var _len = arguments.length, parameters = new Array(_len), _key = 0; _key < _len; _key++) {
13 parameters[_key] = arguments[_key];
14 }
15
16 parameters.push(_metadataMin["default"]);
17 return _parse["default"].apply(this, parameters);
18}
19
20var USE_NON_GEOGRAPHIC_COUNTRY_CODE = false;
21describe('parse', function () {
22 it('should not parse invalid phone numbers', function () {
23 // Too short.
24 parseNumber('+7 (800) 55-35-35').should.deep.equal({}); // Too long.
25
26 parseNumber('+7 (800) 55-35-35-55').should.deep.equal({});
27 parseNumber('+7 (800) 55-35-35', 'US').should.deep.equal({});
28 parseNumber('(800) 55 35 35', {
29 defaultCountry: 'RU'
30 }).should.deep.equal({});
31 parseNumber('+1 187 215 5230', 'US').should.deep.equal({});
32 parseNumber('911231231', 'BE').should.deep.equal({});
33 });
34 it('should parse valid phone numbers', function () {
35 // Instant loans
36 // https://www.youtube.com/watch?v=6e1pMrYH5jI
37 //
38 // Restrict to RU
39 parseNumber('Phone: 8 (800) 555 35 35.', 'RU').should.deep.equal({
40 country: 'RU',
41 phone: '8005553535'
42 }); // International format
43
44 parseNumber('Phone: +7 (800) 555-35-35.').should.deep.equal({
45 country: 'RU',
46 phone: '8005553535'
47 }); // // Restrict to US, but not a US country phone code supplied
48 // parseNumber('+7 (800) 555-35-35', 'US').should.deep.equal({})
49 // Restrict to RU
50
51 parseNumber('(800) 555 35 35', 'RU').should.deep.equal({
52 country: 'RU',
53 phone: '8005553535'
54 }); // Default to RU
55
56 parseNumber('8 (800) 555 35 35', {
57 defaultCountry: 'RU'
58 }).should.deep.equal({
59 country: 'RU',
60 phone: '8005553535'
61 }); // Gangster partyline
62
63 parseNumber('+1-213-373-4253').should.deep.equal({
64 country: 'US',
65 phone: '2133734253'
66 }); // Switzerland (just in case)
67
68 parseNumber('044 668 18 00', 'CH').should.deep.equal({
69 country: 'CH',
70 phone: '446681800'
71 }); // China, Beijing
72
73 parseNumber('010-852644821', 'CN').should.deep.equal({
74 country: 'CN',
75 phone: '10852644821'
76 }); // France
77
78 parseNumber('+33169454850').should.deep.equal({
79 country: 'FR',
80 phone: '169454850'
81 }); // UK (Jersey)
82
83 parseNumber('+44 7700 300000').should.deep.equal({
84 country: 'JE',
85 phone: '7700300000'
86 }); // KZ
87
88 parseNumber('+7 702 211 1111').should.deep.equal({
89 country: 'KZ',
90 phone: '7022111111'
91 }); // Brazil
92
93 parseNumber('11987654321', 'BR').should.deep.equal({
94 country: 'BR',
95 phone: '11987654321'
96 }); // Long country phone code.
97
98 parseNumber('+212659777777').should.deep.equal({
99 country: 'MA',
100 phone: '659777777'
101 }); // No country could be derived.
102 // parseNumber('+212569887076').should.deep.equal({ countryPhoneCode: '212', phone: '569887076' })
103 // GB. Moible numbers starting 07624* are Isle of Man.
104
105 parseNumber('07624369230', 'GB').should.deep.equal({
106 country: 'IM',
107 phone: '7624369230'
108 });
109 });
110 it('should parse possible numbers', function () {
111 // Invalid phone number for a given country.
112 parseNumber('1112223344', 'RU', {
113 extended: true
114 }).should.deep.equal({
115 country: 'RU',
116 countryCallingCode: '7',
117 phone: '1112223344',
118 carrierCode: undefined,
119 ext: undefined,
120 valid: false,
121 possible: true
122 }); // International phone number.
123 // Several countries with the same country phone code.
124
125 parseNumber('+71112223344').should.deep.equal({});
126 parseNumber('+71112223344', {
127 extended: true
128 }).should.deep.equal({
129 country: undefined,
130 countryCallingCode: '7',
131 phone: '1112223344',
132 carrierCode: undefined,
133 ext: undefined,
134 valid: false,
135 possible: true
136 }); // International phone number.
137 // Single country with the given country phone code.
138
139 parseNumber('+33011222333', {
140 extended: true
141 }).should.deep.equal({
142 country: 'FR',
143 countryCallingCode: '33',
144 phone: '011222333',
145 carrierCode: undefined,
146 ext: undefined,
147 valid: false,
148 possible: true
149 }); // Too short.
150 // Won't strip national prefix `8` because otherwise the number would be too short.
151
152 parseNumber('+7 (800) 55-35-35', {
153 extended: true
154 }).should.deep.equal({
155 country: 'RU',
156 countryCallingCode: '7',
157 phone: '800553535',
158 carrierCode: undefined,
159 ext: undefined,
160 valid: false,
161 possible: false
162 }); // Too long.
163
164 parseNumber('+7 (800) 55-35-35-555', {
165 extended: true
166 }).should.deep.equal({
167 country: undefined,
168 countryCallingCode: '7',
169 phone: '00553535555',
170 carrierCode: undefined,
171 ext: undefined,
172 valid: false,
173 possible: false
174 }); // No national number to be parsed.
175
176 parseNumber('+996', {
177 extended: true
178 }).should.deep.equal({// countryCallingCode : '996'
179 }); // Valid number.
180
181 parseNumber('+78005553535', {
182 extended: true
183 }).should.deep.equal({
184 country: 'RU',
185 countryCallingCode: '7',
186 phone: '8005553535',
187 carrierCode: undefined,
188 ext: undefined,
189 valid: true,
190 possible: true
191 }); // https://github.com/catamphetamine/libphonenumber-js/issues/211
192
193 parseNumber('+966', {
194 extended: true
195 }).should.deep.equal({});
196 parseNumber('+9664', {
197 extended: true
198 }).should.deep.equal({});
199 parseNumber('+96645', {
200 extended: true
201 }).should.deep.equal({
202 carrierCode: undefined,
203 phone: '45',
204 ext: undefined,
205 country: 'SA',
206 countryCallingCode: '966',
207 possible: false,
208 valid: false
209 });
210 });
211 it('should parse non-European digits', function () {
212 parseNumber('+١٢١٢٢٣٢٣٢٣٢').should.deep.equal({
213 country: 'US',
214 phone: '2122323232'
215 });
216 });
217 it('should work in edge cases', function () {
218 var thrower; // No input
219
220 parseNumber('').should.deep.equal({}); // No country phone code
221
222 parseNumber('+').should.deep.equal({}); // No country at all (non international number and no explicit country code)
223
224 parseNumber('123').should.deep.equal({}); // No country metadata for this `require` country code
225
226 thrower = function thrower() {
227 return parseNumber('123', 'ZZ');
228 };
229
230 thrower.should["throw"]('Unknown country'); // No country metadata for this `default` country code
231
232 thrower = function thrower() {
233 return parseNumber('123', {
234 defaultCountry: 'ZZ'
235 });
236 };
237
238 thrower.should["throw"]('Unknown country'); // Invalid country phone code
239
240 parseNumber('+210').should.deep.equal({}); // Invalid country phone code (extended parsing mode)
241
242 parseNumber('+210', {
243 extended: true
244 }).should.deep.equal({}); // Too short of a number.
245
246 parseNumber('1', 'US', {
247 extended: true
248 }).should.deep.equal({}); // Too long of a number.
249
250 parseNumber('1111111111111111111', 'RU', {
251 extended: true
252 }).should.deep.equal({}); // Not a number.
253
254 parseNumber('abcdefg', 'US', {
255 extended: true
256 }).should.deep.equal({}); // Country phone code beginning with a '0'
257
258 parseNumber('+0123').should.deep.equal({}); // Barbados NANPA phone number
259
260 parseNumber('+12460000000').should.deep.equal({
261 country: 'BB',
262 phone: '2460000000'
263 }); // // A case when country (restricted to) is not equal
264 // // to the one parsed out of an international number.
265 // parseNumber('+1-213-373-4253', 'RU').should.deep.equal({})
266 // National (significant) number too short
267
268 parseNumber('2', 'US').should.deep.equal({}); // National (significant) number too long
269
270 parseNumber('222222222222222222', 'US').should.deep.equal({}); // No `national_prefix_for_parsing`
271
272 parseNumber('41111', 'AC').should.deep.equal({
273 country: 'AC',
274 phone: '41111'
275 }); // https://github.com/catamphetamine/libphonenumber-js/issues/235
276 // `matchesEntirely()` bug fix.
277
278 parseNumber('+4915784846111‬').should.deep.equal({
279 country: 'DE',
280 phone: '15784846111'
281 }); // No metadata
282
283 thrower = function thrower() {
284 return (0, _parse["default"])('');
285 };
286
287 thrower.should["throw"]('`metadata` argument not passed'); // Numerical `value`
288
289 thrower = function thrower() {
290 return parseNumber(2141111111, 'US');
291 };
292
293 thrower.should["throw"]('A text for parsing must be a string.'); // Input string too long.
294
295 parseNumber('8005553535 ', 'RU').should.deep.equal({});
296 });
297 it('should parse phone number extensions', function () {
298 // "ext"
299 parseNumber('2134567890 ext 123', 'US').should.deep.equal({
300 country: 'US',
301 phone: '2134567890',
302 ext: '123'
303 }); // "ext."
304
305 parseNumber('+12134567890 ext. 12345', 'US').should.deep.equal({
306 country: 'US',
307 phone: '2134567890',
308 ext: '12345'
309 }); // "доб."
310
311 parseNumber('+78005553535 доб. 1234', 'RU').should.deep.equal({
312 country: 'RU',
313 phone: '8005553535',
314 ext: '1234'
315 }); // "#"
316
317 parseNumber('+12134567890#1234').should.deep.equal({
318 country: 'US',
319 phone: '2134567890',
320 ext: '1234'
321 }); // "x"
322
323 parseNumber('+78005553535 x1234').should.deep.equal({
324 country: 'RU',
325 phone: '8005553535',
326 ext: '1234'
327 }); // Not a valid extension
328
329 parseNumber('2134567890 ext. abc', 'US').should.deep.equal({
330 country: 'US',
331 phone: '2134567890'
332 });
333 });
334 it('should parse RFC 3966 phone numbers', function () {
335 parseNumber('tel:+78005553535;ext=123').should.deep.equal({
336 country: 'RU',
337 phone: '8005553535',
338 ext: '123'
339 }); // Should parse "visual separators".
340
341 parseNumber('tel:+7(800)555-35.35;ext=123').should.deep.equal({
342 country: 'RU',
343 phone: '8005553535',
344 ext: '123'
345 }); // Invalid number.
346
347 parseNumber('tel:+7x8005553535;ext=123').should.deep.equal({});
348 });
349 it('should parse invalid international numbers even if they are invalid', function () {
350 parseNumber('+7(8)8005553535', 'RU').should.deep.equal({
351 country: 'RU',
352 phone: '8005553535'
353 });
354 });
355 it('should parse carrier codes', function () {
356 parseNumber('0 15 21 5555-5555', 'BR', {
357 extended: true
358 }).should.deep.equal({
359 country: 'BR',
360 countryCallingCode: '55',
361 phone: '2155555555',
362 carrierCode: '15',
363 ext: undefined,
364 valid: true,
365 possible: true
366 });
367 });
368 it('should parse IDD prefixes', function () {
369 parseNumber('011 61 2 3456 7890', 'US').should.deep.equal({
370 phone: '234567890',
371 country: 'AU'
372 });
373 parseNumber('011 61 2 3456 7890', 'FR').should.deep.equal({});
374 parseNumber('00 61 2 3456 7890', 'US').should.deep.equal({});
375 parseNumber('810 61 2 3456 7890', 'RU').should.deep.equal({
376 phone: '234567890',
377 country: 'AU'
378 });
379 });
380 it('should work with v2 API', function () {
381 parseNumber('+99989160151539');
382 });
383 it('should work with Argentina numbers', function () {
384 // The same mobile number is written differently
385 // in different formats in Argentina:
386 // `9` gets prepended in international format.
387 parseNumber('+54 9 3435 55 1212').should.deep.equal({
388 country: 'AR',
389 phone: '93435551212'
390 });
391 parseNumber('0343 15-555-1212', 'AR').should.deep.equal({
392 country: 'AR',
393 phone: '93435551212'
394 });
395 });
396 it('should work with Mexico numbers', function () {
397 // Fixed line.
398 parseNumber('+52 449 978 0001').should.deep.equal({
399 country: 'MX',
400 phone: '4499780001'
401 });
402 parseNumber('01 (449)978-0001', 'MX').should.deep.equal({
403 country: 'MX',
404 phone: '4499780001'
405 });
406 parseNumber('(449)978-0001', 'MX').should.deep.equal({
407 country: 'MX',
408 phone: '4499780001'
409 }); // Mobile.
410 // `1` is prepended before area code to mobile numbers in international format.
411
412 parseNumber('+52 1 33 1234-5678', 'MX').should.deep.equal({
413 country: 'MX',
414 phone: '3312345678'
415 });
416 parseNumber('+52 33 1234-5678', 'MX').should.deep.equal({
417 country: 'MX',
418 phone: '3312345678'
419 });
420 parseNumber('044 (33) 1234-5678', 'MX').should.deep.equal({
421 country: 'MX',
422 phone: '3312345678'
423 });
424 parseNumber('045 33 1234-5678', 'MX').should.deep.equal({
425 country: 'MX',
426 phone: '3312345678'
427 });
428 });
429 it('should parse non-geographic numbering plan phone numbers', function () {
430 parseNumber('+870773111632').should.deep.equal(USE_NON_GEOGRAPHIC_COUNTRY_CODE ? {
431 country: '001',
432 phone: '773111632'
433 } : {});
434 });
435 it('should parse non-geographic numbering plan phone numbers (default country code)', function () {
436 parseNumber('773111632', {
437 defaultCallingCode: '870'
438 }).should.deep.equal(USE_NON_GEOGRAPHIC_COUNTRY_CODE ? {
439 country: '001',
440 phone: '773111632'
441 } : {});
442 });
443 it('should parse non-geographic numbering plan phone numbers (extended)', function () {
444 parseNumber('+870773111632', {
445 extended: true
446 }).should.deep.equal({
447 country: USE_NON_GEOGRAPHIC_COUNTRY_CODE ? '001' : undefined,
448 countryCallingCode: '870',
449 phone: '773111632',
450 carrierCode: undefined,
451 ext: undefined,
452 possible: true,
453 valid: true
454 });
455 });
456 it('should parse non-geographic numbering plan phone numbers (default country code) (extended)', function () {
457 parseNumber('773111632', {
458 defaultCallingCode: '870',
459 extended: true
460 }).should.deep.equal({
461 country: USE_NON_GEOGRAPHIC_COUNTRY_CODE ? '001' : undefined,
462 countryCallingCode: '870',
463 phone: '773111632',
464 carrierCode: undefined,
465 ext: undefined,
466 possible: true,
467 valid: true
468 });
469 });
470 it('shouldn\'t crash when invalid `defaultCallingCode` is passed', function () {
471 expect(function () {
472 return parseNumber('773111632', {
473 defaultCallingCode: '999'
474 });
475 }).to["throw"]('Unknown calling code');
476 });
477 it('shouldn\'t set `country` when there\'s no `defaultCountry` and `defaultCallingCode` is not of a "non-geographic entity"', function () {
478 parseNumber('88005553535', {
479 defaultCallingCode: '7'
480 }).should.deep.equal({
481 country: 'RU',
482 phone: '8005553535'
483 });
484 });
485 it('should correctly parse numbers starting with the same digit as the national prefix', function () {
486 // https://github.com/catamphetamine/libphonenumber-js/issues/373
487 // `BY`'s `national_prefix` is `8`.
488 parseNumber('+37582004910060').should.deep.equal({
489 country: 'BY',
490 phone: '82004910060'
491 });
492 });
493 it('should autocorrect numbers without a leading +', function () {
494 // https://github.com/catamphetamine/libphonenumber-js/issues/376
495 parseNumber('375447521111', 'BY').should.deep.equal({
496 country: 'BY',
497 phone: '447521111'
498 }); // https://github.com/catamphetamine/libphonenumber-js/issues/316
499
500 parseNumber('33612902554', 'FR').should.deep.equal({
501 country: 'FR',
502 phone: '612902554'
503 }); // https://github.com/catamphetamine/libphonenumber-js/issues/375
504
505 parseNumber('61438331999', 'AU').should.deep.equal({
506 country: 'AU',
507 phone: '438331999'
508 }); // A case when `49` is a country calling code of a number without a leading `+`.
509
510 parseNumber('4930123456', 'DE').should.deep.equal({
511 country: 'DE',
512 phone: '30123456'
513 }); // A case when `49` is a valid area code.
514
515 parseNumber('4951234567890', 'DE').should.deep.equal({
516 country: 'DE',
517 phone: '4951234567890'
518 });
519 });
520 it('should parse extensions (long extensions with explicitl abels)', function () {
521 // Test lower and upper limits of extension lengths for each type of label.
522 // Firstly, when in RFC format: PhoneNumberUtil.extLimitAfterExplicitLabel
523 parseNumber('33316005 ext 0', 'NZ').ext.should.equal('0');
524 parseNumber('33316005 ext 01234567890123456789', 'NZ').ext.should.equal('01234567890123456789'); // Extension too long.
525
526 expect(parseNumber('33316005 ext 012345678901234567890', 'NZ').ext).to.be.undefined; // Explicit extension label.
527
528 parseNumber('03 3316005ext:1', 'NZ').ext.should.equal('1');
529 parseNumber('03 3316005 xtn:12345678901234567890', 'NZ').ext.should.equal('12345678901234567890');
530 parseNumber('03 3316005 extension\t12345678901234567890', 'NZ').ext.should.equal('12345678901234567890');
531 parseNumber('03 3316005 xtensio:12345678901234567890', 'NZ').ext.should.equal('12345678901234567890');
532 parseNumber('03 3316005 xtensión, 12345678901234567890#', 'NZ').ext.should.equal('12345678901234567890');
533 parseNumber('03 3316005extension.12345678901234567890', 'NZ').ext.should.equal('12345678901234567890');
534 parseNumber('03 3316005 доб:12345678901234567890', 'NZ').ext.should.equal('12345678901234567890'); // Extension too long.
535
536 expect(parseNumber('03 3316005 extension 123456789012345678901', 'NZ').ext).to.be.undefined;
537 });
538 it('should parse extensions (long extensions with auto dialling labels)', function () {
539 parseNumber('+12679000000,,123456789012345#').ext.should.equal('123456789012345');
540 parseNumber('+12679000000;123456789012345#').ext.should.equal('123456789012345');
541 parseNumber('+442034000000,,123456789#').ext.should.equal('123456789'); // Extension too long.
542
543 expect(parseNumber('+12679000000,,1234567890123456#').ext).to.be.undefined;
544 });
545 it('should parse extensions (short extensions with ambiguous characters)', function () {
546 parseNumber('03 3316005 x 123456789', 'NZ').ext.should.equal('123456789');
547 parseNumber('03 3316005 x. 123456789', 'NZ').ext.should.equal('123456789');
548 parseNumber('03 3316005 #123456789#', 'NZ').ext.should.equal('123456789');
549 parseNumber('03 3316005 ~ 123456789', 'NZ').ext.should.equal('123456789'); // Extension too long.
550
551 expect(parseNumber('03 3316005 ~ 1234567890', 'NZ').ext).to.be.undefined;
552 });
553 it('should parse extensions (short extensions when not sure of label)', function () {
554 parseNumber('+1123-456-7890 666666#', {
555 v2: true
556 }).ext.should.equal('666666');
557 parseNumber('+11234567890-6#', {
558 v2: true
559 }).ext.should.equal('6'); // Extension too long.
560
561 expect(function () {
562 return parseNumber('+1123-456-7890 7777777#', {
563 v2: true
564 });
565 }).to["throw"]('NOT_A_NUMBER');
566 });
567});
568//# sourceMappingURL=parse.test.js.map
\No newline at end of file