File size: 653 Bytes
3c124f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;
}