UNPKG

46.8 kBJavaScriptView Raw
1"use strict";
2
3var _metadataMin = _interopRequireDefault(require("../metadata.min.json"));
4
5var _AsYouType = _interopRequireDefault(require("./AsYouType"));
6
7function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
8
9function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
10
11function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
12
13function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
14
15function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
16
17function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
18
19function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
20
21function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
22
23var AsYouType =
24/*#__PURE__*/
25function (_AsYouType_) {
26 _inherits(AsYouType, _AsYouType_);
27
28 function AsYouType(country_code) {
29 _classCallCheck(this, AsYouType);
30
31 return _possibleConstructorReturn(this, _getPrototypeOf(AsYouType).call(this, country_code, _metadataMin["default"]));
32 }
33
34 return AsYouType;
35}(_AsYouType["default"]);
36
37var USE_NON_GEOGRAPHIC_COUNTRY_CODE = false;
38describe('AsYouType', function () {
39 it('should use "national_prefix_formatting_rule"', function () {
40 // With national prefix (full).
41 new AsYouType('RU').input('88005553535').should.equal('8 (800) 555-35-35'); // With national prefix (partial).
42
43 new AsYouType('RU').input('880055535').should.equal('8 (800) 555-35');
44 });
45 it('should populate national number template (digit by digit)', function () {
46 var formatter = new AsYouType('US');
47 formatter.input('1');
48 formatter.formatter.template.should.equal('x (xxx) xxx-xxxx');
49 formatter.formatter.populatedNationalNumberTemplate.should.equal('1 (xxx) xxx-xxxx');
50 formatter.input('213');
51 formatter.formatter.populatedNationalNumberTemplate.should.equal('1 (213) xxx-xxxx');
52 formatter.input('3734253');
53 formatter.formatter.populatedNationalNumberTemplate.should.equal('1 (213) 373-4253');
54 });
55 it('should populate international number template (digit by digit) (default country)', function () {
56 var formatter = new AsYouType('US');
57 expect(formatter.formatter.template).to.be.undefined;
58 expect(formatter.formatter.populatedNationalNumberTemplate).to.be.undefined;
59 formatter.input('').should.equal('');
60 expect(formatter.formatter.template).to.be.undefined;
61 expect(formatter.formatter.populatedNationalNumberTemplate).to.be.undefined;
62 formatter.input('+').should.equal('+');
63 formatter.getTemplate().should.equal('x');
64 expect(formatter.formatter.template).to.be.undefined;
65 expect(formatter.formatter.populatedNationalNumberTemplate).to.be.undefined;
66 formatter.input('1').should.equal('+1');
67 formatter.getTemplate().should.equal('xx'); // Hasn't started formatting the phone number using the template yet.
68
69 formatter.formatter.template.should.equal('xx xxx xxx xxxx');
70 formatter.formatter.populatedNationalNumberTemplate.should.equal('xxx xxx xxxx'); // Has some national number digits, starts formatting the phone number using the template.
71
72 formatter.input('213');
73 formatter.formatter.populatedNationalNumberTemplate.should.equal('213 xxx xxxx');
74 formatter.input('3734253');
75 formatter.formatter.populatedNationalNumberTemplate.should.equal('213 373 4253');
76 });
77 it('should populate international number template (digit by digit)', function () {
78 var formatter = new AsYouType();
79 expect(formatter.formatter.template).to.be.undefined;
80 expect(formatter.formatter.populatedNationalNumberTemplate).to.be.undefined;
81 formatter.input('').should.equal('');
82 expect(formatter.formatter.template).to.be.undefined;
83 expect(formatter.formatter.populatedNationalNumberTemplate).to.be.undefined;
84 formatter.input('+').should.equal('+');
85 expect(formatter.formatter.template).to.be.undefined;
86 expect(formatter.formatter.populatedNationalNumberTemplate).to.be.undefined;
87 formatter.input('1').should.equal('+1');
88 formatter.formatter.template.should.equal('xx xxx xxx xxxx'); // Hasn't yet started formatting the phone number using the template.
89
90 formatter.formatter.populatedNationalNumberTemplate.should.equal('xxx xxx xxxx'); // Has some national number digits, starts formatting the phone number using the template.
91
92 formatter.input('213');
93 formatter.formatter.populatedNationalNumberTemplate.should.equal('213 xxx xxxx');
94 formatter.input('3734253');
95 formatter.formatter.populatedNationalNumberTemplate.should.equal('213 373 4253');
96 });
97 it('should populate national number template (attempt to format complete number)', function () {
98 var formatter = new AsYouType('US');
99 formatter.input('12133734253').should.equal('1 (213) 373-4253');
100 formatter.formatter.template.should.equal('x (xxx) xxx-xxxx');
101 formatter.formatter.populatedNationalNumberTemplate.should.equal('1 (213) 373-4253');
102 });
103 it('should parse and format phone numbers as you type', function () {
104 // International number test
105 new AsYouType().input('+12133734').should.equal('+1 213 373 4'); // Local number test
106
107 new AsYouType('US').input('2133734').should.equal('(213) 373-4'); // US national number retains national prefix.
108
109 new AsYouType('US').input('12133734').should.equal('1 (213) 373-4'); // US national number retains national prefix (full number).
110
111 new AsYouType('US').input('12133734253').should.equal('1 (213) 373-4253');
112 var formatter; // // Should discard national prefix from a "complete" phone number.
113 // new AsYouType('RU').input('8800555353').should.equal('880 055-53-53')
114 // Shouldn't extract national prefix when inputting in international format.
115
116 new AsYouType('RU').input('+7800555353').should.equal('+7 800 555 35 3');
117 new AsYouType('CH').input('044-668-1').should.equal('044 668 1'); // Test International phone number (international)
118
119 formatter = new AsYouType(); // formatter.valid.should.be.false
120
121 type(formatter.getCountry()).should.equal('undefined');
122 type(formatter.getCountryCallingCode()).should.equal('undefined');
123 formatter.getTemplate().should.equal('');
124 formatter.input('+').should.equal('+'); // formatter.valid.should.be.false
125
126 type(formatter.getCountry()).should.equal('undefined');
127 type(formatter.getCountryCallingCode()).should.equal('undefined');
128 formatter.getTemplate().should.equal('x');
129 formatter.input('1').should.equal('+1'); // formatter.valid.should.be.false
130
131 type(formatter.getCountry()).should.equal('undefined');
132 formatter.getCountryCallingCode().should.equal('1');
133 formatter.getTemplate().should.equal('xx');
134 formatter.input('2').should.equal('+1 2');
135 formatter.getTemplate().should.equal('xx x'); // formatter.valid.should.be.false
136
137 type(formatter.getCountry()).should.equal('undefined');
138 formatter.input('1').should.equal('+1 21');
139 formatter.input('3').should.equal('+1 213');
140 formatter.input(' ').should.equal('+1 213');
141 formatter.input('3').should.equal('+1 213 3');
142 formatter.input('3').should.equal('+1 213 33');
143 formatter.input('3').should.equal('+1 213 333');
144 formatter.input('4').should.equal('+1 213 333 4');
145 formatter.input('4').should.equal('+1 213 333 44');
146 formatter.input('4').should.equal('+1 213 333 444'); // formatter.valid.should.be.false
147
148 type(formatter.getCountry()).should.equal('undefined');
149 formatter.input('4').should.equal('+1 213 333 4444'); // formatter.valid.should.be.true
150
151 formatter.getCountry().should.equal('US'); // This one below contains "punctuation spaces"
152 // along with the regular spaces
153
154 formatter.getTemplate().should.equal('xx xxx xxx xxxx');
155 formatter.input('5').should.equal('+1 21333344445'); // formatter.valid.should.be.false
156
157 expect(formatter.getCountry()).to.be.undefined;
158 formatter.getCountryCallingCode().should.equal('1');
159 expect(formatter.formatter.template).to.be.undefined; // Check that clearing an international formatter
160 // also clears country metadata.
161
162 formatter.reset();
163 formatter.input('+').should.equal('+');
164 formatter.input('7').should.equal('+7');
165 formatter.input('9').should.equal('+7 9');
166 formatter.input('99 111 22 33').should.equal('+7 999 111 22 33'); // Test Switzerland phone numbers
167
168 formatter = new AsYouType('CH');
169 formatter.input(' ').should.equal('');
170 formatter.input('0').should.equal('0');
171 formatter.input('4').should.equal('04');
172 formatter.input(' ').should.equal('04');
173 formatter.input('-').should.equal('04');
174 formatter.input('4').should.equal('044');
175 formatter.input('-').should.equal('044');
176 formatter.input('6').should.equal('044 6');
177 formatter.input('6').should.equal('044 66');
178 formatter.input('8').should.equal('044 668');
179 formatter.input('-').should.equal('044 668');
180 formatter.input('1').should.equal('044 668 1');
181 formatter.input('8').should.equal('044 668 18'); // formatter.valid.should.be.false
182
183 formatter.getCountry().should.equal('CH');
184 formatter.formatter.template.should.equal('xxx xxx xx xx');
185 formatter.getTemplate().should.equal('xxx xxx xx');
186 formatter.input(' 00').should.equal('044 668 18 00'); // formatter.valid.should.be.true
187
188 formatter.getCountry().should.equal('CH');
189 formatter.getTemplate().should.equal('xxx xxx xx xx');
190 formatter.input('9').should.equal('04466818009'); // formatter.valid.should.be.false
191
192 formatter.getCountry().should.equal('CH');
193 expect(formatter.formatter.template).to.be.undefined; // Kazakhstan (non-main country for +7 country phone code)
194
195 formatter = new AsYouType();
196 formatter.input('+77172580659');
197 formatter.getCountry().should.equal('KZ'); // Brazil
198
199 formatter = new AsYouType('BR');
200 formatter.input('11987654321').should.equal('(11) 98765-4321'); // UK (Jersey) (non-main country for +44 country phone code)
201
202 formatter = new AsYouType();
203 formatter.input('+447700300000').should.equal('+44 7700 300000');
204 formatter.getTemplate().should.equal('xxx xxxx xxxxxx');
205 formatter.getCountry().should.equal('JE'); // Braces must be part of the template.
206
207 formatter = new AsYouType('RU');
208 formatter.input('88005553535').should.equal('8 (800) 555-35-35');
209 formatter.getTemplate().should.equal('x (xxx) xxx-xx-xx'); // Test Russian phone numbers
210 // (with optional national prefix `8`)
211
212 formatter = new AsYouType('RU');
213 formatter.input('8').should.equal('8');
214 formatter.input('9').should.equal('8 9');
215 formatter.input('9').should.equal('8 99');
216 formatter.input('9').should.equal('8 (999)');
217 formatter.input('-').should.equal('8 (999)');
218 formatter.input('1234').should.equal('8 (999) 123-4');
219 formatter.input('567').should.equal('8 (999) 123-45-67');
220 formatter.input('8').should.equal('899912345678'); // Shouldn't strip national prefix if it is optional
221 // and if it's a valid phone number (international).
222
223 formatter = new AsYouType('RU'); // formatter.input('8005553535').should.equal('(800) 555-35-35')
224
225 formatter.input('+78005553535').should.equal('+7 800 555 35 35');
226 formatter.getNationalNumber().should.equal('8005553535'); // Check that clearing an national formatter:
227 // * doesn't clear country metadata
228 // * clears all other things
229
230 formatter.reset();
231 formatter.input('8').should.equal('8');
232 formatter.input('9').should.equal('8 9');
233 formatter.input('9').should.equal('8 99');
234 formatter.input('9').should.equal('8 (999)');
235 formatter.input('-').should.equal('8 (999)');
236 formatter.input('1234').should.equal('8 (999) 123-4');
237 formatter.input('567').should.equal('8 (999) 123-45-67');
238 formatter.input('8').should.equal('899912345678'); // National prefix should not be prepended
239 // when formatting local NANPA phone numbers.
240
241 new AsYouType('US').input('1').should.equal('1');
242 new AsYouType('US').input('12').should.equal('1 2');
243 new AsYouType('US').input('123').should.equal('1 23'); // Bulgaria
244 // (should not prepend national prefix `0`)
245
246 new AsYouType('BG').input('111 222 3').should.equal('1112223'); // Deutchland
247
248 new AsYouType().input('+4915539898001').should.equal('+49 15539 898001'); // KZ detection
249
250 formatter = new AsYouType();
251 formatter.input('+7 702 211 1111');
252 formatter.getCountry().should.equal('KZ'); // formatter.valid.should.equal(true)
253 // New Zealand formatting fix (issue #89)
254
255 new AsYouType('NZ').input('0212').should.equal('021 2'); // South Korea
256
257 formatter = new AsYouType();
258 formatter.input('+82111111111').should.equal('+82 11 111 1111');
259 formatter.getTemplate().should.equal('xxx xx xxx xxxx');
260 });
261 it('should filter out formats that require a national prefix and no national prefix has been input', function () {
262 // Afghanistan.
263 var formatter = new AsYouType('AF'); // No national prefix, and national prefix is required in the format.
264 // (not `"national_prefix_is_optional_when_formatting": true`)
265
266 formatter.input('44444444').should.equal('44444444');
267 expect(formatter.formatter.template).to.be.undefined; // With national prefix
268
269 formatter.reset().input('044444444').should.equal('044 444 444');
270 formatter.formatter.template.should.equal('xxx xxx xxxx');
271 });
272 it('should work when a digit is not a national prefix but a part of a valid national number', function () {
273 // In Russia, `8` could be both a valid national prefix
274 // and a part of a valid national number.
275 var formatter = new AsYouType('RU'); // The formatter could try both variants:
276 // with extracting national prefix
277 // and without extracting it,
278 // and then choose whichever way has `this.matchingFormats`.
279 // Or there could be two instances of the formatter:
280 // one that extracts national prefix and one that doesn't,
281 // and then the one that has `this.matchingFormats` would be
282 // used to format the phone number.
283 // Something like an option `extractNationalPrefix: false`
284 // and creating `this.withNationalPrefixFormatter = new AsYouType(this.defaultCountry || this.defaultCallingCode, { metadata, extractNationalPrefix: false })`
285 // and something like `this.withNationalPrefixFormatter.input(nextDigits)` in `input(nextDigits)`.
286 // But, for this specific case, it's not required:
287 // in Russia, people are used to inputting `800` numbers with national prefix `8`:
288 // `8 800 555 35 35`.
289 // formatter.input('8005553535').should.equal('(800) 555-35-35')
290
291 formatter.input('8005553535').should.equal('8005553535');
292 formatter.reset();
293 formatter.input('+78005553535').should.equal('+7 800 555 35 35');
294 });
295 it('should match formats that require a national prefix and no national prefix has been input (national prefix is mandatory for a format)', function () {
296 var formatter = new AsYouType('FR');
297 formatter.input('612345678').should.equal('612345678');
298 formatter.reset();
299 formatter.input('0612345678').should.equal('06 12 34 56 78');
300 });
301 it('should match formats that require a national prefix and no national prefix has been input (national prefix is not mandatory for a format)', function () {
302 var formatter = new AsYouType('RU'); // Without national prefix.
303
304 formatter.input('9991234567').should.equal('999 123-45-67');
305 formatter.reset(); // With national prefix.
306
307 formatter.input('89991234567').should.equal('8 (999) 123-45-67');
308 });
309 it('should not use `national_prefix_formatting_rule` when formatting international phone numbers', function () {
310 // Brazil.
311 // `national_prefix_formatting_rule` is `($1)`.
312 // Should not add braces around `12` when being input in international format.
313 new AsYouType().input('+55123456789').should.equal('+55 12 3456 789');
314 new AsYouType('BR').input('+55123456789').should.equal('+55 12 3456 789');
315 new AsYouType('BR').input('123456789').should.equal('(12) 3456-789');
316 });
317 it('should support incorrectly entered international phone numbers (with a national prefix)', function () {
318 var formatter;
319 formatter = new AsYouType();
320 formatter.input('+1 1 877 215 5230').should.equal('+1 1 877 215 5230'); // formatter.input('+1 1 877 215 5230').should.equal('+1 1 8772155230')
321
322 formatter.getNationalNumber().should.equal('8772155230');
323 formatter = new AsYouType();
324 formatter.input('+78800555353').should.equal('+7 880 055 53 53');
325 formatter.input('5').should.equal('+7 8 800 555 35 35');
326 formatter.getNationalNumber().should.equal('8005553535');
327 });
328 it('should return a partial template for current value', function () {
329 var asYouType = new AsYouType('US');
330 asYouType.input('').should.equal('');
331 asYouType.getTemplate().should.equal('');
332 asYouType.input('2').should.equal('2'); // asYouType.getTemplate().should.equal('x')
333 // Doesn't format for a single digit.
334
335 asYouType.getTemplate().should.equal('x');
336 asYouType.input('1').should.equal('21');
337 asYouType.getTemplate().should.equal('xx');
338 asYouType.input('3').should.equal('(213)');
339 asYouType.getTemplate().should.equal('(xxx)');
340 });
341 it("should fall back to the default country", function () {
342 var formatter = new AsYouType('RU');
343 formatter.input('8').should.equal('8');
344 formatter.input('9').should.equal('8 9');
345 formatter.input('9').should.equal('8 99');
346 formatter.input('9').should.equal('8 (999)'); // formatter.valid.should.be.false
347
348 formatter.formatter.template.should.equal('x (xxx) xxx-xx-xx');
349 formatter.getCountry().should.equal('RU'); // formatter.getCountryCallingCode().should.equal('7')
350
351 formatter.input('000000000000').should.equal('8999000000000000'); // formatter.valid.should.be.false
352
353 expect(formatter.formatter.template).to.be.undefined;
354 formatter.getCountry().should.equal('RU'); // formatter.getCountryCallingCode().should.equal('7')
355
356 formatter.reset(); // formatter.valid.should.be.false
357
358 expect(formatter.formatter.template).to.be.undefined;
359 expect(formatter.getCountry()).to.be.undefined; // formatter.getCountryCallingCode().should.equal('7')
360
361 formatter.input('+1-213-373-4253').should.equal('+1 213 373 4253'); // formatter.valid.should.be.true
362
363 formatter.getTemplate().should.equal('xx xxx xxx xxxx');
364 formatter.getCountry().should.equal('US');
365 formatter.getCountryCallingCode().should.equal('1');
366 });
367 it('should work in edge cases', function () {
368 var formatter;
369 var thrower; // No metadata
370
371 thrower = function thrower() {
372 return new _AsYouType["default"]('RU');
373 };
374
375 thrower.should["throw"]('`metadata` argument not passed'); // Second '+' sign
376
377 formatter = new AsYouType('RU');
378 formatter.input('+').should.equal('+');
379 formatter.input('7').should.equal('+7');
380 formatter.input('+').should.equal('+7'); // Out-of-position '+' sign
381
382 formatter = new AsYouType('RU');
383 formatter.input('8').should.equal('8');
384 formatter.input('+').should.equal('8'); // No format matched
385
386 formatter = new AsYouType('RU');
387 formatter.input('88005553535').should.equal('8 (800) 555-35-35');
388 formatter.input('0').should.equal('880055535350'); // Invalid country phone code
389
390 formatter = new AsYouType();
391 formatter.input('+0123').should.equal('+0123'); // No country specified and not an international number
392
393 formatter = new AsYouType();
394 formatter.input('88005553535').should.equal('88005553535'); // Extract national prefix when no `national_prefix` is set
395
396 formatter = new AsYouType('AD');
397 formatter.input('155555').should.equal('155 555'); // Typing nonsense
398
399 formatter = new AsYouType('RU');
400 formatter.input('+1abc2').should.equal('+1'); // Should reset default country when explicitly
401 // typing in an international phone number
402
403 formatter = new AsYouType('RU');
404 formatter.input('+');
405 type(formatter.getCountry()).should.equal('undefined');
406 type(formatter.getCountryCallingCode()).should.equal('undefined'); // Country not inferrable from the phone number,
407 // while the phone number itself can already be formatted "completely".
408
409 formatter = new AsYouType();
410 formatter.input('+12223333333');
411 type(formatter.getCountry()).should.equal('undefined');
412 formatter.getCountryCallingCode().should.equal('1'); // Reset a chosen format when it no longer applies given the new leading digits.
413 // If Google changes metadata for England then this test might not cover the case.
414
415 formatter = new AsYouType('GB');
416 formatter.input('0845').should.equal('0845'); // New leading digits don't match the format previously chosen.
417 // Reset the format.
418
419 formatter.input('0').should.equal('0845 0');
420 });
421 it('should choose between matching formats based on the absence or presence of a national prefix', function () {
422 // The first matching format:
423 // {
424 // "pattern": "(\\d{2})(\\d{5,6})",
425 // "leading_digits_patterns": [
426 // "(?:10|2[0-57-9])[19]",
427 // "(?:10|2[0-57-9])(?:10|9[56])",
428 // "(?:10|2[0-57-9])(?:100|9[56])"
429 // ],
430 // "national_prefix_formatting_rule": "0$1",
431 // "format": "$1 $2",
432 // "domestic_carrier_code_formatting_rule": "$CC $FG"
433 // }
434 //
435 // The second matching format:
436 // {
437 // "pattern": "(\\d{2})(\\d{4})(\\d{4})",
438 // "leading_digits_patterns": [
439 // "10|2(?:[02-57-9]|1[1-9])",
440 // "10|2(?:[02-57-9]|1[1-9])",
441 // "10[0-79]|2(?:[02-57-9]|1[1-79])|(?:10|21)8(?:0[1-9]|[1-9])"
442 // ],
443 // "national_prefix_formatting_rule": "0$1",
444 // "national_prefix_is_optional_when_formatting": true,
445 // "format": "$1 $2 $3",
446 // "domestic_carrier_code_formatting_rule": "$CC $FG"
447 // }
448 //
449 var formatter = new AsYouType('CN'); // National prefix has been input.
450 // Chooses the first format.
451
452 formatter.input('01010000').should.equal('010 10000');
453 formatter.reset(); // No national prefix has been input,
454 // and `national_prefix_for_parsing` not matched.
455 // The first format won't match, because it doesn't have
456 // `"national_prefix_is_optional_when_formatting": true`.
457 // The second format will match, because it does have
458 // `"national_prefix_is_optional_when_formatting": true`.
459
460 formatter.input('1010000').should.equal('10 1000 0');
461 });
462 it('should not accept phone number extensions', function () {
463 new AsYouType().input('+1-213-373-4253 ext. 123').should.equal('+1 213 373 4253');
464 });
465 it('should parse non-European digits', function () {
466 new AsYouType().input('+١٢١٢٢٣٢٣٢٣٢').should.equal('+1 212 232 3232');
467 });
468 it('should return a PhoneNumber instance', function () {
469 var formatter = new AsYouType('BR'); // No country calling code.
470
471 expect(formatter.getNumber()).to.be.undefined;
472 formatter.input('+1'); // No national number digits.
473
474 expect(formatter.getNumber()).to.be.undefined;
475 formatter.input('213-373-4253');
476 var phoneNumber = formatter.getNumber();
477 phoneNumber.country.should.equal('US');
478 phoneNumber.countryCallingCode.should.equal('1');
479 phoneNumber.number.should.equal('+12133734253');
480 phoneNumber.nationalNumber.should.equal('2133734253');
481 formatter.reset();
482 formatter.input('+1-113-373-4253');
483 phoneNumber = formatter.getNumber();
484 expect(phoneNumber.country).to.be.undefined;
485 phoneNumber.countryCallingCode.should.equal('1'); // An incorrect NANPA international phone number.
486 // (contains national prefix in an international phone number)
487
488 formatter.reset();
489 formatter.input('+1-1');
490 phoneNumber = formatter.getNumber();
491 expect(phoneNumber).to.not.be.undefined;
492 formatter.input('1');
493 phoneNumber = formatter.getNumber();
494 expect(phoneNumber.country).to.be.undefined;
495 phoneNumber.countryCallingCode.should.equal('1');
496 phoneNumber.number.should.equal('+111');
497 });
498 it('should work with countries that add digits to national (significant) number', function () {
499 // When formatting Argentinian mobile numbers in international format,
500 // a `9` is prepended, when compared to national format.
501 var asYouType = new AsYouType('AR');
502 asYouType.input('+5493435551212').should.equal('+54 9 3435 55 1212');
503 asYouType.reset(); // Digits shouldn't be changed when formatting in national format.
504 // (no `9` is prepended).
505 // First parses national (significant) number by prepending `9` to it
506 // and stripping `15` from it.
507 // Then uses `$2 15-$3-$4` format that strips the leading `9`
508 // and adds `15`.
509
510 asYouType.input('0343515551212').should.equal('03435 15-55-1212');
511 });
512 it('should return non-formatted phone number when no format matches and national (significant) number has digits added', function () {
513 // When formatting Argentinian mobile numbers in international format,
514 // a `9` is prepended, when compared to national format.
515 var asYouType = new AsYouType('AR'); // Digits shouldn't be changed when formatting in national format.
516 // (no `9` is prepended).
517 // First parses national (significant) number by prepending `9` to it
518 // and stripping `15` from it.
519 // Then uses `$2 15-$3-$4` format that strips the leading `9`
520 // and adds `15`.
521 // `this.nationalSignificantNumberMatchesInput` is `false` in this case,
522 // so `getNonFormattedNumber()` returns `getFullNumber(getNationalDigits())`.
523
524 asYouType.input('0343515551212999').should.equal('0343515551212999');
525 });
526 it('should format Argentina numbers (starting with 011) (digit by digit)', function () {
527 // Inputting a number digit-by-digit and as a whole a two different cases
528 // in case of this library compared to Google's `libphonenumber`
529 // that always inputs a number digit-by-digit.
530 // https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/23
531 // nextDigits 0111523456789
532 // nationalNumber 91123456789
533 var formatter = new AsYouType('AR');
534 formatter.input('0').should.equal('0');
535 formatter.getTemplate().should.equal('x');
536 formatter.input('1').should.equal('01');
537 formatter.getTemplate().should.equal('xx');
538 formatter.input('1').should.equal('011');
539 formatter.getTemplate().should.equal('xxx');
540 formatter.input('1').should.equal('011 1');
541 formatter.getTemplate().should.equal('xxx x');
542 formatter.input('5').should.equal('011 15');
543 formatter.getTemplate().should.equal('xxx xx');
544 formatter.input('2').should.equal('011 152');
545 formatter.getTemplate().should.equal('xxx xxx');
546 formatter.input('3').should.equal('011 1523');
547 formatter.getTemplate().should.equal('xxx xxxx');
548 formatter.input('4').should.equal('011 1523-4');
549 formatter.getTemplate().should.equal('xxx xxxx-x');
550 formatter.input('5').should.equal('011 1523-45');
551 formatter.getTemplate().should.equal('xxx xxxx-xx');
552 formatter.input('6').should.equal('011 1523-456');
553 formatter.getTemplate().should.equal('xxx xxxx-xxx');
554 formatter.input('7').should.equal('011 1523-4567');
555 formatter.getTemplate().should.equal('xxx xxxx-xxxx');
556 formatter.input('8').should.equal('011152345678');
557 formatter.getTemplate().should.equal('xxxxxxxxxxxx');
558 formatter.input('9').should.equal('011 15-2345-6789');
559 formatter.getTemplate().should.equal('xxx xx-xxxx-xxxx'); // Private property (not public API).
560
561 formatter.state.nationalSignificantNumber.should.equal('91123456789'); // Private property (not public API).
562 // `formatter.digits` is not always `formatter.nationalPrefix`
563 // plus `formatter.nationalNumberDigits`.
564
565 formatter.state.nationalPrefix.should.equal('0');
566 formatter.isPossible().should.equal(true);
567 formatter.isValid().should.equal(true);
568 });
569 it('should format Argentina numbers (starting with 011)', function () {
570 // Inputting a number digit-by-digit and as a whole a two different cases
571 // in case of this library compared to Google's `libphonenumber`
572 // that always inputs a number digit-by-digit.
573 // https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/23
574 // nextDigits 0111523456789
575 // nationalNumber 91123456789
576 var formatter = new AsYouType('AR');
577 formatter.input('0111523456789').should.equal('011 15-2345-6789'); // Private property (not public API).
578
579 formatter.state.nationalSignificantNumber.should.equal('91123456789'); // Private property (not public API).
580 // `formatter.digits` is not always `formatter.nationalPrefix`
581 // plus `formatter.nationalNumberDigits`.
582
583 expect(formatter.state.nationalPrefix).to.equal('0'); // expect(formatter.nationalPrefix).to.be.undefined
584
585 formatter.isPossible().should.equal(true);
586 formatter.isValid().should.equal(true);
587 });
588 it('should prepend `complexPrefixBeforeNationalSignificantNumber` (not a complete number)', function () {
589 // A country having `national_prefix_for_parsing` with a "capturing group".
590 // National prefix is either not used in a format or is optional.
591 // Input phone number without a national prefix.
592 var formatter = new AsYouType('AU');
593 formatter.input('1831130345678').should.equal('1831 1303 456 78'); // Private property (not public API).
594
595 formatter.state.nationalSignificantNumber.should.equal('130345678'); // Private property (not public API).
596 // `formatter.digits` is not always `formatter.nationalPrefix`
597 // plus `formatter.nationalNumberDigits`.
598
599 expect(formatter.state.nationalPrefix).to.be.undefined;
600 formatter.state.complexPrefixBeforeNationalSignificantNumber.should.equal('1831');
601 });
602 it('should prepend `complexPrefixBeforeNationalSignificantNumber` (complete number)', function () {
603 // A country having `national_prefix_for_parsing` with a "capturing group".
604 // National prefix is either not used in a format or is optional.
605 // Input phone number without a national prefix.
606 var formatter = new AsYouType('AU');
607 formatter.input('18311303456789').should.equal('1831 1303 456 789'); // Private property (not public API).
608
609 formatter.state.nationalSignificantNumber.should.equal('1303456789'); // Private property (not public API).
610 // `formatter.digits` is not always `formatter.nationalPrefix`
611 // plus `formatter.nationalNumberDigits`.
612
613 expect(formatter.state.nationalPrefix).to.be.undefined;
614 formatter.state.complexPrefixBeforeNationalSignificantNumber.should.equal('1831');
615 });
616 it('should work with Mexico numbers', function () {
617 var asYouType = new AsYouType('MX'); // Fixed line.
618
619 asYouType.input('+52(449)978-000').should.equal('+52 449 978 000');
620 asYouType.input('1').should.equal('+52 449 978 0001');
621 asYouType.reset();
622 asYouType.input('01449978000').should.equal('01449 978 000');
623 asYouType.getTemplate().should.equal('xxxxx xxx xxx');
624 asYouType.input('1').should.equal('01449 978 0001');
625 asYouType.getTemplate().should.equal('xxxxx xxx xxxx');
626 asYouType.reset();
627 asYouType.input('(449)978-000').should.equal('449 978 000');
628 asYouType.getTemplate().should.equal('xxx xxx xxx');
629 asYouType.input('1').should.equal('449 978 0001');
630 asYouType.getTemplate().should.equal('xxx xxx xxxx'); // Mobile.
631 // `1` is prepended before area code to mobile numbers in international format.
632
633 asYouType.reset();
634 asYouType.input('+521331234567').should.equal('+52 13 3123 4567');
635 asYouType.getTemplate().should.equal('xxx xx xxxx xxxx'); // Google's `libphonenumber` seems to not able to format this type of number.
636 // https://issuetracker.google.com/issues/147938979
637
638 asYouType.input('8').should.equal('+52 133 1234 5678');
639 asYouType.getTemplate().should.equal('xxx xxx xxxx xxxx');
640 asYouType.reset();
641 asYouType.input('+52331234567').should.equal('+52 33 1234 567');
642 asYouType.input('8').should.equal('+52 33 1234 5678');
643 asYouType.reset();
644 asYouType.input('044331234567').should.equal('04433 1234 567');
645 asYouType.input('8').should.equal('04433 1234 5678');
646 asYouType.reset();
647 asYouType.input('045331234567').should.equal('04533 1234 567');
648 asYouType.input('8').should.equal('04533 1234 5678');
649 });
650 it('should just prepend national prefix if national_prefix_formatting_rule does not produce a suitable number', function () {
651 // "national_prefix": "8"
652 // "national_prefix_for_parsing": "0|80?"
653 var formatter = new AsYouType('BY'); // "national_prefix_formatting_rule": "8 $1"
654 // That `national_prefix_formatting_rule` isn't used
655 // because the user didn't input national prefix `8`.
656
657 formatter.input('0800123').should.equal('0 800 123');
658 formatter.getTemplate().should.equal('x xxx xxx');
659 });
660 it('should not duplicate area code for certain countries', function () {
661 // https://github.com/catamphetamine/libphonenumber-js/issues/318
662 var asYouType = new AsYouType('VI'); // Even though `parse("3406934")` would return a
663 // "(340) 340-6934" national number, still
664 // "As You Type" formatter should leave it as "(340) 6934".
665
666 asYouType.input('340693').should.equal('(340) 693');
667 asYouType.input('4').should.equal('(340) 693-4');
668 asYouType.input('123').should.equal('(340) 693-4123');
669 });
670 it('shouldn\'t throw when passed a non-existent default country', function () {
671 new AsYouType('XX').input('+78005553535').should.equal('+7 800 555 35 35');
672 new AsYouType('XX').input('88005553535').should.equal('88005553535');
673 });
674 it('should parse carrier codes', function () {
675 var formatter = new AsYouType('BR');
676 formatter.input('0 15 21 5555-5555');
677 var phoneNumber = formatter.getNumber();
678 phoneNumber.carrierCode.should.equal('15');
679 formatter.reset();
680 formatter.input('+1-213-373-4253');
681 phoneNumber = formatter.getNumber();
682 expect(phoneNumber.carrierCode).to.be.undefined;
683 });
684 it('should format when default country calling code is configured', function () {
685 var formatter = new AsYouType({
686 defaultCallingCode: '7'
687 });
688 formatter.input('88005553535').should.equal('8 (800) 555-35-35');
689 formatter.getNumber().countryCallingCode.should.equal('7');
690 formatter.getNumber().country.should.equal('RU');
691 });
692 it('shouldn\'t return PhoneNumber if country calling code hasn\'t been input yet', function () {
693 var formatter = new AsYouType();
694 formatter.input('+80');
695 expect(formatter.getNumber()).to.be.undefined;
696 });
697 it('should format non-geographic numbering plan phone numbers', function () {
698 var formatter = new AsYouType();
699 formatter.input('+').should.equal('+');
700 formatter.input('8').should.equal('+8');
701 formatter.input('7').should.equal('+87');
702 expect(formatter.getCountry()).to.be.undefined;
703 formatter.input('0').should.equal('+870');
704
705 if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
706 formatter.getCountry().should.equal('001');
707 } else {
708 expect(formatter.getCountry()).to.be.undefined;
709 }
710
711 formatter.input('7').should.equal('+870 7');
712 formatter.input('7').should.equal('+870 77');
713 formatter.input('3').should.equal('+870 773');
714 formatter.input('1').should.equal('+870 773 1');
715 formatter.input('1').should.equal('+870 773 11');
716 formatter.input('1').should.equal('+870 773 111');
717 formatter.input('6').should.equal('+870 773 111 6');
718 formatter.input('3').should.equal('+870 773 111 63');
719 formatter.input('2').should.equal('+870 773 111 632');
720
721 if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
722 formatter.getNumber().country.should.equal('001');
723 } else {
724 expect(formatter.getCountry()).to.be.undefined;
725 }
726
727 formatter.getNumber().countryCallingCode.should.equal('870');
728 });
729 it('should format non-geographic numbering plan phone numbers (default country calling code)', function () {
730 var formatter = new AsYouType({
731 defaultCallingCode: '870'
732 });
733
734 if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
735 formatter.getNumber().country.should.equal('001');
736 } else {
737 expect(formatter.getCountry()).to.be.undefined;
738 }
739
740 formatter.input('7').should.equal('7');
741 formatter.input('7').should.equal('77');
742 formatter.input('3').should.equal('773');
743 formatter.input('1').should.equal('773 1');
744 formatter.input('1').should.equal('773 11');
745 formatter.input('1').should.equal('773 111');
746 formatter.input('6').should.equal('773 111 6');
747 formatter.input('3').should.equal('773 111 63');
748 formatter.input('2').should.equal('773 111 632');
749
750 if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
751 formatter.getNumber().country.should.equal('001');
752 } else {
753 expect(formatter.getCountry()).to.be.undefined;
754 }
755
756 formatter.getNumber().countryCallingCode.should.equal('870');
757 });
758 it('should not format non-geographic numbering plan phone numbers (default country 001)', function () {
759 var formatter = new AsYouType('001');
760 expect(formatter.defaultCountry).to.be.undefined;
761 expect(formatter.defaultCallingCode).to.be.undefined;
762 formatter.input('7').should.equal('7');
763 formatter.input('7').should.equal('77');
764 formatter.input('3').should.equal('773');
765 formatter.input('1').should.equal('7731');
766 formatter.input('1').should.equal('77311');
767 formatter.input('1').should.equal('773111');
768 formatter.input('6').should.equal('7731116');
769 formatter.input('3').should.equal('77311163');
770 formatter.input('2').should.equal('773111632');
771 expect(formatter.getCountry()).to.be.undefined;
772 expect(formatter.getNumber()).to.be.undefined;
773 });
774 it('should return PhoneNumber (should strip national prefix `1` in E.164 value)', function () {
775 var formatter = new AsYouType('RU');
776 formatter.input('+1111');
777 formatter.getNumber().number.should.equal('+111');
778 });
779 it('should return PhoneNumber with autocorrected international numbers without leading +', function () {
780 // https://github.com/catamphetamine/libphonenumber-js/issues/316
781 var formatter = new AsYouType('FR');
782 formatter.input('33612902554').should.equal('33 6 12 90 25 54');
783 formatter.getNumber().country.should.equal('FR');
784 formatter.getNumber().nationalNumber.should.equal('612902554');
785 formatter.getNumber().number.should.equal('+33612902554'); // Should also strip national prefix.
786
787 formatter.reset();
788 formatter.input('330612902554').should.equal('33 06 12 90 25 54');
789 formatter.getNumber().country.should.equal('FR');
790 formatter.getNumber().nationalNumber.should.equal('612902554');
791 formatter.getNumber().number.should.equal('+33612902554'); // On second thought, this "prepend default area code" feature won't be added,
792 // because when a user selects "British Virgin Islands" and inputs
793 // "2291234", then they see "(229) 123-4" which clearly indicates that
794 // they should input the complete phone number (with area code).
795 // So, unless a user completely doesn't understand what they're doing,
796 // they'd input the complete phone number (with area code).
797 // // Should prepend the default area code in British Virgin Islands.
798 // // https://github.com/catamphetamine/react-phone-number-input/issues/335
799 // const formatter2 = new AsYouType('VG')
800 // formatter2.input('2291234').should.equal('(229) 123-4')
801 // formatter2.getNumber().country.should.equal('VG')
802 // formatter2.getNumber().nationalNumber.should.equal('2842291234')
803 // formatter2.getNumber().number.should.equal('+12842291234')
804 });
805 it('should work with out-of-country dialing prefix (like 00)', function () {
806 var formatter = new AsYouType('DE');
807 formatter.input('00498911196611').should.equal('00 49 89 11196611');
808 formatter.getCountry().should.equal('DE');
809 formatter.formatter.template.should.equal('xx xx xx xxxxxxxx');
810 formatter.formatter.populatedNationalNumberTemplate.should.equal('89 11196611');
811 formatter.getTemplate().should.equal('xx xx xx xxxxxxxx');
812 formatter.getNumber().country.should.equal('DE');
813 formatter.getNumber().nationalNumber.should.equal('8911196611');
814 formatter.getNumber().number.should.equal('+498911196611');
815 });
816 it('shouldn\'t choose a format when there\'re too many digits for any of them', function () {
817 var formatter = new AsYouType('RU');
818 formatter.input('88005553535');
819 formatter.formatter.chosenFormat.format().should.equal('$1 $2-$3-$4');
820 formatter.reset();
821 formatter.input('880055535355');
822 expect(formatter.formatter.chosenFormat).to.be.undefined;
823 });
824 it('should get separator after national prefix', function () {
825 // Russia.
826 // Has separator after national prefix.
827 var formatter = new AsYouType('RU');
828 var format = formatter.metadata.formats()[0];
829 format.nationalPrefixFormattingRule().should.equal('8 ($1)');
830 formatter.formatter.getSeparatorAfterNationalPrefix(format).should.equal(' '); // Britain.
831 // Has no separator after national prefix.
832
833 var formatter2 = new AsYouType('GB');
834 var format2 = formatter2.metadata.formats()[0];
835 format2.nationalPrefixFormattingRule().should.equal('0$1');
836 formatter2.formatter.getSeparatorAfterNationalPrefix(format2).should.equal('');
837 });
838 it('should return if the number is possible', function () {
839 // National. Russia.
840 var formatter = new AsYouType('RU');
841 formatter.isPossible().should.equal(false);
842 formatter.input('8');
843 formatter.isPossible().should.equal(false);
844 formatter.input('8005553535');
845 formatter.isPossible().should.equal(true);
846 formatter.input('5');
847 formatter.isPossible().should.equal(false);
848 });
849 it('should return if the number is valid', function () {
850 // National. Russia.
851 var formatter = new AsYouType('RU');
852 formatter.isValid().should.equal(false);
853 formatter.input('88005553535');
854 formatter.isValid().should.equal(true);
855 formatter.input('5');
856 formatter.isValid().should.equal(false);
857 });
858 it('should return if the number is international', function () {
859 // National. Russia.
860 var formatter = new AsYouType('RU');
861 formatter.isInternational().should.equal(false);
862 formatter.input('88005553535');
863 formatter.isInternational().should.equal(false); // International. Russia.
864
865 var formatterInt = new AsYouType();
866 formatterInt.isInternational().should.equal(false);
867 formatterInt.input('+');
868 formatterInt.isInternational().should.equal(true);
869 formatterInt.input('78005553535');
870 formatterInt.isInternational().should.equal(true);
871 });
872 it('should return country calling code part of the number', function () {
873 // National. Russia.
874 var formatter = new AsYouType('RU');
875 expect(formatter.getCountryCallingCode()).to.be.undefined;
876 formatter.input('88005553535');
877 expect(formatter.getCountryCallingCode()).to.be.undefined; // International. Russia.
878
879 var formatterInt = new AsYouType();
880 expect(formatterInt.getCountryCallingCode()).to.be.undefined;
881 formatterInt.input('+');
882 expect(formatterInt.getCountryCallingCode()).to.be.undefined;
883 formatterInt.input('7');
884 expect(formatterInt.getCountryCallingCode()).to.equal('7');
885 formatterInt.input('8005553535');
886 expect(formatterInt.getCountryCallingCode()).to.equal('7');
887 });
888 it('should return the country of the number', function () {
889 // National. Russia.
890 var formatter = new AsYouType('RU');
891 expect(formatter.getCountry()).to.be.undefined;
892 formatter.input('8');
893 expect(formatter.getCountry()).to.equal('RU');
894 formatter.input('8005553535');
895 expect(formatter.getCountry()).to.equal('RU'); // International. Austria.
896
897 var formatterInt = new AsYouType();
898 expect(formatterInt.getCountry()).to.be.undefined;
899 formatterInt.input('+');
900 expect(formatterInt.getCountry()).to.be.undefined;
901 formatterInt.input('43');
902 expect(formatterInt.getCountry()).to.equal('AT'); // International. USA.
903
904 var formatterIntRu = new AsYouType();
905 expect(formatterIntRu.getCountry()).to.be.undefined;
906 formatterIntRu.input('+');
907 expect(formatterIntRu.getCountry()).to.be.undefined;
908 formatterIntRu.input('1');
909 expect(formatterIntRu.getCountry()).to.be.undefined;
910 formatterIntRu.input('2133734253');
911 expect(formatterIntRu.getCountry()).to.equal('US');
912 formatterIntRu.input('1');
913 expect(formatterIntRu.getCountry()).to.be.undefined;
914 });
915 it('should parse a long IDD prefix', function () {
916 var formatter = new AsYouType('AU'); // `14880011` is a long IDD prefix in Australia.
917
918 formatter.input('1').should.equal('1');
919 formatter.input('4').should.equal('14');
920 formatter.input('8').should.equal('148');
921 formatter.input('8').should.equal('1488');
922 formatter.input('0').should.equal('14880');
923 formatter.input('0').should.equal('148800');
924 formatter.input('1').should.equal('1488001');
925 formatter.input('1').should.equal('14880011'); // As if were calling US using `14880011` IDD prefix,
926 // though that prefix could mean something else.
927
928 formatter.input('1').should.equal('14880011 1');
929 formatter.input('2').should.equal('14880011 1 2');
930 formatter.input('1').should.equal('14880011 1 21');
931 formatter.input('3').should.equal('14880011 1 213');
932 });
933 it('should return the phone number characters entered by the user', function () {
934 var formatter = new AsYouType('RU');
935 formatter.getChars().should.equal('');
936 formatter.input('+123');
937 formatter.getChars().should.equal('+123');
938 formatter.reset();
939 formatter.input('123');
940 formatter.getChars().should.equal('123');
941 });
942});
943
944function type(something) {
945 return _typeof(something);
946}
947//# sourceMappingURL=AsYouType.test.js.map
\No newline at end of file