fix: iterate past rejected middle matches in matchDateAtBoundary()
When the first regex match is a rejected middle-of-text date, continue searching for subsequent matches instead of returning null. This fixes cases like "The 9/11 Report due 10/12" where 9/11 is rejected but 10/12 at the end should still be parsed.
This commit is contained in:
parent
3f0bf71d30
commit
77b8403c24
|
|
@ -24,18 +24,18 @@ const monthsRegexGroup = '(january|february|march|april|june|july|august|septemb
|
|||
*/
|
||||
function matchDateAtBoundary(text: string, pattern: string): RegExpExecArray | null {
|
||||
const regex = new RegExp(`(^| )${pattern}($| )`, 'gi')
|
||||
const result = regex.exec(text)
|
||||
if (result === null) return null
|
||||
let result: RegExpExecArray | null
|
||||
while ((result = regex.exec(text)) !== null) {
|
||||
const matchEnd = result.index + result[0].length
|
||||
const isAtStart = result.index === 0
|
||||
const isAtEnd = matchEnd >= text.length
|
||||
|
||||
const matchEnd = result.index + result[0].length
|
||||
const isAtStart = result.index === 0
|
||||
const isAtEnd = matchEnd >= text.length
|
||||
if (isAtStart || isAtEnd) return result
|
||||
|
||||
if (isAtStart || isAtEnd) return result
|
||||
|
||||
// Allow middle-of-text matches when followed by a time expression
|
||||
const afterMatch = text.substring(matchEnd)
|
||||
if (/^(at |@ )/i.test(afterMatch)) return result
|
||||
// Allow middle-of-text matches when followed by a time expression
|
||||
const afterMatch = text.substring(matchEnd)
|
||||
if (/^(at |@ )/i.test(afterMatch)) return result
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
|
|
|||
|
|
@ -398,6 +398,7 @@ describe('Parse Task Text', () => {
|
|||
{input: 'Lorem Ipsum 06/26/2021', dateStr: '2021-6-26', text: 'Lorem Ipsum'},
|
||||
{input: '01.02 Lorem Ipsum', dateStr: '2022-2-1', text: 'Lorem Ipsum'},
|
||||
{input: 'Lorem Ipsum 01.02', dateStr: '2022-2-1', text: 'Lorem Ipsum'},
|
||||
{input: 'The 9/11 Report due 10/12', dateStr: '2021-10-12', text: 'The 9/11 Report due'},
|
||||
]
|
||||
|
||||
boundaryTests.forEach(({input, dateStr, text}) => {
|
||||
|
|
|
|||
Loading…
Reference in New Issue