@ssts/id
Version:
Create unique random string IDs
2 lines (1 loc) • 1.01 kB
JavaScript
import f from"@ssts/int";var i=new Set,l=(e,n)=>{let t="",s=e.length-1;for(let o=0;o<n;o++)t+=e.charAt(f(0,s));return t},w=(e=[8,4,4,4,12],n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")=>{if(Array.isArray(e)){if(e.length===0)throw new RangeError("lengthArray must not be empty");if(e.some(r=>typeof r!="number"))throw new TypeError("All lengthArray items must be numbers");if(e.some(r=>r<1))throw new RangeError("All lengthArray numbers must be greater than 0")}else throw new TypeError("lengthArray must be an array");if(typeof n!="string")throw new TypeError("characters must be a string");if(n.length===0)throw new RangeError("characters must not be empty");e=e.map(r=>Math.floor(r)),n=[...new Set(n)].join("");let t,s=0,o=e.length-1;do t="",e.forEach((r,m)=>t+=`${l(n,r)}${m===o?"":"-"}`),s++,s>=Math.pow(t.length,t.length)&&e[o]++;while(i.has(t));return i.add(t),t};typeof window<"u"&&(typeof window.ssts!="object"&&(window.ssts={}),window.ssts.seniorID=w);var a=w;export{a as default};