parse.test.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. import metadata from '../../metadata.min.json' assert { type: 'json' };
  2. import _parseNumber from './parse.js';
  3. import Metadata from '../metadata.js';
  4. function parseNumber() {
  5. for (var _len = arguments.length, parameters = new Array(_len), _key = 0; _key < _len; _key++) {
  6. parameters[_key] = arguments[_key];
  7. }
  8. parameters.push(metadata);
  9. return _parseNumber.apply(this, parameters);
  10. }
  11. var USE_NON_GEOGRAPHIC_COUNTRY_CODE = false;
  12. describe('parse', function () {
  13. it('should not parse invalid phone numbers', function () {
  14. // Too short.
  15. parseNumber('+7 (800) 55-35-35').should.deep.equal({}); // Too long.
  16. parseNumber('+7 (800) 55-35-35-55').should.deep.equal({});
  17. parseNumber('+7 (800) 55-35-35', 'US').should.deep.equal({});
  18. parseNumber('(800) 55 35 35', {
  19. defaultCountry: 'RU'
  20. }).should.deep.equal({});
  21. parseNumber('+1 187 215 5230', 'US').should.deep.equal({});
  22. parseNumber('911231231', 'BE').should.deep.equal({});
  23. });
  24. it('should parse valid phone numbers', function () {
  25. // Instant loans
  26. // https://www.youtube.com/watch?v=6e1pMrYH5jI
  27. //
  28. // Restrict to RU
  29. parseNumber('Phone: 8 (800) 555 35 35.', 'RU').should.deep.equal({
  30. country: 'RU',
  31. phone: '8005553535'
  32. }); // International format
  33. parseNumber('Phone: +7 (800) 555-35-35.').should.deep.equal({
  34. country: 'RU',
  35. phone: '8005553535'
  36. }); // // Restrict to US, but not a US country phone code supplied
  37. // parseNumber('+7 (800) 555-35-35', 'US').should.deep.equal({})
  38. // Restrict to RU
  39. parseNumber('(800) 555 35 35', 'RU').should.deep.equal({
  40. country: 'RU',
  41. phone: '8005553535'
  42. }); // Default to RU
  43. parseNumber('8 (800) 555 35 35', {
  44. defaultCountry: 'RU'
  45. }).should.deep.equal({
  46. country: 'RU',
  47. phone: '8005553535'
  48. }); // Gangster partyline
  49. parseNumber('+1-213-373-4253').should.deep.equal({
  50. country: 'US',
  51. phone: '2133734253'
  52. }); // Switzerland (just in case)
  53. parseNumber('044 668 18 00', 'CH').should.deep.equal({
  54. country: 'CH',
  55. phone: '446681800'
  56. }); // China, Beijing
  57. parseNumber('010-852644821', 'CN').should.deep.equal({
  58. country: 'CN',
  59. phone: '10852644821'
  60. }); // France
  61. parseNumber('+33169454850').should.deep.equal({
  62. country: 'FR',
  63. phone: '169454850'
  64. }); // UK (Jersey)
  65. parseNumber('+44 7700 300000').should.deep.equal({
  66. country: 'JE',
  67. phone: '7700300000'
  68. }); // KZ
  69. parseNumber('+7 702 211 1111').should.deep.equal({
  70. country: 'KZ',
  71. phone: '7022111111'
  72. }); // Brazil
  73. parseNumber('11987654321', 'BR').should.deep.equal({
  74. country: 'BR',
  75. phone: '11987654321'
  76. }); // Long country phone code.
  77. parseNumber('+212659777777').should.deep.equal({
  78. country: 'MA',
  79. phone: '659777777'
  80. }); // No country could be derived.
  81. // parseNumber('+212569887076').should.deep.equal({ countryPhoneCode: '212', phone: '569887076' })
  82. // GB. Moible numbers starting 07624* are Isle of Man.
  83. parseNumber('07624369230', 'GB').should.deep.equal({
  84. country: 'IM',
  85. phone: '7624369230'
  86. });
  87. });
  88. it('should parse possible numbers', function () {
  89. // Invalid phone number for a given country.
  90. parseNumber('1112223344', 'RU', {
  91. extended: true
  92. }).should.deep.equal({
  93. country: 'RU',
  94. countryCallingCode: '7',
  95. phone: '1112223344',
  96. carrierCode: undefined,
  97. ext: undefined,
  98. valid: false,
  99. possible: true
  100. }); // International phone number.
  101. // Several countries with the same country phone code.
  102. parseNumber('+71112223344').should.deep.equal({});
  103. parseNumber('+71112223344', {
  104. extended: true
  105. }).should.deep.equal({
  106. country: undefined,
  107. countryCallingCode: '7',
  108. phone: '1112223344',
  109. carrierCode: undefined,
  110. ext: undefined,
  111. valid: false,
  112. possible: true
  113. }); // International phone number.
  114. // Single country with the given country phone code.
  115. parseNumber('+33011222333', {
  116. extended: true
  117. }).should.deep.equal({
  118. country: 'FR',
  119. countryCallingCode: '33',
  120. phone: '011222333',
  121. carrierCode: undefined,
  122. ext: undefined,
  123. valid: false,
  124. possible: true
  125. }); // Too short.
  126. // Won't strip national prefix `8` because otherwise the number would be too short.
  127. parseNumber('+7 (800) 55-35-35', {
  128. extended: true
  129. }).should.deep.equal({
  130. country: 'RU',
  131. countryCallingCode: '7',
  132. phone: '800553535',
  133. carrierCode: undefined,
  134. ext: undefined,
  135. valid: false,
  136. possible: false
  137. }); // Too long.
  138. parseNumber('+1 213 37342530', {
  139. extended: true
  140. }).should.deep.equal({
  141. country: undefined,
  142. countryCallingCode: '1',
  143. phone: '21337342530',
  144. carrierCode: undefined,
  145. ext: undefined,
  146. valid: false,
  147. possible: false
  148. }); // No national number to be parsed.
  149. parseNumber('+996', {
  150. extended: true
  151. }).should.deep.equal({// countryCallingCode : '996'
  152. }); // Valid number.
  153. parseNumber('+78005553535', {
  154. extended: true
  155. }).should.deep.equal({
  156. country: 'RU',
  157. countryCallingCode: '7',
  158. phone: '8005553535',
  159. carrierCode: undefined,
  160. ext: undefined,
  161. valid: true,
  162. possible: true
  163. }); // https://github.com/catamphetamine/libphonenumber-js/issues/211
  164. parseNumber('+966', {
  165. extended: true
  166. }).should.deep.equal({});
  167. parseNumber('+9664', {
  168. extended: true
  169. }).should.deep.equal({});
  170. parseNumber('+96645', {
  171. extended: true
  172. }).should.deep.equal({
  173. carrierCode: undefined,
  174. phone: '45',
  175. ext: undefined,
  176. country: 'SA',
  177. countryCallingCode: '966',
  178. possible: false,
  179. valid: false
  180. });
  181. });
  182. it('should parse non-European digits', function () {
  183. parseNumber('+١٢١٢٢٣٢٣٢٣٢').should.deep.equal({
  184. country: 'US',
  185. phone: '2122323232'
  186. });
  187. });
  188. it('should work in edge cases', function () {
  189. var thrower; // No input
  190. parseNumber('').should.deep.equal({}); // No country phone code
  191. parseNumber('+').should.deep.equal({}); // No country at all (non international number and no explicit country code)
  192. parseNumber('123').should.deep.equal({}); // No country metadata for this `require` country code
  193. thrower = function thrower() {
  194. return parseNumber('123', 'ZZ');
  195. };
  196. thrower.should["throw"]('Unknown country'); // No country metadata for this `default` country code
  197. thrower = function thrower() {
  198. return parseNumber('123', {
  199. defaultCountry: 'ZZ'
  200. });
  201. };
  202. thrower.should["throw"]('Unknown country'); // Invalid country phone code
  203. parseNumber('+210').should.deep.equal({}); // Invalid country phone code (extended parsing mode)
  204. parseNumber('+210', {
  205. extended: true
  206. }).should.deep.equal({}); // Too short of a number.
  207. parseNumber('1', 'US', {
  208. extended: true
  209. }).should.deep.equal({}); // Too long of a number.
  210. parseNumber('1111111111111111111', 'RU', {
  211. extended: true
  212. }).should.deep.equal({}); // Not a number.
  213. parseNumber('abcdefg', 'US', {
  214. extended: true
  215. }).should.deep.equal({}); // Country phone code beginning with a '0'
  216. parseNumber('+0123').should.deep.equal({}); // Barbados NANPA phone number
  217. parseNumber('+12460000000').should.deep.equal({
  218. country: 'BB',
  219. phone: '2460000000'
  220. }); // // A case when country (restricted to) is not equal
  221. // // to the one parsed out of an international number.
  222. // parseNumber('+1-213-373-4253', 'RU').should.deep.equal({})
  223. // National (significant) number too short
  224. parseNumber('2', 'US').should.deep.equal({}); // National (significant) number too long
  225. parseNumber('222222222222222222', 'US').should.deep.equal({}); // No `national_prefix_for_parsing`
  226. parseNumber('41111', 'AC').should.deep.equal({
  227. country: 'AC',
  228. phone: '41111'
  229. }); // https://github.com/catamphetamine/libphonenumber-js/issues/235
  230. // `matchesEntirely()` bug fix.
  231. parseNumber('+4915784846111‬').should.deep.equal({
  232. country: 'DE',
  233. phone: '15784846111'
  234. }); // No metadata
  235. thrower = function thrower() {
  236. return _parseNumber('');
  237. };
  238. thrower.should["throw"]('`metadata` argument not passed'); // Numerical `value`
  239. thrower = function thrower() {
  240. return parseNumber(2141111111, 'US');
  241. };
  242. thrower.should["throw"]('A text for parsing must be a string.'); // Input string too long.
  243. parseNumber('8005553535 ', 'RU').should.deep.equal({});
  244. });
  245. it('should parse phone number extensions', function () {
  246. // "ext"
  247. parseNumber('2134567890 ext 123', 'US').should.deep.equal({
  248. country: 'US',
  249. phone: '2134567890',
  250. ext: '123'
  251. }); // "ext."
  252. parseNumber('+12134567890 ext. 12345', 'US').should.deep.equal({
  253. country: 'US',
  254. phone: '2134567890',
  255. ext: '12345'
  256. }); // "доб."
  257. parseNumber('+78005553535 доб. 1234', 'RU').should.deep.equal({
  258. country: 'RU',
  259. phone: '8005553535',
  260. ext: '1234'
  261. }); // "#"
  262. parseNumber('+12134567890#1234').should.deep.equal({
  263. country: 'US',
  264. phone: '2134567890',
  265. ext: '1234'
  266. }); // "x"
  267. parseNumber('+78005553535 x1234').should.deep.equal({
  268. country: 'RU',
  269. phone: '8005553535',
  270. ext: '1234'
  271. }); // Not a valid extension
  272. parseNumber('2134567890 ext. abc', 'US').should.deep.equal({
  273. country: 'US',
  274. phone: '2134567890'
  275. });
  276. });
  277. it('should parse RFC 3966 phone numbers', function () {
  278. parseNumber('tel:+78005553535;ext=123').should.deep.equal({
  279. country: 'RU',
  280. phone: '8005553535',
  281. ext: '123'
  282. }); // Should parse "visual separators".
  283. parseNumber('tel:+7(800)555-35.35;ext=123').should.deep.equal({
  284. country: 'RU',
  285. phone: '8005553535',
  286. ext: '123'
  287. }); // Invalid number.
  288. parseNumber('tel:+7x8005553535;ext=123').should.deep.equal({});
  289. });
  290. it('should parse invalid international numbers even if they are invalid', function () {
  291. parseNumber('+7(8)8005553535', 'RU').should.deep.equal({
  292. country: 'RU',
  293. phone: '8005553535'
  294. });
  295. });
  296. it('should parse carrier codes', function () {
  297. parseNumber('0 15 21 5555-5555', 'BR', {
  298. extended: true
  299. }).should.deep.equal({
  300. country: 'BR',
  301. countryCallingCode: '55',
  302. phone: '2155555555',
  303. carrierCode: '15',
  304. ext: undefined,
  305. valid: true,
  306. possible: true
  307. });
  308. });
  309. it('should parse IDD prefixes', function () {
  310. parseNumber('011 61 2 3456 7890', 'US').should.deep.equal({
  311. phone: '234567890',
  312. country: 'AU'
  313. });
  314. parseNumber('011 61 2 3456 7890', 'FR').should.deep.equal({});
  315. parseNumber('00 61 2 3456 7890', 'US').should.deep.equal({});
  316. parseNumber('810 61 2 3456 7890', 'RU').should.deep.equal({
  317. phone: '234567890',
  318. country: 'AU'
  319. });
  320. });
  321. it('should work with v2 API', function () {
  322. parseNumber('+99989160151539');
  323. });
  324. it('should work with Argentina numbers', function () {
  325. // The same mobile number is written differently
  326. // in different formats in Argentina:
  327. // `9` gets prepended in international format.
  328. parseNumber('+54 9 3435 55 1212').should.deep.equal({
  329. country: 'AR',
  330. phone: '93435551212'
  331. });
  332. parseNumber('0343 15-555-1212', 'AR').should.deep.equal({
  333. country: 'AR',
  334. phone: '93435551212'
  335. });
  336. });
  337. it('should work with Mexico numbers', function () {
  338. // Fixed line.
  339. parseNumber('+52 449 978 0001').should.deep.equal({
  340. country: 'MX',
  341. phone: '4499780001'
  342. });
  343. parseNumber('01 (449)978-0001', 'MX').should.deep.equal({
  344. country: 'MX',
  345. phone: '4499780001'
  346. });
  347. parseNumber('(449)978-0001', 'MX').should.deep.equal({
  348. country: 'MX',
  349. phone: '4499780001'
  350. }); // Mobile.
  351. // `1` is prepended before area code to mobile numbers in international format.
  352. parseNumber('+52 1 33 1234-5678', 'MX').should.deep.equal({
  353. country: 'MX',
  354. phone: '3312345678'
  355. });
  356. parseNumber('+52 33 1234-5678', 'MX').should.deep.equal({
  357. country: 'MX',
  358. phone: '3312345678'
  359. });
  360. parseNumber('044 (33) 1234-5678', 'MX').should.deep.equal({
  361. country: 'MX',
  362. phone: '3312345678'
  363. });
  364. parseNumber('045 33 1234-5678', 'MX').should.deep.equal({
  365. country: 'MX',
  366. phone: '3312345678'
  367. });
  368. });
  369. it('should parse non-geographic numbering plan phone numbers', function () {
  370. parseNumber('+870773111632').should.deep.equal(USE_NON_GEOGRAPHIC_COUNTRY_CODE ? {
  371. country: '001',
  372. phone: '773111632'
  373. } : {});
  374. });
  375. it('should parse non-geographic numbering plan phone numbers (default country code)', function () {
  376. parseNumber('773111632', {
  377. defaultCallingCode: '870'
  378. }).should.deep.equal(USE_NON_GEOGRAPHIC_COUNTRY_CODE ? {
  379. country: '001',
  380. phone: '773111632'
  381. } : {});
  382. });
  383. it('should parse non-geographic numbering plan phone numbers (extended)', function () {
  384. parseNumber('+870773111632', {
  385. extended: true
  386. }).should.deep.equal({
  387. country: USE_NON_GEOGRAPHIC_COUNTRY_CODE ? '001' : undefined,
  388. countryCallingCode: '870',
  389. phone: '773111632',
  390. carrierCode: undefined,
  391. ext: undefined,
  392. possible: true,
  393. valid: true
  394. });
  395. });
  396. it('should parse non-geographic numbering plan phone numbers (default country code) (extended)', function () {
  397. parseNumber('773111632', {
  398. defaultCallingCode: '870',
  399. extended: true
  400. }).should.deep.equal({
  401. country: USE_NON_GEOGRAPHIC_COUNTRY_CODE ? '001' : undefined,
  402. countryCallingCode: '870',
  403. phone: '773111632',
  404. carrierCode: undefined,
  405. ext: undefined,
  406. possible: true,
  407. valid: true
  408. });
  409. });
  410. it('shouldn\'t crash when invalid `defaultCallingCode` is passed', function () {
  411. expect(function () {
  412. return parseNumber('773111632', {
  413. defaultCallingCode: '999'
  414. });
  415. }).to["throw"]('Unknown calling code');
  416. });
  417. it('shouldn\'t set `country` when there\'s no `defaultCountry` and `defaultCallingCode` is not of a "non-geographic entity"', function () {
  418. parseNumber('88005553535', {
  419. defaultCallingCode: '7'
  420. }).should.deep.equal({
  421. country: 'RU',
  422. phone: '8005553535'
  423. });
  424. });
  425. it('should correctly parse numbers starting with the same digit as the national prefix', function () {
  426. // https://github.com/catamphetamine/libphonenumber-js/issues/373
  427. // `BY`'s `national_prefix` is `8`.
  428. parseNumber('+37582004910060').should.deep.equal({
  429. country: 'BY',
  430. phone: '82004910060'
  431. });
  432. });
  433. it('should autocorrect numbers without a leading +', function () {
  434. // https://github.com/catamphetamine/libphonenumber-js/issues/376
  435. parseNumber('375447521111', 'BY').should.deep.equal({
  436. country: 'BY',
  437. phone: '447521111'
  438. }); // https://github.com/catamphetamine/libphonenumber-js/issues/316
  439. parseNumber('33612902554', 'FR').should.deep.equal({
  440. country: 'FR',
  441. phone: '612902554'
  442. }); // https://github.com/catamphetamine/libphonenumber-js/issues/375
  443. parseNumber('61438331999', 'AU').should.deep.equal({
  444. country: 'AU',
  445. phone: '438331999'
  446. }); // A case when `49` is a country calling code of a number without a leading `+`.
  447. parseNumber('4930123456', 'DE').should.deep.equal({
  448. country: 'DE',
  449. phone: '30123456'
  450. }); // A case when `49` is a valid area code.
  451. parseNumber('4951234567890', 'DE').should.deep.equal({
  452. country: 'DE',
  453. phone: '4951234567890'
  454. });
  455. });
  456. it('should parse extensions (long extensions with explicitl abels)', function () {
  457. // Test lower and upper limits of extension lengths for each type of label.
  458. // Firstly, when in RFC format: PhoneNumberUtil.extLimitAfterExplicitLabel
  459. parseNumber('33316005 ext 0', 'NZ').ext.should.equal('0');
  460. parseNumber('33316005 ext 01234567890123456789', 'NZ').ext.should.equal('01234567890123456789'); // Extension too long.
  461. expect(parseNumber('33316005 ext 012345678901234567890', 'NZ').ext).to.be.undefined; // Explicit extension label.
  462. parseNumber('03 3316005ext:1', 'NZ').ext.should.equal('1');
  463. parseNumber('03 3316005 xtn:12345678901234567890', 'NZ').ext.should.equal('12345678901234567890');
  464. parseNumber('03 3316005 extension\t12345678901234567890', 'NZ').ext.should.equal('12345678901234567890');
  465. parseNumber('03 3316005 xtensio:12345678901234567890', 'NZ').ext.should.equal('12345678901234567890');
  466. parseNumber('03 3316005 xtensión, 12345678901234567890#', 'NZ').ext.should.equal('12345678901234567890');
  467. parseNumber('03 3316005extension.12345678901234567890', 'NZ').ext.should.equal('12345678901234567890');
  468. parseNumber('03 3316005 доб:12345678901234567890', 'NZ').ext.should.equal('12345678901234567890'); // Extension too long.
  469. expect(parseNumber('03 3316005 extension 123456789012345678901', 'NZ').ext).to.be.undefined;
  470. });
  471. it('should parse extensions (long extensions with auto dialling labels)', function () {
  472. parseNumber('+12679000000,,123456789012345#').ext.should.equal('123456789012345');
  473. parseNumber('+12679000000;123456789012345#').ext.should.equal('123456789012345');
  474. parseNumber('+442034000000,,123456789#').ext.should.equal('123456789'); // Extension too long.
  475. expect(parseNumber('+12679000000,,1234567890123456#').ext).to.be.undefined;
  476. });
  477. it('should parse extensions (short extensions with ambiguous characters)', function () {
  478. parseNumber('03 3316005 x 123456789', 'NZ').ext.should.equal('123456789');
  479. parseNumber('03 3316005 x. 123456789', 'NZ').ext.should.equal('123456789');
  480. parseNumber('03 3316005 #123456789#', 'NZ').ext.should.equal('123456789');
  481. parseNumber('03 3316005 ~ 123456789', 'NZ').ext.should.equal('123456789'); // Extension too long.
  482. expect(parseNumber('03 3316005 ~ 1234567890', 'NZ').ext).to.be.undefined;
  483. });
  484. it('should parse extensions (short extensions when not sure of label)', function () {
  485. parseNumber('+1123-456-7890 666666#', {
  486. v2: true
  487. }).ext.should.equal('666666');
  488. parseNumber('+11234567890-6#', {
  489. v2: true
  490. }).ext.should.equal('6'); // Extension too long.
  491. expect(function () {
  492. return parseNumber('+1123-456-7890 7777777#', {
  493. v2: true
  494. });
  495. }).to["throw"]('NOT_A_NUMBER');
  496. });
  497. });
  498. //# sourceMappingURL=parse.test.js.map