isValidCandidate.js 3.0 KB

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