fix(natural language parser): parts of week days in other words

This commit is contained in:
kolaente 2021-09-26 15:00:11 +02:00
parent 403d77ce14
commit 4b64e27d2b
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
2 changed files with 35 additions and 2 deletions

View file

@ -222,7 +222,7 @@ 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)($| )/ig
const results: string[] | null = matcher.exec(text) const results: string[] | null = matcher.exec(text)
if (results === null) { if (results === null) {
return { return {
@ -274,8 +274,15 @@ const getDateFromWeekday = (text: string): dateFoundResult => {
const distance: number = (day + 7 - currentDay) % 7 const distance: number = (day + 7 - currentDay) % 7
date.setDate(date.getDate() + distance) date.setDate(date.getDate() + distance)
// This a space at the end of the found text to not break parsing suffix strings like "at 14:00" in cases where the
// matched string comes with a space at the end (last part of the regex).
let foundText = results[0]
if (foundText.endsWith(' ')) {
foundText = foundText.substr(0, foundText.length - 1)
}
return { return {
foundText: results[0], foundText: foundText,
date: date, date: date,
} }
} }

View file

@ -247,6 +247,32 @@ describe('Parse Task Text', () => {
}) })
} }
}) })
// This tests only standalone days are recognized and not things like "github", "monitor" or "renewed".
// We're not using real words here to generate tests for all days on the fly.
for (const d in days) {
it(`should not recognize ${d} with a space before it but none after it`, () => {
const text = `Lorem Ipsum ${d}ipsum`
const result = parseTaskText(text)
expect(result.text).toBe(text)
expect(result.date).toBeNull()
})
it(`should not recognize ${d} with a space after it but none before it`, () => {
const text = `Lorem ipsum${d} dolor`
const result = parseTaskText(text)
expect(result.text).toBe(text)
expect(result.date).toBeNull()
})
it(`should not recognize ${d} with no space before or after it`, () => {
const text = `Lorem Ipsum lorem${d}ipsum`
const result = parseTaskText(text)
expect(result.text).toBe(text)
expect(result.date).toBeNull()
})
}
}) })
describe('Parse date from text', () => { describe('Parse date from text', () => {