refactor(main): better code splitting
This commit is contained in:
parent
65f8477678
commit
2f6a538151
3 changed files with 33 additions and 39 deletions
|
@ -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
|
||||
|
|
|
@ -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 () => {
|
||||
|
|
29
apps/main/src/utils/getAge.ts
Normal file
29
apps/main/src/utils/getAge.ts
Normal 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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue