format.test.js 8.8 KB

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