From 2f6a5381516b325c363a513541478d8370e83874 Mon Sep 17 00:00:00 2001 From: developomp Date: Tue, 4 Jul 2023 11:43:35 +0900 Subject: [PATCH] refactor(main): better code splitting --- apps/main/src/constants.ts | 5 ---- apps/main/src/routes/+page.svelte | 38 ++++--------------------------- apps/main/src/utils/getAge.ts | 29 +++++++++++++++++++++++ 3 files changed, 33 insertions(+), 39 deletions(-) create mode 100644 apps/main/src/utils/getAge.ts diff --git a/apps/main/src/constants.ts b/apps/main/src/constants.ts index 09732f8..d1ca4f9 100644 --- a/apps/main/src/constants.ts +++ b/apps/main/src/constants.ts @@ -1,6 +1 @@ export const discordInviteLink = "https://discord.gg/aQqamSCUcS" - -// my birthday :D -export const birthYear = 2002 -export const birthMonth = 7 -export const birthDate = 30 diff --git a/apps/main/src/routes/+page.svelte b/apps/main/src/routes/+page.svelte index d9ae1c1..984aad5 100644 --- a/apps/main/src/routes/+page.svelte +++ b/apps/main/src/routes/+page.svelte @@ -8,44 +8,14 @@ import { onMount } from "svelte" import HandWave from "../components/HandWave.svelte" - import { - birthDate, - birthMonth, - birthYear, - discordInviteLink, - } from "../constants" + import { discordInviteLink } from "../constants" + import getAge from "../utils/getAge" - let age = 0 + let age = getAge() // run immediately the first time - function updateAge() { - const now = Date.now() - const date = new Date() - const year = date.getFullYear() - - // integer calculation - - const isOverBirthDay = - birthMonth > date.getMonth() + 1 || - (birthMonth === date.getMonth() + 1 && birthDate >= date.getDate()) - const ageInt = year - birthYear - (isOverBirthDay ? 1 : 0) - - // decimal calculation - - const msThisYear = Date.UTC(year, 0, 0) - const msThisBD = Date.UTC(year, birthMonth - 1, birthDate) - // number of milliseconds since the beginning of this year - const msSinceThisYear = now - msThisYear - // number of milliseconds from the beginning of this year to my birthday - const msToBD = msThisBD - msThisYear - const ageDecimal = msSinceThisYear / msToBD - - age = ageInt + ageDecimal - } - - updateAge() // run immediately the first time onMount(() => { const interval = setInterval(() => { - updateAge() // first called after the delay + age = getAge() // first called after the delay }, 50) return () => { diff --git a/apps/main/src/utils/getAge.ts b/apps/main/src/utils/getAge.ts new file mode 100644 index 0000000..8c45ff3 --- /dev/null +++ b/apps/main/src/utils/getAge.ts @@ -0,0 +1,29 @@ +// my birthday :D +const birthYear = 2002 +const birthMonth = 7 +const birthDate = 30 + +export default function getAge(): number { + const now = Date.now() + const date = new Date() + const year = date.getFullYear() + + // integer calculation + + const isOverBirthDay = + birthMonth > date.getMonth() + 1 || + (birthMonth === date.getMonth() + 1 && birthDate >= date.getDate()) + const ageInt = year - birthYear - (isOverBirthDay ? 1 : 0) + + // decimal calculation + + const msThisYear = Date.UTC(year, 0, 0) + const msThisBD = Date.UTC(year, birthMonth - 1, birthDate) + // number of milliseconds since the beginning of this year + const msSinceThisYear = now - msThisYear + // number of milliseconds from the beginning of this year to my birthday + const msToBD = msThisBD - msThisYear + const ageDecimal = msSinceThisYear / msToBD + + return ageInt + ageDecimal +}