fix(quick-add-magic): prevent parsing partial keywords in words (#2140)
- Fixed date parser incorrectly extracting date components from within words - "Renovation - 2nd Floor Bath" no longer becomes "Reation - Floor Bath" with a due date of November 2nd ## Changes - `getMonthFromText` now requires word boundaries, preventing "nov" from matching inside "Renovation" or "mar" inside "Remark" - `getDayFromText` now only matches ordinals when followed by end-of-string, time expressions, or month names, preventing "2nd Floor" from being parsed as a date Resolves https://community.vikunja.io/t/quick-add-magic-unintended-date-parsing/4259
This commit is contained in:
parent
a86bbfa121
commit
8419794b65
|
|
@ -316,7 +316,9 @@ const getDateFromWeekday = (text: string, date: Date = new Date()): dateFoundRes
|
|||
}
|
||||
|
||||
const getDayFromText = (text: string, now: Date = new Date()) => {
|
||||
const matcher = /(^| )(([1-2][0-9])|(3[01])|(0?[1-9]))(st|nd|rd|th|\.)($| )/ig
|
||||
// Only match ordinals when followed by end-of-string, time expressions, or month names
|
||||
// This prevents matching "2nd Floor" or "13th floor" as dates
|
||||
const matcher = new RegExp('(^| )(([1-2][0-9])|(3[01])|(0?[1-9]))(st|nd|rd|th|\\.)(?=$| at | @ | ' + monthsRegexGroup + ')', 'ig')
|
||||
const results = matcher.exec(text)
|
||||
if (results === null) {
|
||||
return {
|
||||
|
|
@ -348,7 +350,7 @@ const getDayFromText = (text: string, now: Date = new Date()) => {
|
|||
}
|
||||
|
||||
const getMonthFromText = (text: string, date: Date) => {
|
||||
const matcher = new RegExp(monthsRegexGroup, 'ig')
|
||||
const matcher = new RegExp('\\b' + monthsRegexGroup + '\\b', 'ig')
|
||||
const results = matcher.exec(text)
|
||||
|
||||
if (results === null) {
|
||||
|
|
|
|||
|
|
@ -315,7 +315,7 @@ describe('Parse Task Text', () => {
|
|||
expect(result.text).toBe('Lorem Ipsum github')
|
||||
expect(result.date).toBeNull()
|
||||
})
|
||||
describe('Should not recognize weekdays in words', () => {
|
||||
describe('Should not recognize partial keywords in words', () => {
|
||||
const cases = [
|
||||
'renewed',
|
||||
'github',
|
||||
|
|
@ -328,6 +328,11 @@ describe('Parse Task Text', () => {
|
|||
'monitor blood pressure',
|
||||
'Monitor blood pressure',
|
||||
'buy almonds',
|
||||
'Renovation',
|
||||
'Remark',
|
||||
'Renovation - 2nd Floor Bath',
|
||||
'Remark - 13th floor',
|
||||
'13th floor - remark',
|
||||
]
|
||||
|
||||
cases.forEach(c => {
|
||||
|
|
@ -349,6 +354,12 @@ describe('Parse Task Text', () => {
|
|||
expect(result.text).toBe(`Lorem Ipsum ${c} dolor`)
|
||||
expect(result.date).toBeNull()
|
||||
})
|
||||
it(`should not recognize text with ${c} as special keywords`, () => {
|
||||
const result = parseTaskText(c)
|
||||
|
||||
expect(result.text).toBe(c)
|
||||
expect(result.date).toBeNull()
|
||||
})
|
||||
})
|
||||
})
|
||||
it('should not recognize date number with no spacing around them', () => {
|
||||
|
|
|
|||
Loading…
Reference in New Issue