1
0

fix(quick add magic): assume today when no date was specified with time

This commit is contained in:
kolaente 2024-06-06 21:44:29 +02:00
parent 5af8b54618
commit 92cdf5fe9c
No known key found for this signature in database
GPG Key ID: F40E70337AB24C9B
3 changed files with 24 additions and 2 deletions

View File

@ -79,6 +79,12 @@ export const parseDate = (text: string, now: Date = new Date()): dateParseResult
parsed = getDateFromText(text, now) parsed = getDateFromText(text, now)
if (parsed.date === null) { if (parsed.date === null) {
const time = addTimeToDate(text, new Date(now), parsed.foundText)
if (time.date !== null && +now !== +time.date) {
return time
}
return { return {
newText: replaceAll(text, parsed.foundText, ''), newText: replaceAll(text, parsed.foundText, ''),
date: parsed.date, date: parsed.date,
@ -122,7 +128,7 @@ const addTimeToDate = (text: string, date: Date, previousMatch: string | null):
const replace = results !== null ? results[0] : previousMatch const replace = results !== null ? results[0] : previousMatch
return { return {
newText: replaceAll(text, replace, '').trim(), newText: replaceAll(text, replace, '').trim(),
date: date, date,
} }
} }

View File

@ -804,6 +804,17 @@ describe('Parse Task Text', () => {
expect(result?.repeats?.type).toBe(cases[c].type) expect(result?.repeats?.type).toBe(cases[c].type)
expect(result?.repeats?.amount).toBe(cases[c].amount) expect(result?.repeats?.amount).toBe(cases[c].amount)
}) })
it(`should parse ${c} as recurring date every ${cases[c].amount} ${cases[c].type} at 11:42`, () => {
const result = parseTaskText(`Lorem Ipsum ${c} at 11:42`)
expect(result.text).toBe('Lorem Ipsum')
expect(result?.repeats?.type).toBe(cases[c].type)
expect(result?.repeats?.amount).toBe(cases[c].amount)
const now = new Date()
expect(`${result?.date?.getFullYear()}-${result?.date?.getMonth()}-${result?.date?.getDate()}`).toBe(`${now.getFullYear()}-${now.getMonth()}-${now.getDate()}`)
expect(`${result?.date?.getHours()}:${result?.date?.getMinutes()}`).toBe('11:42')
})
} }
const wordCases = [ const wordCases = [

View File

@ -261,9 +261,14 @@ const getRepeats = (text: string): repeatParsedResult => {
break break
} }
} }
let matchedText = results[0]
if(matchedText.endsWith(' ')) {
matchedText = matchedText.substring(0, matchedText.length - 1)
}
return { return {
textWithoutMatched: text.replace(results[0], ''), textWithoutMatched: text.replace(matchedText, ''),
repeats: { repeats: {
amount, amount,
type, type,