format.test.js 12 KB

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