From 4b64e27d2bad3811418489f499d8e2ff88401e4a Mon Sep 17 00:00:00 2001 From: kolaente Date: Sun, 26 Sep 2021 15:00:11 +0200 Subject: [PATCH] fix(natural language parser): parts of week days in other words --- src/helpers/time/parseDate.ts | 11 +++++++++-- src/modules/parseTaskText.test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/helpers/time/parseDate.ts b/src/helpers/time/parseDate.ts index 11aa47b38..218491a39 100644 --- a/src/helpers/time/parseDate.ts +++ b/src/helpers/time/parseDate.ts @@ -222,7 +222,7 @@ export const getDateFromTextIn = (text: string, now: Date = new Date()) => { } 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) if (results === null) { return { @@ -274,8 +274,15 @@ const getDateFromWeekday = (text: string): dateFoundResult => { const distance: number = (day + 7 - currentDay) % 7 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 { - foundText: results[0], + foundText: foundText, date: date, } } diff --git a/src/modules/parseTaskText.test.js b/src/modules/parseTaskText.test.js index 6e28058cd..6b445426b 100644 --- a/src/modules/parseTaskText.test.js +++ b/src/modules/parseTaskText.test.js @@ -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', () => {