UNPKG

@ssts/id

Version:

Create unique random string IDs

2 lines (1 loc) 1.28 kB
"use strict";(()=>{var i=(e,r)=>{if(typeof e!="number")throw new TypeError("min must be a number");if(typeof r!="number")throw new TypeError("max must be a number");return Math.floor(Math.random()*(r-e+1)+e)};typeof window<"u"&&(typeof window.ssts!="object"&&(window.ssts={}),window.ssts.seniorINT=i);var w=i;var m=new Set,a=(e,r)=>{let t="",s=e.length-1;for(let o=0;o<r;o++)t+=e.charAt(w(0,s));return t},u=(e=[8,4,4,4,12],r="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")=>{if(Array.isArray(e)){if(e.length===0)throw new RangeError("lengthArray must not be empty");if(e.some(n=>typeof n!="number"))throw new TypeError("All lengthArray items must be numbers");if(e.some(n=>n<1))throw new RangeError("All lengthArray numbers must be greater than 0")}else throw new TypeError("lengthArray must be an array");if(typeof r!="string")throw new TypeError("characters must be a string");if(r.length===0)throw new RangeError("characters must not be empty");e=e.map(n=>Math.floor(n)),r=[...new Set(r)].join("");let t,s=0,o=e.length-1;do t="",e.forEach((n,f)=>t+=`${a(r,n)}${f===o?"":"-"}`),s++,s>=Math.pow(t.length,t.length)&&e[o]++;while(m.has(t));return m.add(t),t};typeof window<"u"&&(typeof window.ssts!="object"&&(window.ssts={}),window.ssts.seniorID=u);var p=u;})();