From d7ce8dd320a5c61c9060e63a6b43d78614f772e3 Mon Sep 17 00:00:00 2001 From: kolaente Date: Thu, 13 Jul 2023 18:20:30 +0200 Subject: [PATCH] fix(quick add magic): repeating intervals in words Resolves https://kolaente.dev/vikunja/frontend/issues/3676 --- src/modules/parseTaskText.test.ts | 39 ++++++++++++++++++++++++------- src/modules/parseTaskText.ts | 10 ++++---- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/modules/parseTaskText.test.ts b/src/modules/parseTaskText.test.ts index 60845a4a8..efd599844 100644 --- a/src/modules/parseTaskText.test.ts +++ b/src/modules/parseTaskText.test.ts @@ -719,15 +719,6 @@ describe('Parse Task Text', () => { 'every year': {type: 'years', amount: 1}, 'every 1 year': {type: 'years', amount: 1}, 'every 4 years': {type: 'years', amount: 4}, - 'annually': {type: 'years', amount: 1}, - 'biannually': {type: 'months', amount: 6}, - 'semiannually': {type: 'months', amount: 6}, - 'biennially': {type: 'years', amount: 2}, - 'daily': {type: 'days', amount: 1}, - 'hourly': {type: 'hours', amount: 1}, - 'monthly': {type: 'months', amount: 1}, - 'weekly': {type: 'weeks', amount: 1}, - 'yearly': {type: 'years', amount: 1}, 'every one hour': {type: 'hours', amount: 1}, // maybe unnesecary but better to include it for completeness sake 'every two hours': {type: 'hours', amount: 2}, 'every three hours': {type: 'hours', amount: 3}, @@ -738,6 +729,15 @@ describe('Parse Task Text', () => { 'every eight hours': {type: 'hours', amount: 8}, 'every nine hours': {type: 'hours', amount: 9}, 'every ten hours': {type: 'hours', amount: 10}, + 'annually': {type: 'years', amount: 1}, + 'biannually': {type: 'months', amount: 6}, + 'semiannually': {type: 'months', amount: 6}, + 'biennially': {type: 'years', amount: 2}, + 'daily': {type: 'days', amount: 1}, + 'hourly': {type: 'hours', amount: 1}, + 'monthly': {type: 'months', amount: 1}, + 'weekly': {type: 'weeks', amount: 1}, + 'yearly': {type: 'years', amount: 1}, } as Record for (const c in cases) { @@ -749,5 +749,26 @@ describe('Parse Task Text', () => { expect(result?.repeats?.amount).toBe(cases[c].amount) }) } + + const wordCases = [ + 'annually', + 'biannually', + 'semiannually', + 'biennially', + 'daily', + 'hourly', + 'monthly', + 'weekly', + 'yearly', + ] + + wordCases.forEach(c => { + it(`should ignore recurring periods if they are part of a word ${c}`, () => { + const result = parseTaskText(`Lorem Ipsum word${c}notword`) + + expect(result.text).toBe(`Lorem Ipsum word${c}notword`) + expect(result?.repeats).toBeNull() + }) + }) }) }) diff --git a/src/modules/parseTaskText.ts b/src/modules/parseTaskText.ts index 5b20d16c9..5b1b1063c 100644 --- a/src/modules/parseTaskText.ts +++ b/src/modules/parseTaskText.ts @@ -165,7 +165,7 @@ const getPriority = (text: string, prefix: string): number | null => { } const getRepeats = (text: string): repeatParsedResult => { - const regex = /((every|each) (([0-9]+|one|two|three|four|five|six|seven|eight|nine|ten) )?(hours?|days?|weeks?|months?|years?))|annually|biannually|semiannually|biennially|daily|hourly|monthly|weekly|yearly/ig + const regex = /(^| )(((every|each) (([0-9]+|one|two|three|four|five|six|seven|eight|nine|ten) )?(hours?|days?|weeks?|months?|years?))|(annually|biannually|semiannually|biennially|daily|hourly|monthly|weekly|yearly))($| )/ig const results = regex.exec(text) if (results === null) { return { @@ -175,7 +175,7 @@ const getRepeats = (text: string): repeatParsedResult => { } let amount = 1 - switch (results[3] ? results[3].trim() : undefined) { + switch (results[5] ? results[5].trim() : undefined) { case 'one': amount = 1 break @@ -207,11 +207,11 @@ const getRepeats = (text: string): repeatParsedResult => { amount = 10 break default: - amount = results[3] ? parseInt(results[3]) : 1 + amount = results[5] ? parseInt(results[5]) : 1 } let type: IRepeatType = REPEAT_TYPES.Hours - switch (results[0]) { + switch (results[2]) { case 'biennially': type = REPEAT_TYPES.Years amount = 2 @@ -238,7 +238,7 @@ const getRepeats = (text: string): repeatParsedResult => { type = REPEAT_TYPES.Weeks break default: - switch (results[5]) { + switch (results[7]) { case 'hour': case 'hours': type = REPEAT_TYPES.Hours