UNPKG

@tempots/std

Version:

Std library for TypeScript. Natural complement to the Tempo libraries.

73 lines (72 loc) 1.9 kB
const s = (t, r) => { if (t > r) throw new Error("min must be less than or equal to max"); return Math.floor(Math.random() * (r - t + 1)) + t; }, c = (t, r, n = !1) => { if (t > r) throw new Error("min must be less than or equal to max"); const e = Math.random(), o = r - t; return e * o + t; }, h = (t) => { if (t.length === 0) throw new Error("Cannot choose from empty array"); const r = s(0, t.length - 1); return t[r]; }, l = (t, r) => { if (t.length === 0) throw new Error("Cannot choose from empty array"); if (r > t.length) throw new Error("Cannot select more elements than available in array"); if (r <= 0) return []; const n = [...t]; for (let e = n.length - 1; e > 0; e--) { const o = s(0, e); [n[e], n[o]] = [n[o], n[e]]; } return n.slice(0, r); }, a = () => "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (t) => { const r = Math.random() * 16 | 0; return (t === "x" ? r : r & 3 | 8).toString(16); }), f = (t) => { if (t <= 0) return ""; const r = "0123456789abcdef"; let n = ""; for (let e = 0; e < t; e++) n += r[s(0, r.length - 1)]; return n; }, u = (t) => { if (t <= 0) return new Uint8Array(0); const r = new Uint8Array(t); for (let n = 0; n < t; n++) r[n] = s(0, 255); return r; }, i = (t) => { let r = 0; for (let e = 0; e < t.length; e++) { const o = t.charCodeAt(e); r = (r << 5) - r + o, r = r & r; } let n = Math.abs(r); return () => (n = (n * 1664525 + 1013904223) % 4294967296, n / 4294967296); }, x = (t) => { for (let r = t.length - 1; r > 0; r--) { const n = s(0, r); [t[r], t[n]] = [t[n], t[r]]; } return t; }, d = (t) => x([...t]); export { u as randomBytes, h as randomChoice, l as randomChoices, c as randomFloat, f as randomHex, s as randomInt, a as randomUuid, i as seedRandom, x as shuffle, d as shuffled };