fix: make sure weekday parsing in quick add magic ignores the casing

Resolves https://kolaente.dev/vikunja/frontend/issues/2105
This commit is contained in:
kolaente 2022-07-11 12:35:08 +02:00
parent 990639dd24
commit dff5d84ebb
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
2 changed files with 54 additions and 9 deletions

View file

@ -222,8 +222,8 @@ export const getDateFromTextIn = (text: string, now: Date = new Date()) => {
} }
const getDateFromWeekday = (text: string): dateFoundResult => { const getDateFromWeekday = (text: string): dateFoundResult => {
const matcher: RegExp = / (next )?(monday|mon|tuesday|tue|wednesday|wed|thursday|thu|friday|fri|saturday|sat|sunday|sun)($| )/ig const matcher: RegExp = / (next )?(monday|mon|tuesday|tue|wednesday|wed|thursday|thu|friday|fri|saturday|sat|sunday|sun)($| )/g
const results: string[] | null = matcher.exec(text) const results: string[] | null = matcher.exec(text.toLowerCase()) // The i modifier does not seem to work.
if (results === null) { if (results === null) {
return { return {
foundText: null, foundText: null,

View file

@ -122,6 +122,18 @@ describe('Parse Task Text', () => {
expect(result.date.getMonth()).toBe(nextMonday.getMonth()) expect(result.date.getMonth()).toBe(nextMonday.getMonth())
expect(result.date.getDate()).toBe(nextMonday.getDate()) expect(result.date.getDate()).toBe(nextMonday.getDate())
}) })
it('should recognize next monday and ignore casing', () => {
const result = parseTaskText('Lorem Ipsum nExt Monday')
const untilNextMonday = calculateDayInterval('nextMonday')
expect(result.text).toBe('Lorem Ipsum')
const nextMonday = new Date()
nextMonday.setDate(nextMonday.getDate() + untilNextMonday)
expect(result.date.getFullYear()).toBe(nextMonday.getFullYear())
expect(result.date.getMonth()).toBe(nextMonday.getMonth())
expect(result.date.getDate()).toBe(nextMonday.getDate())
})
it('should recognize this weekend', () => { it('should recognize this weekend', () => {
const result = parseTaskText('Lorem Ipsum this weekend') const result = parseTaskText('Lorem Ipsum this weekend')
@ -201,14 +213,47 @@ describe('Parse Task Text', () => {
expect(result.date.getMonth()).toBe(date.getMonth()) expect(result.date.getMonth()).toBe(date.getMonth())
expect(result.date.getDate()).toBe(date.getDate()) expect(result.date.getDate()).toBe(date.getDate())
}) })
it('should recognize weekdays', () => {
const result = parseTaskText('Lorem Ipsum thu') const cases = {
'monday': 1,
'Monday': 1,
'mon': 1,
'Mon': 1,
'tuesday': 2,
'Tuesday': 2,
'tue': 2,
'Tue': 2,
'wednesday': 3,
'Wednesday': 3,
'wed': 3,
'Wed': 3,
'thursday': 4,
'Thursday': 4,
'thu': 4,
'Thu': 4,
'friday': 5,
'Friday': 5,
'fri': 5,
'Fri': 5,
'saturday': 6,
'Saturday': 6,
'sat': 6,
'Sat': 6,
'sunday': 7,
'Sunday': 7,
'sun': 7,
'Sun': 7,
}
for (const c in cases) {
it(`should recognize ${c} as weekday`, () => {
const result = parseTaskText(`Lorem Ipsum ${c}`)
expect(result.text).toBe('Lorem Ipsum') expect(result.text).toBe('Lorem Ipsum')
const nextThursday = new Date() const nextDate = new Date()
nextThursday.setDate(nextThursday.getDate() + ((4 + 7 - nextThursday.getDay()) % 7)) nextDate.setDate(nextDate.getDate() + ((cases[c] + 7 - nextDate.getDay()) % 7))
expect(`${result.date.getFullYear()}-${result.date.getMonth()}-${result.date.getDate()}`).toBe(`${nextThursday.getFullYear()}-${nextThursday.getMonth()}-${nextThursday.getDate()}`) expect(`${result.date.getFullYear()}-${result.date.getMonth()}-${result.date.getDate()}`).toBe(`${nextDate.getFullYear()}-${nextDate.getMonth()}-${nextDate.getDate()}`)
}) })
}
it('should recognize weekdays with time', () => { it('should recognize weekdays with time', () => {
const result = parseTaskText('Lorem Ipsum thu at 14:00') const result = parseTaskText('Lorem Ipsum thu at 14:00')