package.dist.short-unique-id.min.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of short-unique-id Show documentation
Show all versions of short-unique-id Show documentation
Generate random or sequential UUID of any length
The newest version!
"use strict";var ShortUniqueId=(()=>{var l=Object.defineProperty;var D=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames,b=Object.getOwnPropertySymbols;var f=Object.prototype.hasOwnProperty,q=Object.prototype.propertyIsEnumerable;var g=(a,t,i)=>t in a?l(a,t,{enumerable:!0,configurable:!0,writable:!0,value:i}):a[t]=i,m=(a,t)=>{for(var i in t||(t={}))f.call(t,i)&&g(a,i,t[i]);if(b)for(var i of b(t))q.call(t,i)&&g(a,i,t[i]);return a};var x=(a,t)=>{for(var i in t)l(a,i,{get:t[i],enumerable:!0})},y=(a,t,i,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of w(t))!f.call(a,r)&&r!==i&&l(a,r,{get:()=>t[r],enumerable:!(s=D(t,r))||s.enumerable});return a};var S=a=>y(l({},"__esModule",{value:!0}),a);var e=(a,t,i)=>(g(a,typeof t!="symbol"?t+"":t,i),i);var $={};x($,{DEFAULT_OPTIONS:()=>I,DEFAULT_UUID_LENGTH:()=>d,default:()=>p});var U="5.2.0";var d=6,I={dictionary:"alphanum",shuffle:!0,debug:!1,length:d,counter:0},c=class c{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] ${t[0]}`,this.debug===!0&&typeof console<"u"&&console!==null)return console.log(...i)});e(this,"_normalizeDictionary",(t,i)=>{let s;if(t&&Array.isArray(t)&&t.length>1)s=t;else{s=[];let r;this.dictIndex=r=0;let o=`_${t}_dict_ranges`,n=this._dict_ranges[o];Object.keys(n).forEach(h=>{let _=h;for(this.dictRange=n[_],this.lowerBound=this.dictRange[0],this.upperBound=this.dictRange[1],this.dictIndex=r=this.lowerBound;this.lowerBound<=this.upperBound?rthis.upperBound;this.dictIndex=this.lowerBound<=this.upperBound?r+=1:r-=1)s.push(String.fromCharCode(this.dictIndex))})}return i&&(s=s.sort(()=>Math.random()-.5)),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,i,s="";t=this.counter;do i=t%this.dictLength,t=Math.trunc(t/this.dictLength),s+=this.dict[i];while(t!==0);return this.counter+=1,s});e(this,"rnd",(t=this.uuidLength||d)=>this.randomUUID(t));e(this,"randomUUID",(t=this.uuidLength||d)=>{let i,s,r;if(t===null||typeof t>"u"||t<1)throw new Error("Invalid UUID Length Provided");let o=t>=0;for(i="",r=0;rthis.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,o=>{let n=o.slice(0,2),h=parseInt(o.slice(2),10);return n==="$s"?s[n]().padStart(h,"0"):n==="$t"&&i?s[n](h,i):s[n](h)})});e(this,"availableUUIDs",(t=this.uuidLength)=>parseFloat(Math.pow([...new Set(this.dict)].length,t).toFixed(0)));e(this,"approxMaxBeforeCollision",(t=this.availableUUIDs(this.uuidLength))=>parseFloat(Math.sqrt(Math.PI/2*t).toFixed(20)));e(this,"collisionProbability",(t=this.availableUUIDs(this.uuidLength),i=this.uuidLength)=>parseFloat((this.approxMaxBeforeCollision(t)/this.availableUUIDs(i)).toFixed(20)));e(this,"uniqueness",(t=this.availableUUIDs(this.uuidLength))=>{let i=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(`
`));let r=t-9,o=Math.round(Math.random()*(r>15?15:r)),n=this.randomUUID(r);return`${n.substring(0,o)}${s}${n.substring(o)}${o.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,o=>{let n={$r:u=>[...Array(u)].map(()=>"r").join(""),$s:u=>[...Array(u)].map(()=>"s").join(""),$t:u=>[...Array(u)].map(()=>"t").join("")},h=o.slice(0,2),_=parseInt(o.slice(2),10);return n[h](_)}).replace(/^(.*?)(t{8,})(.*)$/g,(o,n,h)=>t.substring(n.length,n.length+h.length)):t;if(s.length===8)return new Date(parseInt(s,16)*1e3);if(s.length<10)throw new Error("Stamp length invalid");let r=parseInt(s.substring(s.length-1),16);return new Date(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=m(m({},I),t);this.counter=0,this.debug=!1,this.dict=[],this.version=U;let{dictionary:s,shuffle:r,length:o,counter:n}=i;return this.uuidLength=o,this.setDictionary(s,r),this.setCounter(n),this.debug=i.debug,this.log(this.dict),this.log(`Generator instantiated with Dictionary Size ${this.dictLength} and counter set to ${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),this}};e(c,"default",c);var p=c;return S($);})();
'undefined'!=typeof module&&(module.exports=ShortUniqueId.default),'undefined'!=typeof window&&(ShortUniqueId=ShortUniqueId.default);