diff options
| author | Brandon Wong <29965003+brandonw3612@users.noreply.github.com> | 2025-04-13 22:50:45 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-13 21:50:45 +0100 |
| commit | 95f504c0bc0b9ec0930b6c6facefc1a8ea093192 (patch) | |
| tree | 967221d7f4f5b716cc8fd3d92b5869bacf1d6089 /packages/shared/utils | |
| parent | 5bdb2d944a08f63772497e203f47533ffb640d82 (diff) | |
| download | karakeep-95f504c0bc0b9ec0930b6c6facefc1a8ea093192.tar.zst | |
feat: add support for filtering by bookmark age (#1228)
Diffstat (limited to 'packages/shared/utils')
| -rw-r--r-- | packages/shared/utils/relativeDateUtils.ts | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/packages/shared/utils/relativeDateUtils.ts b/packages/shared/utils/relativeDateUtils.ts new file mode 100644 index 00000000..437c3ea8 --- /dev/null +++ b/packages/shared/utils/relativeDateUtils.ts @@ -0,0 +1,42 @@ +interface RelativeDate { + direction: "newer" | "older"; + amount: number; + unit: "day" | "week" | "month" | "year"; +} + +const parseRelativeDate = (date: string): RelativeDate => { + const match = date.match(/^([<>])(\d+)([dwmy])$/); + if (!match) { + throw new Error(`Invalid relative date format: ${date}`); + } + const direction = match[1] === "<" ? "newer" : "older"; + const amount = parseInt(match[2], 10); + const unit = { + d: "day", + w: "week", + m: "month", + y: "year", + }[match[3]] as "day" | "week" | "month" | "year"; + return { direction, amount, unit }; +}; + +const toAbsoluteDate = (relativeDate: RelativeDate): Date => { + const date = new Date(); + switch (relativeDate.unit) { + case "day": + date.setDate(date.getDate() - relativeDate.amount); + break; + case "week": + date.setDate(date.getDate() - relativeDate.amount * 7); + break; + case "month": + date.setMonth(date.getMonth() - relativeDate.amount); + break; + case "year": + date.setFullYear(date.getFullYear() - relativeDate.amount); + break; + } + return date; +}; + +export { parseRelativeDate, toAbsoluteDate }; |
