format.test.js 9.7 KB

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