puck / frontend /src /engine /util.ts
vu1n's picture
Puck — desktop fairy familiar (HF Build Small)
3c124f3
Raw
History Blame Contribute Delete
653 Bytes
import type { Rng } from "./types";
export const clamp = (n: number, lo = 0, hi = 100): number => Math.max(lo, Math.min(hi, n));
export const clamp01 = (n: number): number => Math.max(0, Math.min(1, n));
export const pick = <T>(a: readonly T[], rng: Rng = Math.random): T => a[Math.floor(rng() * a.length)];
export const hhmm = (d: Date = new Date()): string =>
d.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit", hour12: false });
export function shuffle<T>(a: T[], rng: Rng = Math.random): T[] {
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(rng() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}