refactor(main): better code splitting

This commit is contained in:
Kim, Jimin 2023-07-04 11:43:35 +09:00
parent 65f8477678
commit 2f6a538151
3 changed files with 33 additions and 39 deletions

View file

@ -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

View file

@ -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 () => {

View file

@ -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
}