refactor: use dayjs

This commit is contained in:
Kim, Jimin 2023-07-31 00:37:34 +09:00
parent 9fc1872667
commit bd93b25dd2
Signed by: pomp
GPG key ID: CE1DDB8A4A765403
4 changed files with 49 additions and 44 deletions

View file

@ -28,6 +28,7 @@
"@typescript-eslint/eslint-plugin": "^5.61.0", "@typescript-eslint/eslint-plugin": "^5.61.0",
"@typescript-eslint/parser": "^5.61.0", "@typescript-eslint/parser": "^5.61.0",
"autoprefixer": "^10.4.14", "autoprefixer": "^10.4.14",
"dayjs": "^1.11.9",
"eslint": "^8.44.0", "eslint": "^8.44.0",
"eslint-config-prettier": "^8.8.0", "eslint-config-prettier": "^8.8.0",
"eslint-plugin-import": "^2.27.5", "eslint-plugin-import": "^2.27.5",

View file

@ -1,10 +1,12 @@
import dayjs from "dayjs"
import { testing } from "./getAge" import { testing } from "./getAge"
const { birth, getAge, ageInt, ageDecimal, isOverBirthDay } = testing const { birth, getAge, ageInt, ageDecimal, isOverBirthDay } = testing
describe("getAge tests", () => { describe("getAge tests", () => {
test("birthday to be 2002-07-30", () => { 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<{ test.each<{
@ -15,91 +17,87 @@ describe("getAge tests", () => {
date: number date: number
}>([ }>([
{ {
timestamp: "2022-12-31, 00:00:00.000 +09:00", timestamp: "2022-12-31 00:00:00.000+09:00",
overBD: true, overBD: true,
year: 2022, year: 2022,
monthIndex: 11, monthIndex: 11,
date: 31, date: 31,
}, },
{ {
timestamp: "2023-01-01, 00:00:00.000 +09:00", timestamp: "2023-01-01 00:00:00.000+09:00",
overBD: false, overBD: false,
year: 2023, year: 2023,
monthIndex: 0, monthIndex: 0,
date: 1, date: 1,
}, },
{ {
timestamp: "2023-07-29, 00:00:00.000 +09:00", timestamp: "2023-07-29 00:00:00.000+09:00",
overBD: false, overBD: false,
year: 2023, year: 2023,
monthIndex: 6, monthIndex: 6,
date: 29, date: 29,
}, },
{ {
timestamp: "2023-07-30, 00:00:00.000 +09:00", timestamp: "2023-07-30 00:00:00.000+09:00",
overBD: true, overBD: true,
year: 2023, year: 2023,
monthIndex: 6, monthIndex: 6,
date: 30, date: 30,
}, },
{ {
timestamp: "2023-07-31, 00:00:00.000 +09:00", timestamp: "2023-07-31 00:00:00.000+09:00",
overBD: true, overBD: true,
year: 2023, year: 2023,
monthIndex: 6, monthIndex: 6,
date: 31, date: 31,
}, },
{ {
timestamp: "2023-12-31, 00:00:00.000 +09:00", timestamp: "2023-12-31 00:00:00.000+09:00",
overBD: true, overBD: true,
year: 2023, year: 2023,
monthIndex: 11, monthIndex: 11,
date: 31, date: 31,
}, },
{ {
timestamp: "2024-01-01, 00:00:00.000 +09:00", timestamp: "2024-01-01 00:00:00.000+09:00",
overBD: false, overBD: false,
year: 2024, year: 2024,
monthIndex: 0, monthIndex: 0,
date: 1, date: 1,
}, },
])("isOverBirthDay to work ($timestamp)", (testData) => { ])("isOverBirthDay to work ($timestamp)", (testData) => {
const date = new Date(testData.timestamp) const date = dayjs(testData.timestamp)
expect(date.getFullYear()).toEqual(testData.year) expect(date.year()).toEqual(testData.year)
expect(date.getMonth()).toEqual(testData.monthIndex) expect(date.month()).toEqual(testData.monthIndex)
expect(date.getDate()).toEqual(testData.date) expect(date.date()).toEqual(testData.date)
expect(isOverBirthDay(date)).toEqual(testData.overBD) expect(isOverBirthDay(date)).toEqual(testData.overBD)
}) })
test("ageInt to work", () => { test("ageInt to work", () => {
expect(ageInt(birth)).toEqual(0) expect(ageInt(birth)).toEqual(0)
expect(ageInt(new Date("2023-07-29"))).toEqual(20) expect(ageInt(dayjs("2023-07-29"))).toEqual(20)
expect(ageInt(new Date("2023-07-30"))).toEqual(21) expect(ageInt(dayjs("2023-07-30"))).toEqual(21)
expect(ageInt(new Date("2023-07-31"))).toEqual(21) expect(ageInt(dayjs("2023-07-31"))).toEqual(21)
}) })
test("ageDecimal to work", () => { 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 0.9972602739726028
) )
expect(ageDecimal(new Date("2023-07-30, 00:00:00.000 +09:00"))).toEqual( expect(ageDecimal(dayjs("2023-07-30 00:00:00.000+09:00"))).toEqual(0)
0 expect(ageDecimal(dayjs("2023-07-31 00:00:00.000+09:00"))).toEqual(
)
expect(ageDecimal(new Date("2023-07-31, 00:00:00.000 +09:00"))).toEqual(
0.00273224043715847 0.00273224043715847
) )
}) })
test("getAge to work", () => { test("getAge to work", () => {
expect(getAge(new Date("2002-07-30, 00:00:00.000 +09:00"))).toEqual(0.0) expect(getAge(dayjs("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("2023-07-29 00:00:00.000+09:00"))).toEqual(
20.997260273972604 20.997260273972604
) )
expect(getAge(new Date("2023-07-30, 00:00:00.000 +09:00"))).toEqual( expect(getAge(dayjs("2023-07-30 00:00:00.000+09:00"))).toEqual(21.0)
21.0 expect(getAge(dayjs("2023-07-31 00:00:00.000+09:00"))).toEqual(
)
expect(getAge(new Date("2023-07-31, 00:00:00.000 +09:00"))).toEqual(
21.002732240437158 21.002732240437158
) )
}) })

View file

@ -1,40 +1,40 @@
import dayjs, { type Dayjs } from "dayjs"
// my birthday in KST :D // 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 * Gets developomp's age with decimal precision
* *
* @param now - current `Date` in KST * @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) return ageInt(now) + ageDecimal(now)
} }
/** /**
* Calculates the integer component of the age * Calculates the integer component of the age
*/ */
function ageInt(now: Date): number { function ageInt(now: Dayjs): number {
return ( return now.year() - birth.year() - (isOverBirthDay(now) ? 0 : 1)
now.getFullYear() - birth.getFullYear() - (isOverBirthDay(now) ? 0 : 1)
)
} }
/** /**
* Calculates the decimal component of the age * Calculates the decimal component of the age
*/ */
function ageDecimal(now: Date): number { function ageDecimal(now: Dayjs): number {
// millisecond timestamp of my last birthday // millisecond timestamp of my last birthday
const BDPrev = new Date(birth).setFullYear( const BDPrev = birth
now.getFullYear() - (isOverBirthDay(now) ? 0 : 1) .year(now.year() - (isOverBirthDay(now) ? 0 : 1))
) .valueOf()
// millisecond timestamp of my upcoming birthday // millisecond timestamp of my upcoming birthday
const BDNext = new Date(birth).setFullYear( const BDNext = birth
now.getFullYear() + (isOverBirthDay(now) ? 1 : 0) .year(now.year() + (isOverBirthDay(now) ? 1 : 0))
) .valueOf()
// milliseconds since my last birthday // milliseconds since my last birthday
const msSinceLastBD = now.getTime() - BDPrev const msSinceLastBD = now.valueOf() - BDPrev
return msSinceLastBD / (BDNext - 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 | ... * ...| Dec 31 | Jan 1 | ... | July 29 | Jul 30 | July 31 | ... | Dec 31 | Jan 1 | ...
* ...| true | false | ... | false | true | true | ... | true | false | ... * ...| true | false | ... | false | true | true | ... | true | false | ...
*/ */
function isOverBirthDay(now: Date): boolean { function isOverBirthDay(now: Dayjs): boolean {
if (birth.getMonth() < now.getMonth()) return true if (birth.month() < now.month()) return true
if (birth.getMonth() === now.getMonth() && birth.getDate() <= now.getDate()) if (birth.month() === now.month() && birth.date() <= now.date()) return true
return true
return false return false
} }

7
pnpm-lock.yaml generated
View file

@ -188,6 +188,9 @@ importers:
autoprefixer: autoprefixer:
specifier: ^10.4.14 specifier: ^10.4.14
version: 10.4.14(postcss@8.4.25) version: 10.4.14(postcss@8.4.25)
dayjs:
specifier: ^1.11.9
version: 1.11.9
eslint: eslint:
specifier: ^8.44.0 specifier: ^8.44.0
version: 8.44.0 version: 8.44.0
@ -3242,6 +3245,10 @@ packages:
whatwg-url: 12.0.1 whatwg-url: 12.0.1
dev: false dev: false
/dayjs@1.11.9:
resolution: {integrity: sha512-QvzAURSbQ0pKdIye2txOzNaHmxtUBXerpY0FJsFXUMKbIZeFm5ht1LS/jFsrncjnmtv8HsG0W2g6c0zUjZWmpA==}
dev: true
/debug@2.6.9: /debug@2.6.9:
resolution: {integrity: sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==} resolution: {integrity: sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==}
peerDependencies: peerDependencies: