UNPKG

9.25 kBJavaScriptView Raw
1"use strict";
2
3var _metadataMin = _interopRequireDefault(require("../metadata.min.json"));
4
5var _format = _interopRequireDefault(require("./format"));
6
7function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
8
9function formatNumber() {
10 for (var _len = arguments.length, parameters = new Array(_len), _key = 0; _key < _len; _key++) {
11 parameters[_key] = arguments[_key];
12 }
13
14 parameters.push(_metadataMin["default"]);
15 return _format["default"].apply(this, parameters);
16}
17
18describe('format', function () {
19 it('should work with the first argument being a E.164 number', function () {
20 formatNumber('+12133734253', 'NATIONAL').should.equal('(213) 373-4253');
21 formatNumber('+12133734253', 'INTERNATIONAL').should.equal('+1 213 373 4253'); // Invalid number.
22
23 formatNumber('+12111111111', 'NATIONAL').should.equal('(211) 111-1111'); // Formatting invalid E.164 numbers.
24
25 formatNumber('+11111', 'INTERNATIONAL').should.equal('+1 1111');
26 formatNumber('+11111', 'NATIONAL').should.equal('1111');
27 });
28 it('should work with the first object argument expanded', function () {
29 formatNumber('2133734253', 'US', 'NATIONAL').should.equal('(213) 373-4253');
30 formatNumber('2133734253', 'US', 'INTERNATIONAL').should.equal('+1 213 373 4253');
31 });
32 it('should sort out the arguments', function () {
33 var options = {
34 formatExtension: function formatExtension(number, extension) {
35 return "".concat(number, " \u0434\u043E\u0431. ").concat(extension);
36 }
37 };
38 formatNumber({
39 phone: '8005553535',
40 country: 'RU',
41 ext: '123'
42 }, 'NATIONAL', options).should.equal('8 (800) 555-35-35 доб. 123'); // Parse number from string.
43
44 formatNumber('+78005553535', 'NATIONAL', options).should.equal('8 (800) 555-35-35');
45 formatNumber('8005553535', 'RU', 'NATIONAL', options).should.equal('8 (800) 555-35-35');
46 });
47 it('should format with national prefix when specifically instructed', function () {
48 // With national prefix.
49 formatNumber('88005553535', 'RU', 'NATIONAL').should.equal('8 (800) 555-35-35'); // Without national prefix via an explicitly set option.
50
51 formatNumber('88005553535', 'RU', 'NATIONAL', {
52 nationalPrefix: false
53 }).should.equal('800 555-35-35');
54 });
55 it('should format valid phone numbers', function () {
56 // Switzerland
57 formatNumber({
58 country: 'CH',
59 phone: '446681800'
60 }, 'INTERNATIONAL').should.equal('+41 44 668 18 00');
61 formatNumber({
62 country: 'CH',
63 phone: '446681800'
64 }, 'E.164').should.equal('+41446681800');
65 formatNumber({
66 country: 'CH',
67 phone: '446681800'
68 }, 'RFC3966').should.equal('tel:+41446681800');
69 formatNumber({
70 country: 'CH',
71 phone: '446681800'
72 }, 'NATIONAL').should.equal('044 668 18 00'); // France
73
74 formatNumber({
75 country: 'FR',
76 phone: '169454850'
77 }, 'NATIONAL').should.equal('01 69 45 48 50'); // Kazakhstan
78
79 formatNumber('+7 702 211 1111', 'NATIONAL').should.deep.equal('8 (702) 211 1111');
80 });
81 it('should format national numbers with national prefix even if it\'s optional', function () {
82 // Russia
83 formatNumber({
84 country: 'RU',
85 phone: '9991234567'
86 }, 'NATIONAL').should.equal('8 (999) 123-45-67');
87 });
88 it('should work in edge cases', function () {
89 var thrower; // No phone number
90
91 formatNumber('', 'RU', 'INTERNATIONAL').should.equal('');
92 formatNumber('', 'RU', 'NATIONAL').should.equal('');
93 formatNumber({
94 country: 'RU',
95 phone: ''
96 }, 'INTERNATIONAL').should.equal('+7');
97 formatNumber({
98 country: 'RU',
99 phone: ''
100 }, 'NATIONAL').should.equal(''); // No suitable format
101
102 formatNumber('+121337342530', 'US', 'NATIONAL').should.equal('21337342530'); // No suitable format (leading digits mismatch)
103
104 formatNumber('28199999', 'AD', 'NATIONAL').should.equal('28199999'); // Numerical `value`
105
106 thrower = function thrower() {
107 return formatNumber(89150000000, 'RU', 'NATIONAL');
108 };
109
110 thrower.should["throw"]('A phone number must either be a string or an object of shape { phone, [country] }.'); // No metadata for country
111
112 expect(function () {
113 return formatNumber('+121337342530', 'USA', 'NATIONAL');
114 }).to["throw"]('Unknown country');
115 expect(function () {
116 return formatNumber('21337342530', 'USA', 'NATIONAL');
117 }).to["throw"]('Unknown country'); // No format type
118
119 thrower = function thrower() {
120 return formatNumber('+123');
121 };
122
123 thrower.should["throw"]('`format` argument not passed'); // Unknown format type
124
125 thrower = function thrower() {
126 return formatNumber('123', 'US', 'Gay');
127 };
128
129 thrower.should["throw"]('Unknown "format" argument'); // No metadata
130
131 thrower = function thrower() {
132 return (0, _format["default"])('123', 'US', 'E.164');
133 };
134
135 thrower.should["throw"]('`metadata`'); // No formats
136
137 formatNumber('012345', 'AC', 'NATIONAL').should.equal('012345'); // No `fromCountry` for `IDD` format.
138
139 expect(formatNumber('+78005553535', 'IDD')).to.be.undefined; // `fromCountry` has no default IDD prefix.
140
141 expect(formatNumber('+78005553535', 'IDD', {
142 fromCountry: 'BO'
143 })).to.be.undefined; // No such country.
144
145 expect(function () {
146 return formatNumber({
147 phone: '123',
148 country: 'USA'
149 }, 'NATIONAL');
150 }).to["throw"]('Unknown country');
151 });
152 it('should format phone number extensions', function () {
153 // National
154 formatNumber({
155 country: 'US',
156 phone: '2133734253',
157 ext: '123'
158 }, 'NATIONAL').should.equal('(213) 373-4253 ext. 123'); // International
159
160 formatNumber({
161 country: 'US',
162 phone: '2133734253',
163 ext: '123'
164 }, 'INTERNATIONAL').should.equal('+1 213 373 4253 ext. 123'); // International
165
166 formatNumber({
167 country: 'US',
168 phone: '2133734253',
169 ext: '123'
170 }, 'INTERNATIONAL').should.equal('+1 213 373 4253 ext. 123'); // E.164
171
172 formatNumber({
173 country: 'US',
174 phone: '2133734253',
175 ext: '123'
176 }, 'E.164').should.equal('+12133734253'); // RFC3966
177
178 formatNumber({
179 country: 'US',
180 phone: '2133734253',
181 ext: '123'
182 }, 'RFC3966').should.equal('tel:+12133734253;ext=123'); // Custom ext prefix.
183
184 formatNumber({
185 country: 'GB',
186 phone: '7912345678',
187 ext: '123'
188 }, 'INTERNATIONAL').should.equal('+44 7912 345678 x123');
189 });
190 it('should work with Argentina numbers', function () {
191 // The same mobile number is written differently
192 // in different formats in Argentina:
193 // `9` gets prepended in international format.
194 formatNumber({
195 country: 'AR',
196 phone: '3435551212'
197 }, 'INTERNATIONAL').should.equal('+54 3435 55 1212');
198 formatNumber({
199 country: 'AR',
200 phone: '3435551212'
201 }, 'NATIONAL').should.equal('03435 55-1212');
202 });
203 it('should work with Mexico numbers', function () {
204 // Fixed line.
205 formatNumber({
206 country: 'MX',
207 phone: '4499780001'
208 }, 'INTERNATIONAL').should.equal('+52 449 978 0001');
209 formatNumber({
210 country: 'MX',
211 phone: '4499780001'
212 }, 'NATIONAL').should.equal('449 978 0001'); // or '(449)978-0001'.
213 // Mobile.
214 // `1` is prepended before area code to mobile numbers in international format.
215
216 formatNumber({
217 country: 'MX',
218 phone: '3312345678'
219 }, 'INTERNATIONAL').should.equal('+52 33 1234 5678');
220 formatNumber({
221 country: 'MX',
222 phone: '3312345678'
223 }, 'NATIONAL').should.equal('33 1234 5678'); // or '045 33 1234-5678'.
224 });
225 it('should format possible numbers', function () {
226 formatNumber({
227 countryCallingCode: '7',
228 phone: '1111111111'
229 }, 'E.164').should.equal('+71111111111');
230 formatNumber({
231 countryCallingCode: '7',
232 phone: '1111111111'
233 }, 'NATIONAL').should.equal('1111111111');
234 formatNumber({
235 countryCallingCode: '7',
236 phone: '1111111111'
237 }, 'INTERNATIONAL').should.equal('+7 1111111111');
238 });
239 it('should format IDD-prefixed number', function () {
240 // No `fromCountry`.
241 expect(formatNumber('+78005553535', 'IDD')).to.be.undefined; // No default IDD prefix.
242
243 expect(formatNumber('+78005553535', 'IDD', {
244 fromCountry: 'BO'
245 })).to.be.undefined; // Same country calling code.
246
247 formatNumber('+12133734253', 'IDD', {
248 fromCountry: 'CA',
249 humanReadable: true
250 }).should.equal('1 (213) 373-4253');
251 formatNumber('+78005553535', 'IDD', {
252 fromCountry: 'KZ',
253 humanReadable: true
254 }).should.equal('8 (800) 555-35-35'); // formatNumber('+78005553535', 'IDD', { fromCountry: 'US' }).should.equal('01178005553535')
255
256 formatNumber('+78005553535', 'IDD', {
257 fromCountry: 'US',
258 humanReadable: true
259 }).should.equal('011 7 800 555 35 35');
260 });
261 it('should format non-geographic numbering plan phone numbers', function () {
262 // https://github.com/catamphetamine/libphonenumber-js/issues/323
263 formatNumber('+870773111632', 'INTERNATIONAL').should.equal('+870 773 111 632');
264 formatNumber('+870773111632', 'NATIONAL').should.equal('773 111 632');
265 });
266});
267//# sourceMappingURL=format.test.js.map
\No newline at end of file