diff --git a/src/utils/reminder.ts b/src/utils/reminder.ts index d856936..4eedf46 100644 --- a/src/utils/reminder.ts +++ b/src/utils/reminder.ts @@ -46,13 +46,23 @@ export type dbReminder = { * @returns An object with the time and the option */ export const splitTime = (time: string) => { - if (time?.endsWith("@")) { - return { time: time.slice(0, -1), option: OptionReminder.Mention }; - } else if (time?.toLowerCase().endsWith("p")) { - return { time: time.slice(0, -1), option: OptionReminder.DirectMessage }; - } + const mapping = { + [OptionReminder.DirectMessage]: "p", + [OptionReminder.Mention]: "@", + }; - return { time: time, option: OptionReminder.Nothing }; + const lowered = time.toLowerCase(); + const trimmed = lowered.replaceAll(new RegExp(Object.values(mapping).join("|"), "g"), ""); + + // Depending of the last character of the string + switch (lowered.slice(-1)) { + case mapping[OptionReminder.Mention]: + return { time: trimmed, option: OptionReminder.Mention }; + case mapping[OptionReminder.DirectMessage]: + return { time: trimmed, option: OptionReminder.DirectMessage }; + default: + return { time: time, option: OptionReminder.Nothing }; + } }; /**