UNPKG

6.06 kBJavaScriptView Raw
1"use strict";
2
3var _parsePhoneNumber2 = _interopRequireDefault(require("./parsePhoneNumber"));
4
5var _metadataMin = _interopRequireDefault(require("../metadata.min.json"));
6
7var _metadataFull = _interopRequireDefault(require("../metadata.full.json"));
8
9function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
10
11function parsePhoneNumber() {
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 _parsePhoneNumber2["default"].apply(this, parameters);
18}
19
20function parsePhoneNumberFull() {
21 for (var _len2 = arguments.length, parameters = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
22 parameters[_key2] = arguments[_key2];
23 }
24
25 parameters.push(_metadataFull["default"]);
26 return _parsePhoneNumber2["default"].apply(this, parameters);
27}
28
29describe('parsePhoneNumber', function () {
30 it('should parse phone numbers', function () {
31 var phoneNumber = parsePhoneNumber('Phone: 8 (800) 555 35 35.', 'RU');
32 phoneNumber.country.should.equal('RU');
33 phoneNumber.countryCallingCode.should.equal('7');
34 phoneNumber.nationalNumber.should.equal('8005553535');
35 phoneNumber.number.should.equal('+78005553535');
36 phoneNumber.isPossible().should.equal(true);
37 phoneNumber.isValid().should.equal(true); // phoneNumber.isValidForRegion('RU').should.equal(true)
38 // Russian phone type regexps aren't included in default metadata.
39
40 parsePhoneNumberFull('Phone: 8 (800) 555 35 35.', 'RU').getType().should.equal('TOLL_FREE');
41 });
42 it('shouldn\'t set country when it\'s non-derivable', function () {
43 var phoneNumber = parsePhoneNumber('+7 111 555 35 35');
44 expect(phoneNumber.country).to.be.undefined;
45 phoneNumber.countryCallingCode.should.equal('7');
46 phoneNumber.nationalNumber.should.equal('1115553535');
47 });
48 it('should parse carrier code', function () {
49 var phoneNumber = parsePhoneNumber('0 15 21 5555-5555', 'BR');
50 phoneNumber.carrierCode.should.equal('15');
51 });
52 it('should parse phone extension', function () {
53 var phoneNumber = parsePhoneNumber('Phone: 8 (800) 555 35 35 ext. 1234.', 'RU');
54 phoneNumber.ext.should.equal('1234');
55 });
56 it('should validate numbers for countries with no type regular expressions', function () {
57 parsePhoneNumber('+380391234567').isValid().should.equal(true);
58 parsePhoneNumber('+380191234567').isValid().should.equal(false);
59 });
60 it('should format numbers', function () {
61 var phoneNumber = parsePhoneNumber('Phone: 8 (800) 555 35 35.', 'RU');
62 phoneNumber.format('NATIONAL').should.equal('8 (800) 555-35-35');
63 phoneNumber.formatNational().should.equal('8 (800) 555-35-35');
64 phoneNumber.format('INTERNATIONAL').should.equal('+7 800 555 35 35');
65 phoneNumber.formatInternational().should.equal('+7 800 555 35 35');
66 });
67 it('should get tel: URI', function () {
68 var phoneNumber = parsePhoneNumber('Phone: 8 (800) 555 35 35 ext. 1234.', 'RU');
69 phoneNumber.getURI().should.equal('tel:+78005553535;ext=1234');
70 });
71 it('should work in edge cases', function () {
72 expect(function () {
73 return parsePhoneNumber('+78005553535', -1, {});
74 }).to["throw"]('Invalid second argument');
75 });
76 it('should throw parse errors', function () {
77 expect(function () {
78 return parsePhoneNumber('8005553535', 'XX');
79 }).to["throw"]('INVALID_COUNTRY');
80 expect(function () {
81 return parsePhoneNumber('8', 'RU');
82 }).to["throw"]('NOT_A_NUMBER'); // Won't throw here because the regexp already demands length > 1.
83 // expect(() => parsePhoneNumber('11', 'RU')).to.throw('TOO_SHORT')
84
85 expect(function () {
86 return parsePhoneNumber('+443');
87 }).to["throw"]('TOO_SHORT');
88 expect(function () {
89 return parsePhoneNumber('+370');
90 }).to["throw"]('TOO_SHORT');
91 expect(function () {
92 return parsePhoneNumber('88888888888888888888', 'RU');
93 }).to["throw"]('TOO_LONG');
94 expect(function () {
95 return parsePhoneNumber('8 (800) 555 35 35');
96 }).to["throw"]('INVALID_COUNTRY');
97 expect(function () {
98 return parsePhoneNumber('+9991112233');
99 }).to["throw"]('INVALID_COUNTRY');
100 expect(function () {
101 return parsePhoneNumber('+9991112233', 'US');
102 }).to["throw"]('INVALID_COUNTRY');
103 expect(function () {
104 return parsePhoneNumber('8005553535 ', 'RU');
105 }).to["throw"]('TOO_LONG');
106 });
107 it('should parse incorrect international phone numbers', function () {
108 // Parsing national prefixes and carrier codes
109 // is only required for local phone numbers
110 // but some people don't understand that
111 // and sometimes write international phone numbers
112 // with national prefixes (or maybe even carrier codes).
113 // http://ucken.blogspot.ru/2016/03/trunk-prefixes-in-skype4b.html
114 // Google's original library forgives such mistakes
115 // and so does this library, because it has been requested:
116 // https://github.com/catamphetamine/libphonenumber-js/issues/127
117 var phoneNumber; // For complete numbers it should strip national prefix.
118
119 phoneNumber = parsePhoneNumber('+1 1877 215 5230');
120 phoneNumber.nationalNumber.should.equal('8772155230');
121 phoneNumber.country.should.equal('US'); // For complete numbers it should strip national prefix.
122
123 phoneNumber = parsePhoneNumber('+7 8800 555 3535');
124 phoneNumber.nationalNumber.should.equal('8005553535');
125 phoneNumber.country.should.equal('RU'); // For incomplete numbers it shouldn't strip national prefix.
126
127 phoneNumber = parsePhoneNumber('+7 8800 555 353');
128 phoneNumber.nationalNumber.should.equal('8800555353');
129 phoneNumber.country.should.equal('RU');
130 });
131});
132//# sourceMappingURL=parsePhoneNumber.test.js.map
\No newline at end of file