diff --git a/apps/main/package.json b/apps/main/package.json index e8bebfe..3622986 100644 --- a/apps/main/package.json +++ b/apps/main/package.json @@ -28,6 +28,7 @@ "@typescript-eslint/eslint-plugin": "^5.61.0", "@typescript-eslint/parser": "^5.61.0", "autoprefixer": "^10.4.14", + "dayjs": "^1.11.9", "eslint": "^8.44.0", "eslint-config-prettier": "^8.8.0", "eslint-plugin-import": "^2.27.5", diff --git a/apps/main/src/utils/getAge.test.ts b/apps/main/src/utils/getAge.test.ts index 9b98459..26569f1 100644 --- a/apps/main/src/utils/getAge.test.ts +++ b/apps/main/src/utils/getAge.test.ts @@ -1,10 +1,12 @@ +import dayjs from "dayjs" + import { testing } from "./getAge" const { birth, getAge, ageInt, ageDecimal, isOverBirthDay } = testing describe("getAge tests", () => { test("birthday to be 2002-07-30", () => { - expect(birth).toEqual(new Date("2002-07-30, 00:00:00.000 +09:00")) + expect(birth).toEqual(dayjs("2002-07-30 00:00:00.000+09:00")) }) test.each<{ @@ -15,91 +17,87 @@ describe("getAge tests", () => { date: number }>([ { - timestamp: "2022-12-31, 00:00:00.000 +09:00", + timestamp: "2022-12-31 00:00:00.000+09:00", overBD: true, year: 2022, monthIndex: 11, date: 31, }, { - timestamp: "2023-01-01, 00:00:00.000 +09:00", + timestamp: "2023-01-01 00:00:00.000+09:00", overBD: false, year: 2023, monthIndex: 0, date: 1, }, { - timestamp: "2023-07-29, 00:00:00.000 +09:00", + timestamp: "2023-07-29 00:00:00.000+09:00", overBD: false, year: 2023, monthIndex: 6, date: 29, }, { - timestamp: "2023-07-30, 00:00:00.000 +09:00", + timestamp: "2023-07-30 00:00:00.000+09:00", overBD: true, year: 2023, monthIndex: 6, date: 30, }, { - timestamp: "2023-07-31, 00:00:00.000 +09:00", + timestamp: "2023-07-31 00:00:00.000+09:00", overBD: true, year: 2023, monthIndex: 6, date: 31, }, { - timestamp: "2023-12-31, 00:00:00.000 +09:00", + timestamp: "2023-12-31 00:00:00.000+09:00", overBD: true, year: 2023, monthIndex: 11, date: 31, }, { - timestamp: "2024-01-01, 00:00:00.000 +09:00", + timestamp: "2024-01-01 00:00:00.000+09:00", overBD: false, year: 2024, monthIndex: 0, date: 1, }, ])("isOverBirthDay to work ($timestamp)", (testData) => { - const date = new Date(testData.timestamp) + const date = dayjs(testData.timestamp) - expect(date.getFullYear()).toEqual(testData.year) - expect(date.getMonth()).toEqual(testData.monthIndex) - expect(date.getDate()).toEqual(testData.date) + expect(date.year()).toEqual(testData.year) + expect(date.month()).toEqual(testData.monthIndex) + expect(date.date()).toEqual(testData.date) expect(isOverBirthDay(date)).toEqual(testData.overBD) }) test("ageInt to work", () => { expect(ageInt(birth)).toEqual(0) - expect(ageInt(new Date("2023-07-29"))).toEqual(20) - expect(ageInt(new Date("2023-07-30"))).toEqual(21) - expect(ageInt(new Date("2023-07-31"))).toEqual(21) + expect(ageInt(dayjs("2023-07-29"))).toEqual(20) + expect(ageInt(dayjs("2023-07-30"))).toEqual(21) + expect(ageInt(dayjs("2023-07-31"))).toEqual(21) }) test("ageDecimal to work", () => { - expect(ageDecimal(new Date("2023-07-29, 00:00:00.000 +09:00"))).toEqual( + expect(ageDecimal(dayjs("2023-07-29 00:00:00.000+09:00"))).toEqual( 0.9972602739726028 ) - expect(ageDecimal(new Date("2023-07-30, 00:00:00.000 +09:00"))).toEqual( - 0 - ) - expect(ageDecimal(new Date("2023-07-31, 00:00:00.000 +09:00"))).toEqual( + expect(ageDecimal(dayjs("2023-07-30 00:00:00.000+09:00"))).toEqual(0) + expect(ageDecimal(dayjs("2023-07-31 00:00:00.000+09:00"))).toEqual( 0.00273224043715847 ) }) test("getAge to work", () => { - expect(getAge(new Date("2002-07-30, 00:00:00.000 +09:00"))).toEqual(0.0) - expect(getAge(new Date("2023-07-29, 00:00:00.000 +09:00"))).toEqual( + expect(getAge(dayjs("2002-07-30 00:00:00.000+09:00"))).toEqual(0.0) + expect(getAge(dayjs("2023-07-29 00:00:00.000+09:00"))).toEqual( 20.997260273972604 ) - expect(getAge(new Date("2023-07-30, 00:00:00.000 +09:00"))).toEqual( - 21.0 - ) - expect(getAge(new Date("2023-07-31, 00:00:00.000 +09:00"))).toEqual( + expect(getAge(dayjs("2023-07-30 00:00:00.000+09:00"))).toEqual(21.0) + expect(getAge(dayjs("2023-07-31 00:00:00.000+09:00"))).toEqual( 21.002732240437158 ) }) diff --git a/apps/main/src/utils/getAge.ts b/apps/main/src/utils/getAge.ts index 9ddebc6..b2cdbad 100644 --- a/apps/main/src/utils/getAge.ts +++ b/apps/main/src/utils/getAge.ts @@ -1,40 +1,40 @@ +import dayjs, { type Dayjs } from "dayjs" + // my birthday in KST :D -const birth = new Date("2002-07-30, 00:00:00.000 +09:00") +const birth = dayjs("2002-07-30 00:00:00.000+09:00") /** * Gets developomp's age with decimal precision * * @param now - current `Date` in KST */ -export default function getAge(now: Date = new Date()): number { +export default function getAge(now: Dayjs = dayjs()): number { return ageInt(now) + ageDecimal(now) } /** * Calculates the integer component of the age */ -function ageInt(now: Date): number { - return ( - now.getFullYear() - birth.getFullYear() - (isOverBirthDay(now) ? 0 : 1) - ) +function ageInt(now: Dayjs): number { + return now.year() - birth.year() - (isOverBirthDay(now) ? 0 : 1) } /** * Calculates the decimal component of the age */ -function ageDecimal(now: Date): number { +function ageDecimal(now: Dayjs): number { // millisecond timestamp of my last birthday - const BDPrev = new Date(birth).setFullYear( - now.getFullYear() - (isOverBirthDay(now) ? 0 : 1) - ) + const BDPrev = birth + .year(now.year() - (isOverBirthDay(now) ? 0 : 1)) + .valueOf() // millisecond timestamp of my upcoming birthday - const BDNext = new Date(birth).setFullYear( - now.getFullYear() + (isOverBirthDay(now) ? 1 : 0) - ) + const BDNext = birth + .year(now.year() + (isOverBirthDay(now) ? 1 : 0)) + .valueOf() // milliseconds since my last birthday - const msSinceLastBD = now.getTime() - BDPrev + const msSinceLastBD = now.valueOf() - BDPrev return msSinceLastBD / (BDNext - BDPrev) } @@ -45,11 +45,10 @@ function ageDecimal(now: Date): number { * ...| Dec 31 | Jan 1 | ... | July 29 | Jul 30 | July 31 | ... | Dec 31 | Jan 1 | ... * ...| true | false | ... | false | true | true | ... | true | false | ... */ -function isOverBirthDay(now: Date): boolean { - if (birth.getMonth() < now.getMonth()) return true +function isOverBirthDay(now: Dayjs): boolean { + if (birth.month() < now.month()) return true - if (birth.getMonth() === now.getMonth() && birth.getDate() <= now.getDate()) - return true + if (birth.month() === now.month() && birth.date() <= now.date()) return true return false } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 567e373..8ed1ff8 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -188,6 +188,9 @@ importers: autoprefixer: specifier: ^10.4.14 version: 10.4.14(postcss@8.4.25) + dayjs: + specifier: ^1.11.9 + version: 1.11.9 eslint: specifier: ^8.44.0 version: 8.44.0 @@ -3242,6 +3245,10 @@ packages: whatwg-url: 12.0.1 dev: false + /dayjs@1.11.9: + resolution: {integrity: sha512-QvzAURSbQ0pKdIye2txOzNaHmxtUBXerpY0FJsFXUMKbIZeFm5ht1LS/jFsrncjnmtv8HsG0W2g6c0zUjZWmpA==} + dev: true + /debug@2.6.9: resolution: {integrity: sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==} peerDependencies: