format.test.js 12 KB

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