extractPhoneContext.js 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.RFC3966_PREFIX_ = exports.RFC3966_PHONE_CONTEXT_ = exports.RFC3966_ISDN_SUBADDRESS_ = exports.PLUS_SIGN = void 0;
  6. exports["default"] = extractPhoneContext;
  7. exports.isPhoneContextValid = isPhoneContextValid;
  8. var _constants = require("../constants.js");
  9. // When phone numbers are written in `RFC3966` format — `"tel:+12133734253"` —
  10. // they can have their "calling code" part written separately in a `phone-context` parameter.
  11. // Example: `"tel:12133734253;phone-context=+1"`.
  12. // This function parses the full phone number from the local number and the `phone-context`
  13. // when the `phone-context` contains a `+` sign.
  14. var PLUS_SIGN = '+';
  15. exports.PLUS_SIGN = PLUS_SIGN;
  16. var RFC3966_VISUAL_SEPARATOR_ = '[\\-\\.\\(\\)]?';
  17. var RFC3966_PHONE_DIGIT_ = '(' + '[' + _constants.VALID_DIGITS + ']' + '|' + RFC3966_VISUAL_SEPARATOR_ + ')';
  18. var RFC3966_GLOBAL_NUMBER_DIGITS_ = '^' + '\\' + PLUS_SIGN + RFC3966_PHONE_DIGIT_ + '*' + '[' + _constants.VALID_DIGITS + ']' + RFC3966_PHONE_DIGIT_ + '*' + '$';
  19. /**
  20. * Regular expression of valid global-number-digits for the phone-context
  21. * parameter, following the syntax defined in RFC3966.
  22. */
  23. var RFC3966_GLOBAL_NUMBER_DIGITS_PATTERN_ = new RegExp(RFC3966_GLOBAL_NUMBER_DIGITS_, 'g'); // In this port of Google's library, we don't accept alpha characters in phone numbers.
  24. // const ALPHANUM_ = VALID_ALPHA_ + VALID_DIGITS
  25. var ALPHANUM_ = _constants.VALID_DIGITS;
  26. var RFC3966_DOMAINLABEL_ = '[' + ALPHANUM_ + ']+((\\-)*[' + ALPHANUM_ + '])*';
  27. var VALID_ALPHA_ = 'a-zA-Z';
  28. var RFC3966_TOPLABEL_ = '[' + VALID_ALPHA_ + ']+((\\-)*[' + ALPHANUM_ + '])*';
  29. var RFC3966_DOMAINNAME_ = '^(' + RFC3966_DOMAINLABEL_ + '\\.)*' + RFC3966_TOPLABEL_ + '\\.?$';
  30. /**
  31. * Regular expression of valid domainname for the phone-context parameter,
  32. * following the syntax defined in RFC3966.
  33. */
  34. var RFC3966_DOMAINNAME_PATTERN_ = new RegExp(RFC3966_DOMAINNAME_, 'g');
  35. var RFC3966_PREFIX_ = 'tel:';
  36. exports.RFC3966_PREFIX_ = RFC3966_PREFIX_;
  37. var RFC3966_PHONE_CONTEXT_ = ';phone-context=';
  38. exports.RFC3966_PHONE_CONTEXT_ = RFC3966_PHONE_CONTEXT_;
  39. var RFC3966_ISDN_SUBADDRESS_ = ';isub=';
  40. /**
  41. * Extracts the value of the phone-context parameter of `numberToExtractFrom`,
  42. * following the syntax defined in RFC3966.
  43. *
  44. * @param {string} numberToExtractFrom
  45. * @return {string|null} the extracted string (possibly empty), or `null` if no phone-context parameter is found.
  46. */
  47. exports.RFC3966_ISDN_SUBADDRESS_ = RFC3966_ISDN_SUBADDRESS_;
  48. function extractPhoneContext(numberToExtractFrom) {
  49. var indexOfPhoneContext = numberToExtractFrom.indexOf(RFC3966_PHONE_CONTEXT_); // If no phone-context parameter is present
  50. if (indexOfPhoneContext < 0) {
  51. return null;
  52. }
  53. var phoneContextStart = indexOfPhoneContext + RFC3966_PHONE_CONTEXT_.length; // If phone-context parameter is empty
  54. if (phoneContextStart >= numberToExtractFrom.length) {
  55. return '';
  56. }
  57. var phoneContextEnd = numberToExtractFrom.indexOf(';', phoneContextStart); // If phone-context is not the last parameter
  58. if (phoneContextEnd >= 0) {
  59. return numberToExtractFrom.substring(phoneContextStart, phoneContextEnd);
  60. } else {
  61. return numberToExtractFrom.substring(phoneContextStart);
  62. }
  63. }
  64. /**
  65. * Returns whether the value of phoneContext follows the syntax defined in RFC3966.
  66. *
  67. * @param {string|null} phoneContext
  68. * @return {boolean}
  69. */
  70. function isPhoneContextValid(phoneContext) {
  71. if (phoneContext === null) {
  72. return true;
  73. }
  74. if (phoneContext.length === 0) {
  75. return false;
  76. } // Does phone-context value match pattern of global-number-digits or domainname.
  77. return RFC3966_GLOBAL_NUMBER_DIGITS_PATTERN_.test(phoneContext) || RFC3966_DOMAINNAME_PATTERN_.test(phoneContext);
  78. }
  79. //# sourceMappingURL=extractPhoneContext.js.map