UNPKG

@tempots/std

Version:

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

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