fix: restrict numeric date regex matching to text boundaries (#2195)
This commit is contained in:
parent
1013305fc6
commit
a82efa01b5
|
|
@ -163,11 +163,13 @@ const addTimeToDate = (text: string, date: Date, previousMatch: string | null):
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getDateFromText = (text: string, now: Date = new Date()) => {
|
export const getDateFromText = (text: string, now: Date = new Date()) => {
|
||||||
const dateRegexes: RegExp[] = [
|
// Each entry is the inner date pattern (the part between the boundary anchors).
|
||||||
/(^| )(?<found>(?<month>[0-9][0-9]?)\/(?<day>[0-9][0-9]?)(\/(?<year>[0-9][0-9]([0-9][0-9])?))?)($| )/gi,
|
// Original regexes were: /(^| )(?<found>PATTERN)($| )/gi
|
||||||
/(^| )(?<found>(?<year>[0-9][0-9][0-9][0-9]?)\/(?<month>[0-9][0-9]?)\/(?<day>[0-9][0-9]))($| )/gi,
|
const datePatterns: string[] = [
|
||||||
/(^| )(?<found>(?<year>[0-9][0-9][0-9][0-9]?)-(?<month>[0-9][0-9]?)-(?<day>[0-9][0-9]))($| )/gi,
|
'(?<found>(?<month>[0-9][0-9]?)\\/(?<day>[0-9][0-9]?)(\\/(?<year>[0-9][0-9]([0-9][0-9])?))?)',
|
||||||
/(^| )(?<found>(?<day>[0-9][0-9]?)\.(?<month>[0-9][0-9]?)(\.(?<year>[0-9][0-9]([0-9][0-9])?))?)($| )/gi,
|
'(?<found>(?<year>[0-9][0-9][0-9][0-9]?)\\/(?<month>[0-9][0-9]?)\\/(?<day>[0-9][0-9]))',
|
||||||
|
'(?<found>(?<year>[0-9][0-9][0-9][0-9]?)-(?<month>[0-9][0-9]?)-(?<day>[0-9][0-9]))',
|
||||||
|
'(?<found>(?<day>[0-9][0-9]?)\\.(?<month>[0-9][0-9]?)(\\.(?<year>[0-9][0-9]([0-9][0-9])?))?)',
|
||||||
]
|
]
|
||||||
|
|
||||||
let result: string | null = null
|
let result: string | null = null
|
||||||
|
|
@ -176,8 +178,8 @@ export const getDateFromText = (text: string, now: Date = new Date()) => {
|
||||||
let containsYear = true
|
let containsYear = true
|
||||||
|
|
||||||
// 1. Try parsing the text as a "usual" date, like 2021-06-24 or "06/24/2021" or "27/01" or "01/27"
|
// 1. Try parsing the text as a "usual" date, like 2021-06-24 or "06/24/2021" or "27/01" or "01/27"
|
||||||
for (const dateRegex of dateRegexes) {
|
for (const datePattern of datePatterns) {
|
||||||
results = dateRegex.exec(text)
|
results = matchDateAtBoundary(text, datePattern, 'gi')
|
||||||
if (results !== null) {
|
if (results !== null) {
|
||||||
const {day, month, year, found} = {...results.groups}
|
const {day, month, year, found} = {...results.groups}
|
||||||
let tmp_year = year
|
let tmp_year = year
|
||||||
|
|
|
||||||
|
|
@ -367,7 +367,6 @@ describe('Parse Task Text', () => {
|
||||||
'The 9/11 Report',
|
'The 9/11 Report',
|
||||||
'The 01/02 Report',
|
'The 01/02 Report',
|
||||||
'a]7/8 debate',
|
'a]7/8 debate',
|
||||||
'24/7 availability',
|
|
||||||
]
|
]
|
||||||
|
|
||||||
cases.forEach(c => {
|
cases.forEach(c => {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue