export const nextFrame = async (): Promise => new Promise((resolve) => requestAnimationFrame(resolve)); export const randInt = (min: number, max: number) => Math.round(min + (max - min - 1) * Math.random()); export const randBool = () => Math.random() < 0.5; export const choice = (array: T[]): T => array[randInt(0, array.length)]; export const weightedChoice = (options: [T, number][] | Partial>): T | null => { if (!Array.isArray(options)) { options = Object.entries(options) as [T, number][]; } const sum = options.reduce((acc, o) => acc + o[1], 0); const rnd = Math.random() * sum; let weight = 0; for (const [item, probability] of options) { weight += probability; if (rnd < weight) { return item; } } return null; } export const shuffle = (array: T[]): T[] => { const shuffledArray = [...array]; for (let i = shuffledArray.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [shuffledArray[i], shuffledArray[j]] = [shuffledArray[j], shuffledArray[i]]; } return shuffledArray; } export function zip(a1: Iterable, a2: Iterable, a3: Iterable): Generator<[T1, T2, T3]>; export function zip(a1: Iterable, a2: Iterable): Generator<[T1, T2]>; export function zip(a1: Iterable): Generator<[T1]>; export function* zip(...args: Iterable[]) { const iterators = args.map(i => i[Symbol.iterator]()); while (true) { const nextValues = iterators.map(i => i.next()); if (nextValues.some(v => v.done)) return; yield nextValues.map(v => v.value); } } export const range = (size: number | string) => Object.keys((new Array(+size)).fill(0)).map(k => +k); export const clamp = (value: number, min: number, max: number) => Math.min(max, Math.max(min, value)); export const prevent = (e: Event) => (e.preventDefault(), false); export const intHash = (seed: number, ...parts: number[]) => { let h1 = 0xdeadbeef ^ seed, h2 = 0x41c6ce57 ^ seed; for (let i = 0; i < parts.length; i++) { const ch = parts[i]; h1 = Math.imul(h1 ^ ch, 2654435761); h2 = Math.imul(h2 ^ ch, 1597334677); } h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507); h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909); return h1; }; export const sinHash = (...data: number[]) => data.reduce((hash, n) => Math.sin((hash * 123.12 + n) * 756.12), 0) / 2 + 0.5; export const throttle = function R>(func: F, ms: number, trailing = false): F { let isThrottled = false; let savedResult: R; let savedThis: T; let savedArgs: A | undefined; const wrapper: F = function (...args: A) { if (isThrottled) { savedThis = this; savedArgs = args; } else { savedResult = func.apply(this, args); savedArgs = undefined; isThrottled = true; setTimeout(function () { isThrottled = false; if (trailing && savedArgs) { savedResult = wrapper.apply(savedThis, savedArgs); } }, ms); } return savedResult; } as F; return wrapper; }