Skip to content

Commit

Permalink
refactor: rename DueDateInfo to DueDate
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiebrynes7 committed Nov 24, 2024
1 parent 3cb2a7c commit 31c6c78
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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";

Expand All @@ -13,7 +13,7 @@ vi.mock("../infra/time.ts", () => {

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

Expand All @@ -40,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 Down Expand Up @@ -78,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 Down Expand Up @@ -133,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 Down Expand Up @@ -179,7 +179,7 @@ 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);
});
Expand Down
10 changes: 5 additions & 5 deletions plugin/src/data/dueDateInfo.ts → plugin/src/data/dueDate.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { DueDate } from "@/api/domain/dueDate";
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 DueDateInfo {
export class DueDate {
private inner: ZonedDateTime | CalendarDate;

constructor(dueDate: DueDate) {
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.
Expand Down Expand Up @@ -61,7 +61,7 @@ export class DueDateInfo {
return formatter.format(this.naiveDate());
}

compareDate(other: DueDateInfo): -1 | 0 | 1 {
compareDate(other: DueDate): -1 | 0 | 1 {
const thisDate = this.calendarDate();
const otherDate = other.calendarDate();
const cmp = thisDate.compare(otherDate);
Expand All @@ -77,7 +77,7 @@ export class DueDateInfo {
return 1;
}

compareDateTime(other: DueDateInfo): -1 | 0 | 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");
}
Expand Down
44 changes: 22 additions & 22 deletions plugin/src/data/dueDateFormatter.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DueDate } from "@/data/dueDate";
import { formatAsHeader, formatDueDate } from "@/data/dueDateFormatter";
import { DueDateInfo } from "@/data/dueDateInfo";
import { CalendarDate, ZonedDateTime } from "@internationalized/date";
import { describe, expect, test, vi } from "vitest";

Expand All @@ -20,22 +20,22 @@ vi.mock("../infra/locale.ts", () => {
describe("formatDueDate", () => {
type Testcase = {
description: string;
dueDate: DueDateInfo;
dueDate: DueDate;
expected: string;
};

const testcases: Testcase[] = [
{
description: "Today, no time",
dueDate: new DueDateInfo({
dueDate: new DueDate({
recurring: false,
date: "2024-06-01",
}),
expected: "Today",
},
{
description: "Today, with time",
dueDate: new DueDateInfo({
dueDate: new DueDate({
recurring: false,
date: "2024-06-01",
datetime: "2024-06-01T12:00:00",
Expand All @@ -44,15 +44,15 @@ describe("formatDueDate", () => {
},
{
description: "Tomorrow, no time",
dueDate: new DueDateInfo({
dueDate: new DueDate({
recurring: false,
date: "2024-06-02",
}),
expected: "Tomorrow",
},
{
description: "Tomorrow, with time",
dueDate: new DueDateInfo({
dueDate: new DueDate({
recurring: false,
date: "2024-06-02",
datetime: "2024-06-02T12:00:00",
Expand All @@ -61,15 +61,15 @@ describe("formatDueDate", () => {
},
{
description: "Next week, no time",
dueDate: new DueDateInfo({
dueDate: new DueDate({
recurring: false,
date: "2024-06-05",
}),
expected: "Wednesday",
},
{
description: "Next week, with time",
dueDate: new DueDateInfo({
dueDate: new DueDate({
recurring: false,
date: "2024-06-05",
datetime: "2024-06-05T12:00:00",
Expand All @@ -78,15 +78,15 @@ describe("formatDueDate", () => {
},
{
description: "Future date, no time",
dueDate: new DueDateInfo({
dueDate: new DueDate({
recurring: false,
date: "2024-06-30",
}),
expected: "Jun 30",
},
{
description: "Future date, with time",
dueDate: new DueDateInfo({
dueDate: new DueDate({
recurring: false,
date: "2024-06-30",
datetime: "2024-06-30T12:00:00",
Expand All @@ -95,15 +95,15 @@ describe("formatDueDate", () => {
},
{
description: "Yesterday, no time",
dueDate: new DueDateInfo({
dueDate: new DueDate({
recurring: false,
date: "2024-05-31",
}),
expected: "Yesterday",
},
{
description: "Yesterday, with time",
dueDate: new DueDateInfo({
dueDate: new DueDate({
recurring: false,
date: "2024-05-31",
datetime: "2024-05-31T12:00:00",
Expand All @@ -112,15 +112,15 @@ describe("formatDueDate", () => {
},
{
description: "Last week, no time",
dueDate: new DueDateInfo({
dueDate: new DueDate({
recurring: false,
date: "2024-05-29",
}),
expected: "Last Wednesday",
},
{
description: "Last week, with time",
dueDate: new DueDateInfo({
dueDate: new DueDate({
recurring: false,
date: "2024-05-29",
datetime: "2024-05-29T12:00:00",
Expand All @@ -129,15 +129,15 @@ describe("formatDueDate", () => {
},
{
description: "Next year, no time",
dueDate: new DueDateInfo({
dueDate: new DueDate({
recurring: false,
date: "2025-06-10",
}),
expected: "Jun 10, 2025",
},
{
description: "Next year, with time",
dueDate: new DueDateInfo({
dueDate: new DueDate({
recurring: false,
date: "2025-06-10",
datetime: "2025-06-10T12:00:00",
Expand All @@ -146,15 +146,15 @@ describe("formatDueDate", () => {
},
{
description: "Last year, no time",
dueDate: new DueDateInfo({
dueDate: new DueDate({
recurring: false,
date: "2023-08-15",
}),
expected: "Aug 15, 2023",
},
{
description: "Last year, with time",
dueDate: new DueDateInfo({
dueDate: new DueDate({
recurring: false,
date: "2023-08-15",
datetime: "2023-08-15T12:00:00",
Expand All @@ -174,30 +174,30 @@ describe("formatDueDate", () => {
describe("formatAsHeader", () => {
type Testcase = {
description: string;
dueDate: DueDateInfo;
dueDate: DueDate;
expected: string;
};

const testcases: Testcase[] = [
{
description: "Today",
dueDate: new DueDateInfo({
dueDate: new DueDate({
date: "2024-06-01",
recurring: false,
}),
expected: "Jun 1 ‧ Saturday ‧ Today",
},
{
description: "Tomorrow",
dueDate: new DueDateInfo({
dueDate: new DueDate({
date: "2024-06-02",
recurring: false,
}),
expected: "Jun 2 ‧ Sunday ‧ Tomorrow",
},
{
description: "Other date",
dueDate: new DueDateInfo({
dueDate: new DueDate({
date: "2024-06-03",
recurring: false,
}),
Expand Down
8 changes: 4 additions & 4 deletions plugin/src/data/dueDateFormatter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DueDateInfo } from "@/data/dueDateInfo";
import type { DueDate } from "@/data/dueDate";
import { t } from "@/i18n";
import { locale } from "@/infra/locale";

Expand Down Expand Up @@ -31,7 +31,7 @@ const getFormatter = (style: string): Intl.DateTimeFormat => {
return formatterCache[style];
};

export const formatDueDate = (dueDate: DueDateInfo): string => {
export const formatDueDate = (dueDate: DueDate): string => {
const date = formatDate(dueDate);

if (dueDate.hasTime()) {
Expand All @@ -44,7 +44,7 @@ export const formatDueDate = (dueDate: DueDateInfo): string => {
return date;
};

const formatDate = (dueDate: DueDateInfo): string => {
const formatDate = (dueDate: DueDate): string => {
const i18n = t().dates;

if (dueDate.isToday()) {
Expand Down Expand Up @@ -74,7 +74,7 @@ const formatDate = (dueDate: DueDateInfo): string => {
return dueDate.format(getFormatter("date"));
};

export const formatAsHeader = (dueDate: DueDateInfo): string => {
export const formatAsHeader = (dueDate: DueDate): string => {
const formatParts: string[] = [
dueDate.format(getFormatter("date")),
dueDate.format(getFormatter("weekday")),
Expand Down
6 changes: 3 additions & 3 deletions plugin/src/data/transformations/grouping.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Project } from "@/api/domain/project";
import type { Section } from "@/api/domain/section";
import type { Priority } from "@/api/domain/task";
import { DueDate } from "@/data/dueDate";
import { formatAsHeader } from "@/data/dueDateFormatter";
import { DueDateInfo } from "@/data/dueDateInfo";
import type { Task } from "@/data/task";
import { GroupVariant } from "@/query/query";

Expand Down Expand Up @@ -134,15 +134,15 @@ function groupByDate(tasks: Task[]): GroupedTasks[] {
return "Overdue";
}

return formatAsHeader(new DueDateInfo({ recurring: false, date }));
return formatAsHeader(new DueDate({ recurring: false, date }));
};

const dates = partitionBy(tasks, (task: Task) => {
if (task.due?.date === undefined) {
return undefined;
}

if (new DueDateInfo(task.due).isOverdue()) {
if (new DueDate(task.due).isOverdue()) {
return "Overdue";
}

Expand Down
6 changes: 3 additions & 3 deletions plugin/src/data/transformations/sorting.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Task } from "@/data//task";
import { DueDateInfo } from "@/data/dueDateInfo";
import { DueDate } from "@/data/dueDate";
import { SortingVariant } from "@/query/query";
import { parseAbsoluteToLocal } from "@internationalized/date";

Expand Down Expand Up @@ -62,8 +62,8 @@ function compareTaskDate<T extends Task>(self: T, other: T): number {
return -1;
}

const selfInfo = new DueDateInfo(self.due);
const otherInfo = new DueDateInfo(other.due);
const selfInfo = new DueDate(self.due);
const otherInfo = new DueDate(other.due);

const dateCmp = selfInfo.compareDate(otherInfo);

Expand Down
6 changes: 3 additions & 3 deletions plugin/src/ui/query/task/Task.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DueDateInfo } from "@/data/dueDateInfo";
import { DueDate } from "@/data/dueDate";
import type { TaskTree } from "@/data/transformations/relationships";
import { t } from "@/i18n";
import { ShowMetadataVariant } from "@/query/query";
Expand Down Expand Up @@ -81,7 +81,7 @@ function getDueMetadataInfo(task: TaskTree): string | undefined {
return undefined;
}

const info = new DueDateInfo(task.due);
const info = new DueDate(task.due);

if (info.isOverdue()) {
return "overdue";
Expand All @@ -101,7 +101,7 @@ function getTimeMetadataInfo(task: TaskTree): boolean | undefined {
return undefined;
}

return new DueDateInfo(task.due).hasTime();
return new DueDate(task.due).hasTime();
}

const sanitizeContent = (content: string): string => {
Expand Down
4 changes: 2 additions & 2 deletions plugin/src/ui/query/task/TaskMetadata.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DueDate } from "@/data/dueDate";
import { formatDueDate } from "@/data/dueDateFormatter";
import { DueDateInfo } from "@/data/dueDateInfo";
import type { Task } from "@/data/task";
import { type Query, ShowMetadataVariant } from "@/query/query";
import type { Settings } from "@/settings";
Expand Down Expand Up @@ -78,7 +78,7 @@ const dateLabel = (task: Task): string => {
return "";
}

return formatDueDate(new DueDateInfo(task.due));
return formatDueDate(new DueDate(task.due));
};

type TaskMetadataProps = {
Expand Down

0 comments on commit 31c6c78

Please sign in to comment.