extractPhoneContext.js 3.3 KB

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