short-unique-id
Version:
Generate random or sequential UUID of any length
2 lines • 8.22 kB
JavaScript
"use strict";var ShortUniqueId=(()=>{var p=Object.defineProperty;var y=Object.getOwnPropertyDescriptor;var x=Object.getOwnPropertyNames,I=Object.getOwnPropertySymbols;var D=Object.prototype.hasOwnProperty,S=Object.prototype.propertyIsEnumerable;var b=(o,t,i)=>t in o?p(o,t,{enumerable:!0,configurable:!0,writable:!0,value:i}):o[t]=i,f=(o,t)=>{for(var i in t||(t={}))D.call(t,i)&&b(o,i,t[i]);if(I)for(var i of I(t))S.call(t,i)&&b(o,i,t[i]);return o};var C=(o,t)=>{for(var i in t)p(o,i,{get:t[i],enumerable:!0})},M=(o,t,i,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of x(t))!D.call(o,r)&&r!==i&&p(o,r,{get:()=>t[r],enumerable:!(s=y(t,r))||s.enumerable});return o};var v=o=>M(p({},"__esModule",{value:!0}),o);var e=(o,t,i)=>b(o,typeof t!="symbol"?t+"":t,i);var j={};C(j,{DEFAULT_OPTIONS:()=>q,DEFAULT_UUID_LENGTH:()=>_,default:()=>g});var w="5.3.2";var _=6,q={dictionary:"alphanum",shuffle:!0,debug:!1,length:_,counter:0},m=class m{constructor(t={}){e(this,"counter");e(this,"debug");e(this,"dict");e(this,"version");e(this,"dictIndex",0);e(this,"dictRange",[]);e(this,"lowerBound",0);e(this,"upperBound",0);e(this,"dictLength",0);e(this,"uuidLength");e(this,"_digit_first_ascii",48);e(this,"_digit_last_ascii",58);e(this,"_alpha_lower_first_ascii",97);e(this,"_alpha_lower_last_ascii",123);e(this,"_hex_last_ascii",103);e(this,"_alpha_upper_first_ascii",65);e(this,"_alpha_upper_last_ascii",91);e(this,"_number_dict_ranges",{digits:[this._digit_first_ascii,this._digit_last_ascii]});e(this,"_alpha_dict_ranges",{lowerCase:[this._alpha_lower_first_ascii,this._alpha_lower_last_ascii],upperCase:[this._alpha_upper_first_ascii,this._alpha_upper_last_ascii]});e(this,"_alpha_lower_dict_ranges",{lowerCase:[this._alpha_lower_first_ascii,this._alpha_lower_last_ascii]});e(this,"_alpha_upper_dict_ranges",{upperCase:[this._alpha_upper_first_ascii,this._alpha_upper_last_ascii]});e(this,"_alphanum_dict_ranges",{digits:[this._digit_first_ascii,this._digit_last_ascii],lowerCase:[this._alpha_lower_first_ascii,this._alpha_lower_last_ascii],upperCase:[this._alpha_upper_first_ascii,this._alpha_upper_last_ascii]});e(this,"_alphanum_lower_dict_ranges",{digits:[this._digit_first_ascii,this._digit_last_ascii],lowerCase:[this._alpha_lower_first_ascii,this._alpha_lower_last_ascii]});e(this,"_alphanum_upper_dict_ranges",{digits:[this._digit_first_ascii,this._digit_last_ascii],upperCase:[this._alpha_upper_first_ascii,this._alpha_upper_last_ascii]});e(this,"_hex_dict_ranges",{decDigits:[this._digit_first_ascii,this._digit_last_ascii],alphaDigits:[this._alpha_lower_first_ascii,this._hex_last_ascii]});e(this,"_dict_ranges",{_number_dict_ranges:this._number_dict_ranges,_alpha_dict_ranges:this._alpha_dict_ranges,_alpha_lower_dict_ranges:this._alpha_lower_dict_ranges,_alpha_upper_dict_ranges:this._alpha_upper_dict_ranges,_alphanum_dict_ranges:this._alphanum_dict_ranges,_alphanum_lower_dict_ranges:this._alphanum_lower_dict_ranges,_alphanum_upper_dict_ranges:this._alphanum_upper_dict_ranges,_hex_dict_ranges:this._hex_dict_ranges});e(this,"log",(...t)=>{let i=[...t];if(i[0]="[short-unique-id] ".concat(t[0]),this.debug===!0&&typeof console<"u"&&console!==null){console.log(...i);return}});e(this,"_normalizeDictionary",(t,i)=>{let s;if(t&&Array.isArray(t)&&t.length>1)s=t;else{s=[],this.dictIndex=0;let r="_".concat(t,"_dict_ranges"),n=this._dict_ranges[r],a=0;for(let[,c]of Object.entries(n)){let[u,d]=c;a+=Math.abs(d-u)}s=new Array(a);let h=0;for(let[,c]of Object.entries(n)){this.dictRange=c,this.lowerBound=this.dictRange[0],this.upperBound=this.dictRange[1];let u=this.lowerBound<=this.upperBound,d=this.lowerBound,U=this.upperBound;if(u)for(let l=d;l<U;l++)s[h++]=String.fromCharCode(l),this.dictIndex=l;else for(let l=d;l>U;l--)s[h++]=String.fromCharCode(l),this.dictIndex=l}s.length=h}if(i){let r=s.length;for(let n=r-1;n>0;n--){let a=Math.floor(Math.random()*(n+1));[s[n],s[a]]=[s[a],s[n]]}}return s});e(this,"setDictionary",(t,i)=>{this.dict=this._normalizeDictionary(t,i),this.dictLength=this.dict.length,this.setCounter(0)});e(this,"seq",()=>this.sequentialUUID());e(this,"sequentialUUID",()=>{let t=this.dictLength,i=this.dict,s=this.counter,r=[];do{let a=s%t;s=Math.trunc(s/t),r.push(i[a])}while(s!==0);let n=r.join("");return this.counter+=1,n});e(this,"rnd",(t=this.uuidLength||_)=>this.randomUUID(t));e(this,"randomUUID",(t=this.uuidLength||_)=>{if(t===null||typeof t>"u"||t<1)throw new Error("Invalid UUID Length Provided");let i=new Array(t),s=this.dictLength,r=this.dict;for(let n=0;n<t;n++){let a=Math.floor(Math.random()*s);i[n]=r[a]}return i.join("")});e(this,"fmt",(t,i)=>this.formattedUUID(t,i));e(this,"formattedUUID",(t,i)=>{let s={$r:this.randomUUID,$s:this.sequentialUUID,$t:this.stamp};return t.replace(/\$[rs]\d{0,}|\$t0|\$t[1-9]\d{1,}/g,n=>{let a=n.slice(0,2),h=Number.parseInt(n.slice(2),10);return a==="$s"?s[a]().padStart(h,"0"):a==="$t"&&i?s[a](h,i):s[a](h)})});e(this,"availableUUIDs",(t=this.uuidLength)=>Number.parseFloat(([...new Set(this.dict)].length**t).toFixed(0)));e(this,"_collisionCache",new Map);e(this,"approxMaxBeforeCollision",(t=this.availableUUIDs(this.uuidLength))=>{let i=t,s=this._collisionCache.get(i);if(s!==void 0)return s;let r=Number.parseFloat(Math.sqrt(Math.PI/2*t).toFixed(20));return this._collisionCache.set(i,r),r});e(this,"collisionProbability",(t=this.availableUUIDs(this.uuidLength),i=this.uuidLength)=>Number.parseFloat((this.approxMaxBeforeCollision(t)/this.availableUUIDs(i)).toFixed(20)));e(this,"uniqueness",(t=this.availableUUIDs(this.uuidLength))=>{let i=Number.parseFloat((1-this.approxMaxBeforeCollision(t)/t).toFixed(20));return i>1?1:i<0?0:i});e(this,"getVersion",()=>this.version);e(this,"stamp",(t,i)=>{let s=Math.floor(+(i||new Date)/1e3).toString(16);if(typeof t=="number"&&t===0)return s;if(typeof t!="number"||t<10)throw new Error(["Param finalLength must be a number greater than or equal to 10,","or 0 if you want the raw hexadecimal timestamp"].join("\n"));let r=t-9,n=Math.round(Math.random()*(r>15?15:r)),a=this.randomUUID(r);return"".concat(a.substring(0,n)).concat(s).concat(a.substring(n)).concat(n.toString(16))});e(this,"parseStamp",(t,i)=>{if(i&&!/t0|t[1-9]\d{1,}/.test(i))throw new Error("Cannot extract date from a formated UUID with no timestamp in the format");let s=i?i.replace(/\$[rs]\d{0,}|\$t0|\$t[1-9]\d{1,}/g,n=>{let a={$r:u=>[...Array(u)].map(()=>"r").join(""),$s:u=>[...Array(u)].map(()=>"s").join(""),$t:u=>[...Array(u)].map(()=>"t").join("")},h=n.slice(0,2),c=Number.parseInt(n.slice(2),10);return a[h](c)}).replace(/^(.*?)(t{8,})(.*)$/g,(n,a,h)=>t.substring(a.length,a.length+h.length)):t;if(s.length===8)return new Date(Number.parseInt(s,16)*1e3);if(s.length<10)throw new Error("Stamp length invalid");let r=Number.parseInt(s.substring(s.length-1),16);return new Date(Number.parseInt(s.substring(r,r+8),16)*1e3)});e(this,"setCounter",t=>{this.counter=t});e(this,"validate",(t,i)=>{let s=i?this._normalizeDictionary(i):this.dict;return t.split("").every(r=>s.includes(r))});let i=f(f({},q),t);this.counter=0,this.debug=!1,this.dict=[],this.version=w;let{dictionary:s,shuffle:r,length:n,counter:a}=i;this.uuidLength=n,this.setDictionary(s,r),this.setCounter(a),this.debug=i.debug,this.log(this.dict),this.log("Generator instantiated with Dictionary Size ".concat(this.dictLength," and counter set to ").concat(this.counter)),this.log=this.log.bind(this),this.setDictionary=this.setDictionary.bind(this),this.setCounter=this.setCounter.bind(this),this.seq=this.seq.bind(this),this.sequentialUUID=this.sequentialUUID.bind(this),this.rnd=this.rnd.bind(this),this.randomUUID=this.randomUUID.bind(this),this.fmt=this.fmt.bind(this),this.formattedUUID=this.formattedUUID.bind(this),this.availableUUIDs=this.availableUUIDs.bind(this),this.approxMaxBeforeCollision=this.approxMaxBeforeCollision.bind(this),this.collisionProbability=this.collisionProbability.bind(this),this.uniqueness=this.uniqueness.bind(this),this.getVersion=this.getVersion.bind(this),this.stamp=this.stamp.bind(this),this.parseStamp=this.parseStamp.bind(this)}};e(m,"default",m);var g=m;return v(j);})();
'undefined'!=typeof module&&(module.exports=ShortUniqueId.default),'undefined'!=typeof window&&(ShortUniqueId=ShortUniqueId.default);