Skip to content
This repository has been archived by the owner on Jun 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #44 from ruchernchong/43-refactor-moment-to-date-fns
Browse files Browse the repository at this point in the history
Refactor moment to date-fns
  • Loading branch information
ruchernchong committed Nov 30, 2023
2 parents 7561fd5 + afb23c2 commit bc42e8c
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 26 deletions.
Binary file modified bun.lockb
Binary file not shown.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
"dependencies": {
"@heroicons/react": "^2.0.16",
"classnames": "^2.3.2",
"moment": "^2.29.4",
"moment-timezone": "^0.5.43",
"date-fns": "^2.30.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"recharts": "^2.10.1"
Expand Down
4 changes: 0 additions & 4 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import moment from "moment-timezone";
import type { FAQ } from "../types";

const defaultTimezone = "Asia/Singapore";
moment.tz.setDefault(defaultTimezone);

export const CPF_INCOME_CEILING_BEFORE_SEPT_2023: number = 6000;
export const CPF_INCOME_CEILING: Record<number | string, number> = {
"01-01-2023": 6000,
Expand Down
8 changes: 4 additions & 4 deletions src/lib/convertBirthDateToAge.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import moment from "moment";
import { differenceInYears, parse } from "date-fns";

export const convertBirthDateToAge = (birthDate: string) => {
const parsedDate = moment(birthDate, "MM/YYYY");
const currentDate = moment();
const parsedDate = parse(birthDate, "MM/yyyy", new Date());
const currentDate = new Date();

return currentDate.diff(parsedDate, "years");
return differenceInYears(currentDate, parsedDate);
};
20 changes: 10 additions & 10 deletions src/lib/findLatestIncomeCeilingDate.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import moment from "moment";
import { closestTo, max, parse } from "date-fns";
import { formatDate } from "./format";
import { CPFIncomeCeiling } from "../types";

export const findLatestIncomeCeilingDate = (
dates: CPFIncomeCeiling[]
): string => {
const currentDate = moment();
const currentDate = new Date();

const pastDates = dates
.map(({ effectiveDate, ceiling }) => ({
effectiveDate: moment(effectiveDate, "MM-DD-YYYY"),
effectiveDate: parse(effectiveDate, "MM-dd-yyyy", new Date()),
ceiling,
}))
.filter(({ effectiveDate }) => effectiveDate.isBefore(currentDate));
.filter(({ effectiveDate }) => effectiveDate < currentDate);

const closestPastDate = moment.max(
const closestPastDate = closestTo(
currentDate,
pastDates.map(({ effectiveDate }) => effectiveDate)
);

if (closestPastDate) {
return formatDate(closestPastDate, "MM-DD-YYYY");
return formatDate(closestPastDate, "MM-dd-yyyy");
}

return formatDate(
pastDates.reverse().map(({ effectiveDate }) => effectiveDate)[0],
"MM-DD-YYYY"
);
const latestDate = max(pastDates.map(({ effectiveDate }) => effectiveDate));

return formatDate(latestDate, "MM-dd-yyyy");
};
8 changes: 6 additions & 2 deletions src/lib/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ describe("formatDate", () => {
expect(formatDate("09-01-2023")).toBe("01 September 2023");
});

it("should return a nicely formatted date", () => {
it("should return the date formatted to the expected output", () => {
expect(formatDate("09-01-2023")).toBe("01 September 2023");
expect(formatDate("01-01-2024", "DD-MM-YYYY")).toBe("01-01-2024");
expect(formatDate("01-01-2024", "dd-MM-yyyy")).toBe("01-01-2024");
});

it("should return the date formatted to the expected output for a instanceof Date provided", () => {
expect(formatDate(new Date("01-01-2023"))).toBe("01 January 2023");
});
});

Expand Down
14 changes: 10 additions & 4 deletions src/lib/format.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import moment, { Moment } from "moment";
import { format, parse, toDate } from "date-fns";

type PercentageFormatOptions = {
decimalPlaces?: number;
Expand All @@ -16,9 +16,15 @@ export const formatCurrency = (value: number | string): string => {
};

export const formatDate = (
date: Moment | string,
format: string = "DD MMMM YYYY"
) => moment(date, "MM-DD-YYYY").format(format);
date: Date | string,
dateFormat: string = "dd MMMM yyyy"
) => {
if (date instanceof Date) {
return format(date, dateFormat);
}

return format(parse(date, "MM-dd-yyyy", new Date()), dateFormat);
};

export const formatPercentage = (
value: number | string,
Expand Down

0 comments on commit bc42e8c

Please sign in to comment.