1 | "use strict";
|
2 |
|
3 | var _metadataMin = _interopRequireDefault(require("../metadata.min.json"));
|
4 |
|
5 | var _AsYouType = _interopRequireDefault(require("./AsYouType"));
|
6 |
|
7 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
8 |
|
9 | function _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 |
|
11 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
12 |
|
13 | function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
|
14 |
|
15 | function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
16 |
|
17 | function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
18 |
|
19 | function _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 |
|
21 | function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
22 |
|
23 | var AsYouType =
|
24 |
|
25 | function (_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 |
|
37 | var USE_NON_GEOGRAPHIC_COUNTRY_CODE = false;
|
38 | describe('AsYouType', function () {
|
39 | it('should use "national_prefix_formatting_rule"', function () {
|
40 |
|
41 | new AsYouType('RU').input('88005553535').should.equal('8 (800) 555-35-35');
|
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');
|
68 |
|
69 | formatter.formatter.template.should.equal('xx xxx xxx xxxx');
|
70 | formatter.formatter.populatedNationalNumberTemplate.should.equal('xxx xxx xxxx');
|
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');
|
89 |
|
90 | formatter.formatter.populatedNationalNumberTemplate.should.equal('xxx xxx xxxx');
|
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 |
|
105 | new AsYouType().input('+12133734').should.equal('+1 213 373 4');
|
106 |
|
107 | new AsYouType('US').input('2133734').should.equal('(213) 373-4');
|
108 |
|
109 | new AsYouType('US').input('12133734').should.equal('1 (213) 373-4');
|
110 |
|
111 | new AsYouType('US').input('12133734253').should.equal('1 (213) 373-4253');
|
112 | var formatter;
|
113 |
|
114 |
|
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');
|
118 |
|
119 | formatter = new AsYouType();
|
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('+');
|
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');
|
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');
|
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');
|
147 |
|
148 | type(formatter.getCountry()).should.equal('undefined');
|
149 | formatter.input('4').should.equal('+1 213 333 4444');
|
150 |
|
151 | formatter.getCountry().should.equal('US');
|
152 |
|
153 |
|
154 | formatter.getTemplate().should.equal('xx xxx xxx xxxx');
|
155 | formatter.input('5').should.equal('+1 21333344445');
|
156 |
|
157 | expect(formatter.getCountry()).to.be.undefined;
|
158 | formatter.getCountryCallingCode().should.equal('1');
|
159 | expect(formatter.formatter.template).to.be.undefined;
|
160 |
|
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');
|
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');
|
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');
|
187 |
|
188 | formatter.getCountry().should.equal('CH');
|
189 | formatter.getTemplate().should.equal('xxx xxx xx xx');
|
190 | formatter.input('9').should.equal('04466818009');
|
191 |
|
192 | formatter.getCountry().should.equal('CH');
|
193 | expect(formatter.formatter.template).to.be.undefined;
|
194 |
|
195 | formatter = new AsYouType();
|
196 | formatter.input('+77172580659');
|
197 | formatter.getCountry().should.equal('KZ');
|
198 |
|
199 | formatter = new AsYouType('BR');
|
200 | formatter.input('11987654321').should.equal('(11) 98765-4321');
|
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');
|
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');
|
210 |
|
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');
|
221 |
|
222 |
|
223 | formatter = new AsYouType('RU');
|
224 |
|
225 | formatter.input('+78005553535').should.equal('+7 800 555 35 35');
|
226 | formatter.getNationalNumber().should.equal('8005553535');
|
227 |
|
228 |
|
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');
|
239 |
|
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');
|
244 |
|
245 |
|
246 | new AsYouType('BG').input('111 222 3').should.equal('1112223');
|
247 |
|
248 | new AsYouType().input('+4915539898001').should.equal('+49 15539 898001');
|
249 |
|
250 | formatter = new AsYouType();
|
251 | formatter.input('+7 702 211 1111');
|
252 | formatter.getCountry().should.equal('KZ');
|
253 |
|
254 |
|
255 | new AsYouType('NZ').input('0212').should.equal('021 2');
|
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 |
|
263 | var formatter = new AsYouType('AF');
|
264 |
|
265 |
|
266 | formatter.input('44444444').should.equal('44444444');
|
267 | expect(formatter.formatter.template).to.be.undefined;
|
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 |
|
274 |
|
275 | var formatter = new AsYouType('RU');
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
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');
|
303 |
|
304 | formatter.input('9991234567').should.equal('999 123-45-67');
|
305 | formatter.reset();
|
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 |
|
311 |
|
312 |
|
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');
|
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');
|
333 |
|
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)');
|
347 |
|
348 | formatter.formatter.template.should.equal('x (xxx) xxx-xx-xx');
|
349 | formatter.getCountry().should.equal('RU');
|
350 |
|
351 | formatter.input('000000000000').should.equal('8999000000000000');
|
352 |
|
353 | expect(formatter.formatter.template).to.be.undefined;
|
354 | formatter.getCountry().should.equal('RU');
|
355 |
|
356 | formatter.reset();
|
357 |
|
358 | expect(formatter.formatter.template).to.be.undefined;
|
359 | expect(formatter.getCountry()).to.be.undefined;
|
360 |
|
361 | formatter.input('+1-213-373-4253').should.equal('+1 213 373 4253');
|
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;
|
370 |
|
371 | thrower = function thrower() {
|
372 | return new _AsYouType["default"]('RU');
|
373 | };
|
374 |
|
375 | thrower.should["throw"]('`metadata` argument not passed');
|
376 |
|
377 | formatter = new AsYouType('RU');
|
378 | formatter.input('+').should.equal('+');
|
379 | formatter.input('7').should.equal('+7');
|
380 | formatter.input('+').should.equal('+7');
|
381 |
|
382 | formatter = new AsYouType('RU');
|
383 | formatter.input('8').should.equal('8');
|
384 | formatter.input('+').should.equal('8');
|
385 |
|
386 | formatter = new AsYouType('RU');
|
387 | formatter.input('88005553535').should.equal('8 (800) 555-35-35');
|
388 | formatter.input('0').should.equal('880055535350');
|
389 |
|
390 | formatter = new AsYouType();
|
391 | formatter.input('+0123').should.equal('+0123');
|
392 |
|
393 | formatter = new AsYouType();
|
394 | formatter.input('88005553535').should.equal('88005553535');
|
395 |
|
396 | formatter = new AsYouType('AD');
|
397 | formatter.input('155555').should.equal('155 555');
|
398 |
|
399 | formatter = new AsYouType('RU');
|
400 | formatter.input('+1abc2').should.equal('+1');
|
401 |
|
402 |
|
403 | formatter = new AsYouType('RU');
|
404 | formatter.input('+');
|
405 | type(formatter.getCountry()).should.equal('undefined');
|
406 | type(formatter.getCountryCallingCode()).should.equal('undefined');
|
407 |
|
408 |
|
409 | formatter = new AsYouType();
|
410 | formatter.input('+12223333333');
|
411 | type(formatter.getCountry()).should.equal('undefined');
|
412 | formatter.getCountryCallingCode().should.equal('1');
|
413 |
|
414 |
|
415 | formatter = new AsYouType('GB');
|
416 | formatter.input('0845').should.equal('0845');
|
417 |
|
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 |
|
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 | var formatter = new AsYouType('CN');
|
450 |
|
451 |
|
452 | formatter.input('01010000').should.equal('010 10000');
|
453 | formatter.reset();
|
454 |
|
455 |
|
456 |
|
457 |
|
458 |
|
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');
|
470 |
|
471 | expect(formatter.getNumber()).to.be.undefined;
|
472 | formatter.input('+1');
|
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');
|
486 |
|
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 |
|
500 |
|
501 | var asYouType = new AsYouType('AR');
|
502 | asYouType.input('+5493435551212').should.equal('+54 9 3435 55 1212');
|
503 | asYouType.reset();
|
504 |
|
505 |
|
506 |
|
507 |
|
508 |
|
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 |
|
514 |
|
515 | var asYouType = new AsYouType('AR');
|
516 |
|
517 |
|
518 |
|
519 |
|
520 |
|
521 |
|
522 |
|
523 |
|
524 | asYouType.input('0343515551212999').should.equal('0343515551212999');
|
525 | });
|
526 | it('should format Argentina numbers (starting with 011) (digit by digit)', function () {
|
527 |
|
528 |
|
529 |
|
530 |
|
531 |
|
532 |
|
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');
|
560 |
|
561 | formatter.state.nationalSignificantNumber.should.equal('91123456789');
|
562 |
|
563 |
|
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 |
|
571 |
|
572 |
|
573 |
|
574 |
|
575 |
|
576 | var formatter = new AsYouType('AR');
|
577 | formatter.input('0111523456789').should.equal('011 15-2345-6789');
|
578 |
|
579 | formatter.state.nationalSignificantNumber.should.equal('91123456789');
|
580 |
|
581 |
|
582 |
|
583 | expect(formatter.state.nationalPrefix).to.equal('0');
|
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 |
|
590 |
|
591 |
|
592 | var formatter = new AsYouType('AU');
|
593 | formatter.input('1831130345678').should.equal('1831 1303 456 78');
|
594 |
|
595 | formatter.state.nationalSignificantNumber.should.equal('130345678');
|
596 |
|
597 |
|
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 |
|
604 |
|
605 |
|
606 | var formatter = new AsYouType('AU');
|
607 | formatter.input('18311303456789').should.equal('1831 1303 456 789');
|
608 |
|
609 | formatter.state.nationalSignificantNumber.should.equal('1303456789');
|
610 |
|
611 |
|
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');
|
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');
|
631 |
|
632 |
|
633 | asYouType.reset();
|
634 | asYouType.input('+521331234567').should.equal('+52 13 3123 4567');
|
635 | asYouType.getTemplate().should.equal('xxx xx xxxx xxxx');
|
636 |
|
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 |
|
652 |
|
653 | var formatter = new AsYouType('BY');
|
654 |
|
655 |
|
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 |
|
662 | var asYouType = new AsYouType('VI');
|
663 |
|
664 |
|
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 |
|
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');
|
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');
|
792 |
|
793 |
|
794 |
|
795 |
|
796 |
|
797 |
|
798 |
|
799 |
|
800 |
|
801 |
|
802 |
|
803 |
|
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 |
|
826 |
|
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(' ');
|
831 |
|
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 |
|
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 |
|
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 |
|
860 | var formatter = new AsYouType('RU');
|
861 | formatter.isInternational().should.equal(false);
|
862 | formatter.input('88005553535');
|
863 | formatter.isInternational().should.equal(false);
|
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 |
|
874 | var formatter = new AsYouType('RU');
|
875 | expect(formatter.getCountryCallingCode()).to.be.undefined;
|
876 | formatter.input('88005553535');
|
877 | expect(formatter.getCountryCallingCode()).to.be.undefined;
|
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 |
|
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');
|
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');
|
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');
|
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');
|
926 |
|
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 |
|
944 | function type(something) {
|
945 | return _typeof(something);
|
946 | }
|
947 |
|
\ | No newline at end of file |