parse.test.js 19 KB

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