Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: move off momentjs #356

Merged
merged 4 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions plugin/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"camelize-ts": "^3.0.0",
"classnames": "^2.5.1",
"framer-motion": "^11.1.8",
"moment": "^2.29.4",
"obsidian": "0.15",
"react": "^18.2.0",
"react-aria-components": "^1.1.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,61 +1,24 @@
import type { DueDate } from "@/api/domain/dueDate";
import { DueDateInfo } from "@/data/dueDateInfo";
import type { DueDate as ApiDueDate } from "@/api/domain/dueDate";
import { DueDate } from "@/data/dueDate";
import { CalendarDate, ZonedDateTime } from "@internationalized/date";
import { describe, expect, it, vi } from "vitest";

vi.mock("../now.ts", () => {
vi.mock("../infra/time.ts", () => {
return {
now: () => new Date("2024-01-01T12:00:00"),
today: () => new CalendarDate(2024, 1, 1),
now: () => new ZonedDateTime(2024, 1, 1, "Etc/UTC", 0, 12), // 2024-01-01T12:00:00Z
timezone: () => "Etc/UTC",
};
});

type TestCase = {
description: string;
input: DueDate | undefined;
input: ApiDueDate;
expected: boolean;
};

describe("hasDueDate", () => {
const testcases: TestCase[] = [
{
description: "should be false for empty due date",
input: undefined,
expected: false,
},
{
description: "should be true for just date",
input: {
recurring: false,
date: "2024-01-01",
},
expected: true,
},
{
description: "should be true for datetime",
input: {
recurring: false,
date: "2024-01-01",
datetime: "2024-01-01T12:00:00",
},
expected: true,
},
];

for (const tc of testcases) {
it(tc.description, () => {
const info = new DueDateInfo(tc.input);
const hasDueDate = info.hasDueDate();
expect(hasDueDate).toEqual(tc.expected);
});
}
});

describe("hasTime", () => {
const testcases: TestCase[] = [
{
description: "should be false for empty due date",
input: undefined,
expected: false,
},
{
description: "should be false for just date",
input: {
Expand All @@ -77,7 +40,7 @@ describe("hasTime", () => {

for (const tc of testcases) {
it(tc.description, () => {
const info = new DueDateInfo(tc.input);
const info = new DueDate(tc.input);
const hasTime = info.hasTime();
expect(hasTime).toEqual(tc.expected);
});
Expand All @@ -86,11 +49,6 @@ describe("hasTime", () => {

describe("isToday", () => {
const testcases: TestCase[] = [
{
description: "should be false for empty due date",
input: undefined,
expected: false,
},
{
description: "should be false for different day",
input: {
Expand Down Expand Up @@ -120,7 +78,7 @@ describe("isToday", () => {

for (const tc of testcases) {
it(tc.description, () => {
const info = new DueDateInfo(tc.input);
const info = new DueDate(tc.input);
const isToday = info.isToday();
expect(isToday).toEqual(tc.expected);
});
Expand All @@ -129,11 +87,6 @@ describe("isToday", () => {

describe("isOverdue", () => {
const testcases: TestCase[] = [
{
description: "should be false for empty due date",
input: undefined,
expected: false,
},
{
description: "should be false if date in future",
input: {
Expand Down Expand Up @@ -180,7 +133,7 @@ describe("isOverdue", () => {

for (const tc of testcases) {
it(tc.description, () => {
const info = new DueDateInfo(tc.input);
const info = new DueDate(tc.input);
const isOverdue = info.isOverdue();
expect(isOverdue).toEqual(tc.expected);
});
Expand All @@ -189,11 +142,6 @@ describe("isOverdue", () => {

describe("isTomorrow", () => {
const testcases: TestCase[] = [
{
description: "should be false for empty due date",
input: undefined,
expected: false,
},
{
description: "should be false for same day",
input: {
Expand Down Expand Up @@ -231,9 +179,11 @@ describe("isTomorrow", () => {

for (const tc of testcases) {
it(tc.description, () => {
const info = new DueDateInfo(tc.input);
const info = new DueDate(tc.input);
const isTomorrow = info.isTomorrow();
expect(isTomorrow).toEqual(tc.expected);
});
}
});

// TODO: Write tests for other is* methods.
112 changes: 112 additions & 0 deletions plugin/src/data/dueDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import type { DueDate as ApiDueDate } from "@/api/domain/dueDate";
import { now, timezone, today } from "@/infra/time";
import { CalendarDate, ZonedDateTime, parseAbsolute, parseDate } from "@internationalized/date";

export class DueDate {
private inner: ZonedDateTime | CalendarDate;

constructor(dueDate: ApiDueDate) {
if (dueDate.datetime !== undefined) {
// Todoist's datetime comes as a UTC timezone, but without the trailing 'Z'.
// So we just patch it in and carry on our merry way.
this.inner = parseAbsolute(`${dueDate.datetime}Z`, timezone());
} else {
this.inner = parseDate(dueDate.date);
}
}

hasTime(): boolean {
return this.inner instanceof ZonedDateTime;
}

isToday(): boolean {
const date = this.calendarDate();
return date.compare(today()) === 0;
}

isOverdue(): boolean {
if (this.inner instanceof CalendarDate) {
return this.inner.compare(today()) < 0;
}

return this.inner.compare(now()) < 0;
}

isTomorrow(): boolean {
const date = this.calendarDate();
return date.compare(today().add({ days: 1 })) === 0;
}

isYesterday(): boolean {
const date = this.calendarDate();
return date.compare(today().add({ days: -1 })) === 0;
}

isInLastWeek(): boolean {
const date = this.calendarDate();
return date.compare(today().add({ days: -7 })) >= 0 && date.compare(today()) < 0;
}

isInNextWeek(): boolean {
const date = this.calendarDate();
return date.compare(today().add({ days: 7 })) <= 0 && date.compare(today()) > 0;
}

isCurrentYear(): boolean {
const date = this.calendarDate();
return date.year === today().year;
}

format(formatter: Intl.DateTimeFormat): string {
return formatter.format(this.naiveDate());
}

compareDate(other: DueDate): -1 | 0 | 1 {
const thisDate = this.calendarDate();
const otherDate = other.calendarDate();
const cmp = thisDate.compare(otherDate);

if (cmp === 0) {
return 0;
}

if (cmp < 0) {
return -1;
}

return 1;
}

compareDateTime(other: DueDate): -1 | 0 | 1 {
if (!(this.inner instanceof ZonedDateTime) || !(other.inner instanceof ZonedDateTime)) {
throw new Error("Called compareDateTime on due dates without time");
}

const cmp = this.inner.compare(other.inner);
if (cmp === 0) {
return 0;
}

if (cmp < 0) {
return -1;
}

return 1;
}

private naiveDate(): Date {
if (this.inner instanceof CalendarDate) {
return this.inner.toDate(timezone());
}

return this.inner.toDate();
}

private calendarDate(): CalendarDate {
if (this.inner instanceof CalendarDate) {
return this.inner;
}

return new CalendarDate(this.inner.year, this.inner.month, this.inner.day);
}
}
Loading