isValidCandidate.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.LEAD_CLASS = void 0;
  6. exports["default"] = isValidCandidate;
  7. var _constants = require("../constants.js");
  8. var _util = require("./util.js");
  9. var _utf = require("./utf-8.js");
  10. // Copy-pasted from `PhoneNumberMatcher.js`.
  11. var OPENING_PARENS = "(\\[\uFF08\uFF3B";
  12. var CLOSING_PARENS = ")\\]\uFF09\uFF3D";
  13. var NON_PARENS = "[^".concat(OPENING_PARENS).concat(CLOSING_PARENS, "]");
  14. var LEAD_CLASS = "[".concat(OPENING_PARENS).concat(_constants.PLUS_CHARS, "]"); // Punctuation that may be at the start of a phone number - brackets and plus signs.
  15. exports.LEAD_CLASS = LEAD_CLASS;
  16. var LEAD_CLASS_LEADING = new RegExp('^' + LEAD_CLASS); // Limit on the number of pairs of brackets in a phone number.
  17. var BRACKET_PAIR_LIMIT = (0, _util.limit)(0, 3);
  18. /**
  19. * Pattern to check that brackets match. Opening brackets should be closed within a phone number.
  20. * This also checks that there is something inside the brackets. Having no brackets at all is also
  21. * fine.
  22. *
  23. * An opening bracket at the beginning may not be closed, but subsequent ones should be. It's
  24. * also possible that the leading bracket was dropped, so we shouldn't be surprised if we see a
  25. * closing bracket first. We limit the sets of brackets in a phone number to four.
  26. */
  27. var MATCHING_BRACKETS_ENTIRE = new RegExp('^' + "(?:[" + OPENING_PARENS + "])?" + "(?:" + NON_PARENS + "+" + "[" + CLOSING_PARENS + "])?" + NON_PARENS + "+" + "(?:[" + OPENING_PARENS + "]" + NON_PARENS + "+[" + CLOSING_PARENS + "])" + BRACKET_PAIR_LIMIT + NON_PARENS + "*" + '$');
  28. /**
  29. * Matches strings that look like publication pages. Example:
  30. * <pre>Computing Complete Answers to Queries in the Presence of Limited Access Patterns.
  31. * Chen Li. VLDB J. 12(3): 211-227 (2003).</pre>
  32. *
  33. * The string "211-227 (2003)" is not a telephone number.
  34. */
  35. var PUB_PAGES = /\d{1,5}-+\d{1,5}\s{0,4}\(\d{1,4}/;
  36. function isValidCandidate(candidate, offset, text, leniency) {
  37. // Check the candidate doesn't contain any formatting
  38. // which would indicate that it really isn't a phone number.
  39. if (!MATCHING_BRACKETS_ENTIRE.test(candidate) || PUB_PAGES.test(candidate)) {
  40. return;
  41. } // If leniency is set to VALID or stricter, we also want to skip numbers that are surrounded
  42. // by Latin alphabetic characters, to skip cases like abc8005001234 or 8005001234def.
  43. if (leniency !== 'POSSIBLE') {
  44. // If the candidate is not at the start of the text,
  45. // and does not start with phone-number punctuation,
  46. // check the previous character.
  47. if (offset > 0 && !LEAD_CLASS_LEADING.test(candidate)) {
  48. var previousChar = text[offset - 1]; // We return null if it is a latin letter or an invalid punctuation symbol.
  49. if ((0, _utf.isInvalidPunctuationSymbol)(previousChar) || (0, _utf.isLatinLetter)(previousChar)) {
  50. return false;
  51. }
  52. }
  53. var lastCharIndex = offset + candidate.length;
  54. if (lastCharIndex < text.length) {
  55. var nextChar = text[lastCharIndex];
  56. if ((0, _utf.isInvalidPunctuationSymbol)(nextChar) || (0, _utf.isLatinLetter)(nextChar)) {
  57. return false;
  58. }
  59. }
  60. }
  61. return true;
  62. }
  63. //# sourceMappingURL=isValidCandidate.js.map