UNPKG

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