fix(natural language parser): fix parsing short days

related: #773
This commit is contained in:
kolaente 2021-09-26 14:38:15 +02:00
parent e918b82cfa
commit 403d77ce14
Signed by untrusted user: konrad
GPG Key ID: F40E70337AB24C9B
2 changed files with 45 additions and 3 deletions

View File

@ -222,7 +222,7 @@ export const getDateFromTextIn = (text: string, now: Date = new Date()) => {
}
const getDateFromWeekday = (text: string): dateFoundResult => {
const matcher: RegExp = / (mon|monday|tue|tuesday|wed|wednesday|thu|thursday|fri|friday|sat|saturday|sun|sunday)/ig
const matcher: RegExp = / (next )?(monday|mon|tuesday|tue|wednesday|wed|thursday|thu|friday|fri|saturday|sat|sunday|sun)/ig
const results: string[] | null = matcher.exec(text)
if (results === null) {
return {
@ -235,7 +235,7 @@ const getDateFromWeekday = (text: string): dateFoundResult => {
const currentDay: number = date.getDay()
let day: number = 0
switch (results[1]) {
switch (results[2]) {
case 'mon':
case 'monday':
day = 1
@ -275,7 +275,7 @@ const getDateFromWeekday = (text: string): dateFoundResult => {
date.setDate(date.getDate() + distance)
return {
foundText: results[1],
foundText: results[0],
date: date,
}
}

View File

@ -207,6 +207,48 @@ describe('Parse Task Text', () => {
expect(result.date).toBeNull()
})
describe('Parse weekdays', () => {
const days = {
'mon': 1,
'monday': 1,
'tue': 2,
'tuesday': 2,
'wed': 3,
'wednesday': 3,
'thu': 4,
'thursday': 4,
'fri': 5,
'friday': 5,
'sat': 6,
'saturday': 6,
'sun': 7,
'sunday': 7,
}
const prefix = [
'next ',
'',
]
prefix.forEach(p => {
for (const d in days) {
it(`should recognize ${p}${d}`, () => {
const result = parseTaskText(`Lorem Ipsum ${p}${d}`)
const next = new Date()
const distance = (days[d] + 7 - next.getDay()) % 7
next.setDate(next.getDate() + distance)
expect(result.text).toBe('Lorem Ipsum')
expect(result.date.getFullYear()).toBe(next.getFullYear())
expect(result.date.getMonth()).toBe(next.getMonth())
expect(result.date.getDate()).toBe(next.getDate())
})
}
})
})
describe('Parse date from text', () => {
const now = new Date()
now.setFullYear(2021, 5, 24)