Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

core: impl reminder time formatting

Abdullah Attab7ebbfda1d60
diff --git a/packages/core/__tests__/reminders.test.js b/packages/core/__tests__/reminders.test.js
new file mode 100644
index 000000000..e1b9464d9
--- /dev/null
+++ b/packages/core/__tests__/reminders.test.js
@@ -0,0 +1,153 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2022 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+import { formatReminderTime } from "../collections/reminders";
+import MockDate from "mockdate";
+
+describe("format reminder time", () => {
+ afterAll(() => {
+ MockDate.reset();
+ });
+
+ beforeAll(() => {
+ MockDate.set(new Date(2022, 5, 6, 5, 5));
+ });
+
+ test("daily reminder [today]", () => {
+ const reminder = {
+ recurringMode: "day",
+ date: new Date(0).setHours(14),
+ mode: "repeat"
+ };
+ expect(formatReminderTime(reminder)).toBe("Upcoming: Today, 02:00 PM");
+ });
+
+ test("daily reminder [tomorrow]", () => {
+ const reminder = {
+ recurringMode: "day",
+ date: new Date(0).setHours(3),
+ mode: "repeat"
+ };
+ expect(formatReminderTime(reminder)).toBe("Upcoming: Tomorrow, 03:00 AM");
+ });
+
+ test("weekly reminder [current week]", () => {
+ const reminder = {
+ recurringMode: "week",
+ date: new Date(0).setHours(3),
+ selectedDays: [3, 5],
+ mode: "repeat"
+ };
+ expect(formatReminderTime(reminder)).toBe(
+ "Upcoming: Wed, 2022-06-08 03:00 AM"
+ );
+ });
+
+ test("weekly reminder [next week]", () => {
+ const reminder = {
+ recurringMode: "week",
+ date: new Date(0).setHours(3),
+ selectedDays: [0],
+ mode: "repeat"
+ };
+ expect(formatReminderTime(reminder)).toBe(
+ "Upcoming: Sun, 2022-06-12 03:00 AM"
+ );
+ });
+
+ test("weekly reminder [current week, multiple days]", () => {
+ const reminder = {
+ recurringMode: "week",
+ date: new Date(0).setHours(3),
+ selectedDays: [0, 5, 6],
+ mode: "repeat"
+ };
+ expect(formatReminderTime(reminder)).toBe(
+ "Upcoming: Fri, 2022-06-10 03:00 AM"
+ );
+ });
+
+ test("weekly reminder [current week, today]", () => {
+ const reminder = {
+ recurringMode: "week",
+ date: new Date(0).setHours(3),
+ selectedDays: [0, 1],
+ mode: "repeat"
+ };
+ expect(formatReminderTime(reminder)).toBe("Upcoming: Today, 03:00 AM");
+ });
+
+ test("monthly reminder [current month]", () => {
+ const reminder = {
+ recurringMode: "month",
+ date: new Date(0).setHours(3),
+ selectedDays: [12, 18],
+ mode: "repeat"
+ };
+ expect(formatReminderTime(reminder)).toBe(
+ "Upcoming: Sun, 2022-06-12 03:00 AM"
+ );
+ });
+
+ test("monthly reminder [next month]", () => {
+ const reminder = {
+ recurringMode: "month",
+ date: new Date(0).setHours(3),
+ selectedDays: [1, 2, 3],
+ mode: "repeat"
+ };
+ expect(formatReminderTime(reminder)).toBe(
+ "Upcoming: Fri, 2022-07-01 03:00 AM"
+ );
+ });
+
+ test("monthly reminder [current month, today]", () => {
+ const reminder = {
+ recurringMode: "month",
+ date: new Date(0).setHours(3),
+ selectedDays: [6],
+ mode: "repeat"
+ };
+ expect(formatReminderTime(reminder)).toBe("Upcoming: Today, 03:00 AM");
+ });
+
+ test("today", () => {
+ const reminder = {
+ date: new Date(2022, 5, 6, 8, 5).getTime(),
+ mode: "once"
+ };
+ expect(formatReminderTime(reminder)).toBe("Upcoming: Today, 08:05 AM");
+ });
+
+ test("tomorrow", () => {
+ const reminder = {
+ date: new Date(2022, 5, 7, 8, 5).getTime(),
+ mode: "once"
+ };
+ expect(formatReminderTime(reminder)).toBe("Upcoming: Tomorrow, 08:05 AM");
+ });
+
+ test("yesterday", () => {
+ const reminder = {
+ date: new Date(2022, 5, 5, 8, 5).getTime(),
+ mode: "once"
+ };
+ expect(formatReminderTime(reminder)).toBe("Last: Yesterday, 08:05 AM");
+ });
+});
diff --git a/packages/core/collections/reminders.js b/packages/core/collections/reminders.js
index 012b4f7e2..0ab2202c9 100644
--- a/packages/core/collections/reminders.js
+++ b/packages/core/collections/reminders.js
@@ -19,6 +19,18 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import getId from "../utils/id";
import Collection from "./collection";
+import dayjs from "dayjs";
+import isBetween from "dayjs/plugin/isBetween";
+import isToday from "dayjs/plugin/isToday";
+import isTomorrow from "dayjs/plugin/isTomorrow";
+import isYesterday from "dayjs/plugin/isYesterday";
+import isSameOrBefore from "dayjs/plugin/isSameOrBefore";
+
+dayjs.extend(isTomorrow);
+dayjs.extend(isSameOrBefore);
+dayjs.extend(isYesterday);
+dayjs.extend(isToday);
+dayjs.extend(isBetween);
/**
* @typedef {{
@@ -106,3 +118,76 @@ export default class Reminders extends Collection {
}
}
}
+
+/**
+ * @param {Reminder} reminder
+ */
+export function formatReminderTime(reminder) {
+ const { date } = reminder;
+ let time = date;
+ if (reminder.mode === "repeat") {
+ time = getUpcomingReminderTime(reminder);
+ }
+
+ if (dayjs(time).isTomorrow()) {
+ return `Upcoming: Tomorrow, ${dayjs(time).format("hh:mm A")}`;
+ }
+
+ if (dayjs(time).isYesterday()) {
+ return `Last: Yesterday, ${dayjs(time).format("hh:mm A")}`;
+ }
+
+ if (dayjs(time).isToday()) {
+ return `Upcoming: Today, ${dayjs(time).format("hh:mm A")}`;
+ }
+
+ const isPast = dayjs(time).isBefore(dayjs());
+ return dayjs(time).format(
+ `${isPast ? "[Last]:" : "[Upcoming]:"} ddd, YYYY-MM-DD hh:mm A`
+ );
+}
+
+/**
+ * @param {Reminder} reminder
+ */
+function getUpcomingReminderTime(reminder) {
+ // this is only the time (hour & minutes); date is not included
+ const time = dayjs(reminder.date);
+ const now = dayjs();
+ const relativeTime = now.clone().hour(time.hour()).minute(time.minute());
+
+ const isPast = relativeTime.isBefore(now);
+
+ const isDay = reminder.recurringMode === "day";
+ const isWeek = reminder.recurringMode === "week";
+ const isMonth = reminder.recurringMode === "month";
+
+ if (isDay) {
+ if (isPast) return relativeTime.add(1, "day").valueOf();
+ else return relativeTime.valueOf();
+ }
+
+ if (!reminder.selectedDays || !reminder.selectedDays.length)
+ return relativeTime.valueOf();
+
+ const sorted = reminder.selectedDays.sort((a, b) => a - b);
+ const lastSelectedDay = sorted[sorted.length - 1];
+
+ if (isWeek) {
+ if (now.day() > lastSelectedDay)
+ return relativeTime.day(sorted[0]).add(1, "week").valueOf();
+ else {
+ for (const day of reminder.selectedDays)
+ if (now.day() < day) return relativeTime.day(day).valueOf();
+ }
+ } else if (isMonth) {
+ if (now.date() > lastSelectedDay)
+ return relativeTime.date(sorted[0]).add(1, "month").valueOf();
+ else {
+ for (const day of reminder.selectedDays)
+ if (now.date() < day) return relativeTime.date(day).valueOf();
+ }
+ }
+
+ return relativeTime.valueOf();
+}