extractPhoneContext.test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import parsePhoneNumber_ from '../parsePhoneNumber.js';
  2. import PhoneNumber from '../PhoneNumber.js';
  3. import metadata from '../../metadata.min.json' assert { type: 'json' };
  4. function parsePhoneNumber() {
  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 parsePhoneNumber_.apply(this, parameters);
  10. }
  11. describe('extractPhoneContext', function () {
  12. it('should parse RFC 3966 phone number URIs', function () {
  13. // context = ";phone-context=" descriptor
  14. // descriptor = domainname / global-number-digits
  15. var NZ_NUMBER = new PhoneNumber('64', '33316005', metadata); // Valid global-phone-digits
  16. expectPhoneNumbersToBeEqual(parsePhoneNumber('tel:033316005;phone-context=+64'), NZ_NUMBER);
  17. expectPhoneNumbersToBeEqual(parsePhoneNumber('tel:033316005;phone-context=+64;{this isn\'t part of phone-context anymore!}'), NZ_NUMBER);
  18. var nzFromPhoneContext = new PhoneNumber('64', '3033316005', metadata);
  19. expectPhoneNumbersToBeEqual(parsePhoneNumber('tel:033316005;phone-context=+64-3'), nzFromPhoneContext);
  20. var brFromPhoneContext = new PhoneNumber('55', '5033316005', metadata);
  21. expectPhoneNumbersToBeEqual(parsePhoneNumber('tel:033316005;phone-context=+(555)'), brFromPhoneContext);
  22. var usFromPhoneContext = new PhoneNumber('1', '23033316005', metadata);
  23. expectPhoneNumbersToBeEqual(parsePhoneNumber('tel:033316005;phone-context=+-1-2.3()'), usFromPhoneContext); // Valid domainname.
  24. expectPhoneNumbersToBeEqual(parsePhoneNumber('tel:033316005;phone-context=abc.nz', 'NZ'), NZ_NUMBER);
  25. expectPhoneNumbersToBeEqual(parsePhoneNumber('tel:033316005;phone-context=www.PHONE-numb3r.com', 'NZ'), NZ_NUMBER);
  26. expectPhoneNumbersToBeEqual(parsePhoneNumber('tel:033316005;phone-context=a', 'NZ'), NZ_NUMBER);
  27. expectPhoneNumbersToBeEqual(parsePhoneNumber('tel:033316005;phone-context=3phone.J.', 'NZ'), NZ_NUMBER);
  28. expectPhoneNumbersToBeEqual(parsePhoneNumber('tel:033316005;phone-context=a--z', 'NZ'), NZ_NUMBER); // Should strip ISDN subaddress.
  29. expectPhoneNumbersToBeEqual(parsePhoneNumber('tel:033316005;isub=/@;phone-context=+64', 'NZ'), NZ_NUMBER); // // Should support incorrectly-written RFC 3966 phone numbers:
  30. // // the ones written without a `tel:` prefix.
  31. // expectPhoneNumbersToBeEqual(
  32. // parsePhoneNumber('033316005;phone-context=+64', 'NZ'),
  33. // NZ_NUMBER
  34. // )
  35. // Invalid descriptor.
  36. expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=');
  37. expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=+');
  38. expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=64');
  39. expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=++64');
  40. expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=+abc');
  41. expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=.');
  42. expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=3phone');
  43. expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=a-.nz');
  44. expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=a{b}c');
  45. });
  46. });
  47. function expectToThrowForInvalidPhoneContext(string) {
  48. expect(parsePhoneNumber(string)).to.be.undefined;
  49. }
  50. function expectPhoneNumbersToBeEqual(phoneNumber1, phoneNumber2) {
  51. if (!phoneNumber1 || !phoneNumber2) {
  52. return false;
  53. }
  54. return phoneNumber1.number === phoneNumber2.number && phoneNumber1.ext === phoneNumber2.ext;
  55. }
  56. //# sourceMappingURL=extractPhoneContext.test.js.map