createExtensionPattern.js 5.3 KB

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