short-unique-id
Advanced tools
Comparing version 4.3.0 to 4.3.1
@@ -5,2 +5,9 @@ # Changelog | ||
### [4.3.1](https://github.com/jeanlescure/short-unique-id/compare/v4.3.0...v4.3.1) (2021-05-04) | ||
### Bug Fixes | ||
* stamp length check ([4714321](https://github.com/jeanlescure/short-unique-id/commit/47143219a0c33e98addd319e58ca6d3ab29f7ef9)) | ||
## [4.3.0](https://github.com/jeanlescure/short-unique-id/compare/v4.2.1...v4.3.0) (2021-05-04) | ||
@@ -7,0 +14,0 @@ |
@@ -72,3 +72,3 @@ (function (global, factory) { | ||
var version = "4.3.0"; | ||
var version = "4.3.1"; | ||
@@ -396,4 +396,4 @@ // @module ShortUniqueId | ||
_this.stamp = function (finalLength) { | ||
if (finalLength < 10) { | ||
throw new Error('Param finalLength must be at least 10'); | ||
if (typeof finalLength !== 'number' || finalLength < 10) { | ||
throw new Error('Param finalLength must be number greater than 10'); | ||
} | ||
@@ -400,0 +400,0 @@ var hexStamp = Math.floor(+new Date() / 1000).toString(16); |
@@ -1,1 +0,1 @@ | ||
(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?module.exports=factory():typeof define==="function"&&define.amd?define(factory):(global=typeof globalThis!=="undefined"?globalThis:global||self,global.ShortUniqueId=factory())})(this,(function(){"use strict";var extendStatics=function(d,b){extendStatics=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(d,b){d.__proto__=b}||function(d,b){for(var p in b)if(Object.prototype.hasOwnProperty.call(b,p))d[p]=b[p]};return extendStatics(d,b)};function __extends(d,b){if(typeof b!=="function"&&b!==null)throw new TypeError("Class extends value "+String(b)+" is not a constructor or null");extendStatics(d,b);function __(){this.constructor=d}d.prototype=b===null?Object.create(b):(__.prototype=b.prototype,new __)}var __assign=function(){__assign=Object.assign||function __assign(t){for(var s,i=1,n=arguments.length;i<n;i++){s=arguments[i];for(var p in s)if(Object.prototype.hasOwnProperty.call(s,p))t[p]=s[p]}return t};return __assign.apply(this,arguments)};function __read(o,n){var m=typeof Symbol==="function"&&o[Symbol.iterator];if(!m)return o;var i=m.call(o),r,ar=[],e;try{while((n===void 0||n-- >0)&&!(r=i.next()).done)ar.push(r.value)}catch(error){e={error:error}}finally{try{if(r&&!r.done&&(m=i["return"]))m.call(i)}finally{if(e)throw e.error}}return ar}function __spreadArray(to,from){for(var i=0,il=from.length,j=to.length;i<il;i++,j++)to[j]=from[i];return to}var version="4.3.0";var DEFAULT_UUID_LENGTH=6;var DEFAULT_OPTIONS={dictionary:"alphanum",shuffle:true,debug:false,length:DEFAULT_UUID_LENGTH};var ShortUniqueId=function(_super){__extends(ShortUniqueId,_super);function ShortUniqueId(argOptions){if(argOptions===void 0){argOptions={}}var _this=_super.call(this)||this;_this.dictIndex=0;_this.dictRange=[];_this.lowerBound=0;_this.upperBound=0;_this.dictLength=0;_this._digit_first_ascii=48;_this._digit_last_ascii=58;_this._alpha_lower_first_ascii=97;_this._alpha_lower_last_ascii=123;_this._hex_last_ascii=103;_this._alpha_upper_first_ascii=65;_this._alpha_upper_last_ascii=91;_this._number_dict_ranges={digits:[_this._digit_first_ascii,_this._digit_last_ascii]};_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]};_this._alpha_lower_dict_ranges={lowerCase:[_this._alpha_lower_first_ascii,_this._alpha_lower_last_ascii]};_this._alpha_upper_dict_ranges={upperCase:[_this._alpha_upper_first_ascii,_this._alpha_upper_last_ascii]};_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]};_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]};_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]};_this._hex_dict_ranges={decDigits:[_this._digit_first_ascii,_this._digit_last_ascii],alphaDigits:[_this._alpha_lower_first_ascii,_this._hex_last_ascii]};_this.log=function(){var args=[];for(var _i=0;_i<arguments.length;_i++){args[_i]=arguments[_i]}var finalArgs=__spreadArray([],__read(args));finalArgs[0]="[short-unique-id] "+args[0];if(_this.debug===true){if(typeof console!=="undefined"&&console!==null){return console.log.apply(console,__spreadArray([],__read(finalArgs)))}}};_this.setDictionary=function(dictionary,shuffle){var finalDict;if(dictionary&&Array.isArray(dictionary)&&dictionary.length>1){finalDict=dictionary}else{finalDict=[];var i_1;_this.dictIndex=i_1=0;var rangesName="_"+dictionary+"_dict_ranges";var ranges_1=_this[rangesName];Object.keys(ranges_1).forEach((function(rangeType){var rangeTypeKey=rangeType;_this.dictRange=ranges_1[rangeTypeKey];_this.lowerBound=_this.dictRange[0];_this.upperBound=_this.dictRange[1];for(_this.dictIndex=i_1=_this.lowerBound;_this.lowerBound<=_this.upperBound?i_1<_this.upperBound:i_1>_this.upperBound;_this.dictIndex=_this.lowerBound<=_this.upperBound?i_1+=1:i_1-=1){finalDict.push(String.fromCharCode(_this.dictIndex))}}))}if(shuffle){var PROBABILITY_1=.5;finalDict=finalDict.sort((function(){return Math.random()-PROBABILITY_1}))}_this.dict=finalDict;_this.dictLength=_this.dict.length;_this.counter=0};_this.seq=function(){return _this.sequentialUUID()};_this.sequentialUUID=function(){var counterDiv;var counterRem;var id="";counterDiv=_this.counter;do{counterRem=counterDiv%_this.dictLength;counterDiv=Math.trunc(counterDiv/_this.dictLength);id+=_this.dict[counterRem]}while(counterDiv!==0);_this.counter+=1;return id};_this.randomUUID=function(uuidLength){if(uuidLength===void 0){uuidLength=_this.uuidLength||DEFAULT_UUID_LENGTH}var id;var randomPartIdx;var j;if(uuidLength===null||typeof uuidLength==="undefined"||uuidLength<1){throw new Error("Invalid UUID Length Provided")}id="";for(j=0;j<uuidLength;j+=1){randomPartIdx=parseInt((Math.random()*_this.dictLength).toFixed(0),10)%_this.dictLength;id+=_this.dict[randomPartIdx]}return id};_this.availableUUIDs=function(uuidLength){if(uuidLength===void 0){uuidLength=_this.uuidLength}return parseFloat(Math.pow(__spreadArray([],__read(new Set(_this.dict))).length,uuidLength).toFixed(0))};_this.approxMaxBeforeCollision=function(rounds){if(rounds===void 0){rounds=_this.availableUUIDs(_this.uuidLength)}return parseFloat(Math.sqrt(Math.PI/2*rounds).toFixed(20))};_this.collisionProbability=function(rounds,uuidLength){if(rounds===void 0){rounds=_this.availableUUIDs(_this.uuidLength)}if(uuidLength===void 0){uuidLength=_this.uuidLength}return parseFloat((_this.approxMaxBeforeCollision(rounds)/_this.availableUUIDs(uuidLength)).toFixed(20))};_this.uniqueness=function(rounds){if(rounds===void 0){rounds=_this.availableUUIDs(_this.uuidLength)}var score=parseFloat((1-_this.approxMaxBeforeCollision(rounds)/rounds).toFixed(20));return score>1?1:score<0?0:score};_this.getVersion=function(){return _this.version};_this.stamp=function(finalLength){if(finalLength<10){throw new Error("Param finalLength must be at least 10")}var hexStamp=Math.floor(+new Date/1e3).toString(16);var idLength=finalLength-9;var rndIdx=Math.round(Math.random()*(idLength>15?15:idLength));var id=_this.randomUUID(idLength);return""+id.substr(0,rndIdx)+hexStamp+id.substr(rndIdx)+rndIdx.toString(16)};_this.parseStamp=function(stamp){if(stamp.length<10){throw new Error("Stamp length invalid")}var rndIdx=parseInt(stamp.substr(stamp.length-1,1),16);return new Date(parseInt(stamp.substr(rndIdx,8),16)*1e3)};var options=__assign(__assign({},DEFAULT_OPTIONS),argOptions);_this.counter=0;_this.debug=false;_this.dict=[];_this.version=version;var dictionary=options.dictionary,shuffle=options.shuffle,length=options.length;_this.uuidLength=length;_this.setDictionary(dictionary,shuffle);_this.debug=options.debug;_this.log(_this.dict);_this.log("Generator instantiated with Dictionary Size "+_this.dictLength);return new Proxy(_this,{apply:function(target,that,args){return _this.randomUUID.apply(_this,__spreadArray([],__read(args)))}})}return ShortUniqueId}(Function);return ShortUniqueId})); | ||
(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?module.exports=factory():typeof define==="function"&&define.amd?define(factory):(global=typeof globalThis!=="undefined"?globalThis:global||self,global.ShortUniqueId=factory())})(this,(function(){"use strict";var extendStatics=function(d,b){extendStatics=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(d,b){d.__proto__=b}||function(d,b){for(var p in b)if(Object.prototype.hasOwnProperty.call(b,p))d[p]=b[p]};return extendStatics(d,b)};function __extends(d,b){if(typeof b!=="function"&&b!==null)throw new TypeError("Class extends value "+String(b)+" is not a constructor or null");extendStatics(d,b);function __(){this.constructor=d}d.prototype=b===null?Object.create(b):(__.prototype=b.prototype,new __)}var __assign=function(){__assign=Object.assign||function __assign(t){for(var s,i=1,n=arguments.length;i<n;i++){s=arguments[i];for(var p in s)if(Object.prototype.hasOwnProperty.call(s,p))t[p]=s[p]}return t};return __assign.apply(this,arguments)};function __read(o,n){var m=typeof Symbol==="function"&&o[Symbol.iterator];if(!m)return o;var i=m.call(o),r,ar=[],e;try{while((n===void 0||n-- >0)&&!(r=i.next()).done)ar.push(r.value)}catch(error){e={error:error}}finally{try{if(r&&!r.done&&(m=i["return"]))m.call(i)}finally{if(e)throw e.error}}return ar}function __spreadArray(to,from){for(var i=0,il=from.length,j=to.length;i<il;i++,j++)to[j]=from[i];return to}var version="4.3.1";var DEFAULT_UUID_LENGTH=6;var DEFAULT_OPTIONS={dictionary:"alphanum",shuffle:true,debug:false,length:DEFAULT_UUID_LENGTH};var ShortUniqueId=function(_super){__extends(ShortUniqueId,_super);function ShortUniqueId(argOptions){if(argOptions===void 0){argOptions={}}var _this=_super.call(this)||this;_this.dictIndex=0;_this.dictRange=[];_this.lowerBound=0;_this.upperBound=0;_this.dictLength=0;_this._digit_first_ascii=48;_this._digit_last_ascii=58;_this._alpha_lower_first_ascii=97;_this._alpha_lower_last_ascii=123;_this._hex_last_ascii=103;_this._alpha_upper_first_ascii=65;_this._alpha_upper_last_ascii=91;_this._number_dict_ranges={digits:[_this._digit_first_ascii,_this._digit_last_ascii]};_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]};_this._alpha_lower_dict_ranges={lowerCase:[_this._alpha_lower_first_ascii,_this._alpha_lower_last_ascii]};_this._alpha_upper_dict_ranges={upperCase:[_this._alpha_upper_first_ascii,_this._alpha_upper_last_ascii]};_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]};_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]};_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]};_this._hex_dict_ranges={decDigits:[_this._digit_first_ascii,_this._digit_last_ascii],alphaDigits:[_this._alpha_lower_first_ascii,_this._hex_last_ascii]};_this.log=function(){var args=[];for(var _i=0;_i<arguments.length;_i++){args[_i]=arguments[_i]}var finalArgs=__spreadArray([],__read(args));finalArgs[0]="[short-unique-id] "+args[0];if(_this.debug===true){if(typeof console!=="undefined"&&console!==null){return console.log.apply(console,__spreadArray([],__read(finalArgs)))}}};_this.setDictionary=function(dictionary,shuffle){var finalDict;if(dictionary&&Array.isArray(dictionary)&&dictionary.length>1){finalDict=dictionary}else{finalDict=[];var i_1;_this.dictIndex=i_1=0;var rangesName="_"+dictionary+"_dict_ranges";var ranges_1=_this[rangesName];Object.keys(ranges_1).forEach((function(rangeType){var rangeTypeKey=rangeType;_this.dictRange=ranges_1[rangeTypeKey];_this.lowerBound=_this.dictRange[0];_this.upperBound=_this.dictRange[1];for(_this.dictIndex=i_1=_this.lowerBound;_this.lowerBound<=_this.upperBound?i_1<_this.upperBound:i_1>_this.upperBound;_this.dictIndex=_this.lowerBound<=_this.upperBound?i_1+=1:i_1-=1){finalDict.push(String.fromCharCode(_this.dictIndex))}}))}if(shuffle){var PROBABILITY_1=.5;finalDict=finalDict.sort((function(){return Math.random()-PROBABILITY_1}))}_this.dict=finalDict;_this.dictLength=_this.dict.length;_this.counter=0};_this.seq=function(){return _this.sequentialUUID()};_this.sequentialUUID=function(){var counterDiv;var counterRem;var id="";counterDiv=_this.counter;do{counterRem=counterDiv%_this.dictLength;counterDiv=Math.trunc(counterDiv/_this.dictLength);id+=_this.dict[counterRem]}while(counterDiv!==0);_this.counter+=1;return id};_this.randomUUID=function(uuidLength){if(uuidLength===void 0){uuidLength=_this.uuidLength||DEFAULT_UUID_LENGTH}var id;var randomPartIdx;var j;if(uuidLength===null||typeof uuidLength==="undefined"||uuidLength<1){throw new Error("Invalid UUID Length Provided")}id="";for(j=0;j<uuidLength;j+=1){randomPartIdx=parseInt((Math.random()*_this.dictLength).toFixed(0),10)%_this.dictLength;id+=_this.dict[randomPartIdx]}return id};_this.availableUUIDs=function(uuidLength){if(uuidLength===void 0){uuidLength=_this.uuidLength}return parseFloat(Math.pow(__spreadArray([],__read(new Set(_this.dict))).length,uuidLength).toFixed(0))};_this.approxMaxBeforeCollision=function(rounds){if(rounds===void 0){rounds=_this.availableUUIDs(_this.uuidLength)}return parseFloat(Math.sqrt(Math.PI/2*rounds).toFixed(20))};_this.collisionProbability=function(rounds,uuidLength){if(rounds===void 0){rounds=_this.availableUUIDs(_this.uuidLength)}if(uuidLength===void 0){uuidLength=_this.uuidLength}return parseFloat((_this.approxMaxBeforeCollision(rounds)/_this.availableUUIDs(uuidLength)).toFixed(20))};_this.uniqueness=function(rounds){if(rounds===void 0){rounds=_this.availableUUIDs(_this.uuidLength)}var score=parseFloat((1-_this.approxMaxBeforeCollision(rounds)/rounds).toFixed(20));return score>1?1:score<0?0:score};_this.getVersion=function(){return _this.version};_this.stamp=function(finalLength){if(typeof finalLength!=="number"||finalLength<10){throw new Error("Param finalLength must be number greater than 10")}var hexStamp=Math.floor(+new Date/1e3).toString(16);var idLength=finalLength-9;var rndIdx=Math.round(Math.random()*(idLength>15?15:idLength));var id=_this.randomUUID(idLength);return""+id.substr(0,rndIdx)+hexStamp+id.substr(rndIdx)+rndIdx.toString(16)};_this.parseStamp=function(stamp){if(stamp.length<10){throw new Error("Stamp length invalid")}var rndIdx=parseInt(stamp.substr(stamp.length-1,1),16);return new Date(parseInt(stamp.substr(rndIdx,8),16)*1e3)};var options=__assign(__assign({},DEFAULT_OPTIONS),argOptions);_this.counter=0;_this.debug=false;_this.dict=[];_this.version=version;var dictionary=options.dictionary,shuffle=options.shuffle,length=options.length;_this.uuidLength=length;_this.setDictionary(dictionary,shuffle);_this.debug=options.debug;_this.log(_this.dict);_this.log("Generator instantiated with Dictionary Size "+_this.dictLength);return new Proxy(_this,{apply:function(target,that,args){return _this.randomUUID.apply(_this,__spreadArray([],__read(args)))}})}return ShortUniqueId}(Function);return ShortUniqueId})); |
{ | ||
"name": "short-unique-id", | ||
"version": "4.3.0", | ||
"version": "4.3.1", | ||
"description": "Generate random or sequential UUID of any length", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -171,3 +171,3 @@ # Short Unique ID (UUID) Generating Library | ||
For more information take a look at the [docs](https://shortunique.id/globals.html#options). | ||
For more information take a look at the [docs](https://shortunique.id/interfaces/shortuniqueidoptions.html). | ||
@@ -206,3 +206,3 @@ ## Available for | ||
To find out more about the math behind these functions please refer to the | ||
<a target="_blank" href="https://shortunique.id/classes/shortuniqueid.html#collisionprobability">API Reference</a>. | ||
<a target="_blank" href="https://shortunique.id/classes/default.html#collisionprobability">API Reference</a>. | ||
@@ -214,4 +214,7 @@ ## Acknowledgement and platform support | ||
![image depicting over 12000 weekly npm downloads](https://raw.githubusercontent.com/jeanlescure/short-unique-id/main/assets/weekly-downloads.png) | ||
![image depicting over 100000 weekly cdn hits](https://raw.githubusercontent.com/jeanlescure/short-unique-id/main/assets/weekly-cdn-hits.png) | ||
Since this package is now reporting 12k+ npm weekly downloads, we've gone ahead and re-written the whole of it in TypeScript and made sure to package dist modules compatible with Deno, Node.js and all major Browsers. | ||
Since this package is now reporting 12k+ npm weekly downloads and 100k+ weekly cdn hits, | ||
we've gone ahead and re-written the whole of it in TypeScript and made sure to package | ||
dist modules compatible with Deno, Node.js and all major Browsers. | ||
@@ -218,0 +221,0 @@ ## Development |
@@ -422,4 +422,4 @@ // @module ShortUniqueId | ||
stamp = (finalLength: number): string => { | ||
if (finalLength < 10) { | ||
throw new Error('Param finalLength must be at least 10'); | ||
if (typeof finalLength !== 'number' || finalLength < 10) { | ||
throw new Error('Param finalLength must be number greater than 10'); | ||
} | ||
@@ -426,0 +426,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
637367
56
288