format.test.js 9.9 KB

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