life.ts
955 B · typescript · 29 lines
1import { LIVE_DAYS } from "../config/shop.ts";23const DAY = 24 * 60 * 60 * 1000;45const LUNAR = LIVE_DAYS * DAY;67const expiry = (born: string | number) => new Date(born).getTime() + LUNAR;89export const left = (born: string | number, now = Date.now()) => Math.max(0, expiry(born) - now);1011export const juice = (born: string | number, now = Date.now()) => Math.min(100, (100 * left(born, now)) / LUNAR);1213const pad = (n: number) => String(n).padStart(2, "0");1415function countdown(ms: number) {16 const s = Math.floor(ms / 1000);17 const d = Math.floor(s / 86400);18 const h = Math.floor((s % 86400) / 3600);19 const m = Math.floor((s % 3600) / 60);20 if (s <= 0) return "Gone";21 if (s < 3600) return `${m}m ${pad(s % 60)}s`;22 if (d) return `${d}d ${pad(h)}h ${pad(m)}m`;23 return `${h}h ${pad(m)}m`;24}2526export function label(born: string | number, now = Date.now()) {27 const ms = left(born, now);28 return ms <= 0 ? "Gone" : `${countdown(ms)} left`;29}