isValidPreCandidate.js 927 B

1234567891011121314151617181920212223242526272829
  1. // Matches strings that look like dates using "/" as a separator.
  2. // Examples: 3/10/2011, 31/10/96 or 08/31/95.
  3. const SLASH_SEPARATED_DATES = /(?:(?:[0-3]?\d\/[01]?\d)|(?:[01]?\d\/[0-3]?\d))\/(?:[12]\d)?\d{2}/
  4. // Matches timestamps.
  5. // Examples: "2012-01-02 08:00".
  6. // Note that the reg-ex does not include the
  7. // trailing ":\d\d" -- that is covered by TIME_STAMPS_SUFFIX.
  8. const TIME_STAMPS = /[12]\d{3}[-/]?[01]\d[-/]?[0-3]\d +[0-2]\d$/
  9. const TIME_STAMPS_SUFFIX_LEADING = /^:[0-5]\d/
  10. export default function isValidPreCandidate(candidate, offset, text)
  11. {
  12. // Skip a match that is more likely to be a date.
  13. if (SLASH_SEPARATED_DATES.test(candidate)) {
  14. return false
  15. }
  16. // Skip potential time-stamps.
  17. if (TIME_STAMPS.test(candidate))
  18. {
  19. const followingText = text.slice(offset + candidate.length)
  20. if (TIME_STAMPS_SUFFIX_LEADING.test(followingText)) {
  21. return false
  22. }
  23. }
  24. return true
  25. }