createExtensionPattern.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { VALID_DIGITS } from '../../constants.js'; // The RFC 3966 format for extensions.
  2. var RFC3966_EXTN_PREFIX = ';ext=';
  3. /**
  4. * Helper method for constructing regular expressions for parsing. Creates
  5. * an expression that captures up to max_length digits.
  6. * @return {string} RegEx pattern to capture extension digits.
  7. */
  8. var getExtensionDigitsPattern = function getExtensionDigitsPattern(maxLength) {
  9. return "([".concat(VALID_DIGITS, "]{1,").concat(maxLength, "})");
  10. };
  11. /**
  12. * Helper initialiser method to create the regular-expression pattern to match
  13. * extensions.
  14. * Copy-pasted from Google's `libphonenumber`:
  15. * https://github.com/google/libphonenumber/blob/55b2646ec9393f4d3d6661b9c82ef9e258e8b829/javascript/i18n/phonenumbers/phonenumberutil.js#L759-L766
  16. * @return {string} RegEx pattern to capture extensions.
  17. */
  18. export default function createExtensionPattern(purpose) {
  19. // We cap the maximum length of an extension based on the ambiguity of the way
  20. // the extension is prefixed. As per ITU, the officially allowed length for
  21. // extensions is actually 40, but we don't support this since we haven't seen real
  22. // examples and this introduces many false interpretations as the extension labels
  23. // are not standardized.
  24. /** @type {string} */
  25. var extLimitAfterExplicitLabel = '20';
  26. /** @type {string} */
  27. var extLimitAfterLikelyLabel = '15';
  28. /** @type {string} */
  29. var extLimitAfterAmbiguousChar = '9';
  30. /** @type {string} */
  31. var extLimitWhenNotSure = '6';
  32. /** @type {string} */
  33. var possibleSeparatorsBetweenNumberAndExtLabel = "[ \xA0\\t,]*"; // Optional full stop (.) or colon, followed by zero or more spaces/tabs/commas.
  34. /** @type {string} */
  35. var possibleCharsAfterExtLabel = "[:\\.\uFF0E]?[ \xA0\\t,-]*";
  36. /** @type {string} */
  37. var optionalExtnSuffix = "#?"; // Here the extension is called out in more explicit way, i.e mentioning it obvious
  38. // patterns like "ext.".
  39. /** @type {string} */
  40. var explicitExtLabels = "(?:e?xt(?:ensi(?:o\u0301?|\xF3))?n?|\uFF45?\uFF58\uFF54\uFF4E?|\u0434\u043E\u0431|anexo)"; // One-character symbols that can be used to indicate an extension, and less
  41. // commonly used or more ambiguous extension labels.
  42. /** @type {string} */
  43. var ambiguousExtLabels = "(?:[x\uFF58#\uFF03~\uFF5E]|int|\uFF49\uFF4E\uFF54)"; // When extension is not separated clearly.
  44. /** @type {string} */
  45. var ambiguousSeparator = "[- ]+"; // This is the same as possibleSeparatorsBetweenNumberAndExtLabel, but not matching
  46. // comma as extension label may have it.
  47. /** @type {string} */
  48. var possibleSeparatorsNumberExtLabelNoComma = "[ \xA0\\t]*"; // ",," is commonly used for auto dialling the extension when connected. First
  49. // comma is matched through possibleSeparatorsBetweenNumberAndExtLabel, so we do
  50. // not repeat it here. Semi-colon works in Iphone and Android also to pop up a
  51. // button with the extension number following.
  52. /** @type {string} */
  53. var autoDiallingAndExtLabelsFound = "(?:,{2}|;)";
  54. /** @type {string} */
  55. var rfcExtn = RFC3966_EXTN_PREFIX + getExtensionDigitsPattern(extLimitAfterExplicitLabel);
  56. /** @type {string} */
  57. var explicitExtn = possibleSeparatorsBetweenNumberAndExtLabel + explicitExtLabels + possibleCharsAfterExtLabel + getExtensionDigitsPattern(extLimitAfterExplicitLabel) + optionalExtnSuffix;
  58. /** @type {string} */
  59. var ambiguousExtn = possibleSeparatorsBetweenNumberAndExtLabel + ambiguousExtLabels + possibleCharsAfterExtLabel + getExtensionDigitsPattern(extLimitAfterAmbiguousChar) + optionalExtnSuffix;
  60. /** @type {string} */
  61. var americanStyleExtnWithSuffix = ambiguousSeparator + getExtensionDigitsPattern(extLimitWhenNotSure) + "#";
  62. /** @type {string} */
  63. var autoDiallingExtn = possibleSeparatorsNumberExtLabelNoComma + autoDiallingAndExtLabelsFound + possibleCharsAfterExtLabel + getExtensionDigitsPattern(extLimitAfterLikelyLabel) + optionalExtnSuffix;
  64. /** @type {string} */
  65. var onlyCommasExtn = possibleSeparatorsNumberExtLabelNoComma + "(?:,)+" + possibleCharsAfterExtLabel + getExtensionDigitsPattern(extLimitAfterAmbiguousChar) + optionalExtnSuffix; // The first regular expression covers RFC 3966 format, where the extension is added
  66. // using ";ext=". The second more generic where extension is mentioned with explicit
  67. // labels like "ext:". In both the above cases we allow more numbers in extension than
  68. // any other extension labels. The third one captures when single character extension
  69. // labels or less commonly used labels are used. In such cases we capture fewer
  70. // extension digits in order to reduce the chance of falsely interpreting two
  71. // numbers beside each other as a number + extension. The fourth one covers the
  72. // special case of American numbers where the extension is written with a hash
  73. // at the end, such as "- 503#". The fifth one is exclusively for extension
  74. // autodialling formats which are used when dialling and in this case we accept longer
  75. // extensions. The last one is more liberal on the number of commas that acts as
  76. // extension labels, so we have a strict cap on the number of digits in such extensions.
  77. return rfcExtn + "|" + explicitExtn + "|" + ambiguousExtn + "|" + americanStyleExtnWithSuffix + "|" + autoDiallingExtn + "|" + onlyCommasExtn;
  78. }
  79. //# sourceMappingURL=createExtensionPattern.js.map