@daanila01/smallid
Version:
Generate uniq id
3 lines • 1.08 kB
JavaScript
var crypto=require('crypto'),smallid=function(){this.POOL_SIZE=32;this.alphabet="nLYOvX2g3Jdqz1tuDiah8f5Rk7sVpHPEQUSITwxKbZcCGMlBA4Fjo0eNWrmy69_-";this.poolOffset=0;this.pool};smallid.prototype.randomBytes=function(a){!this.pool||this.pool.length<a?(this.pool=Buffer.allocUnsafe(a*this.POOL_SIZE),crypto.randomFillSync(this.pool),this.poolOffset=0):this.poolOffset+a>this.pool.length&&(crypto.randomFillSync(this.pool),this.poolOffset=0);var b=this.pool.subarray(this.poolOffset,this.poolOffset+a);this.poolOffset+=a;return b};
smallid.prototype.customAlphabet=function(a){for(var b=a.split(""),c=[],d=0;d<a.length;d++){var e=0<b.length-1?crypto.randomInt(b.length-1):0;c.push(b[e]);b.splice(e,1)}return this.alphabet=String(c.toString().replace(/,/g,""))};smallid.prototype.generate=function(a){a=void 0===a?12:a;for(var b="",c=this.randomBytes(a);a--;)b+=this.alphabet[c[a-1]&this.alphabet.length-1];return b};
smallid.prototype.isValid=function(a){var b=!0;if(1>a.length)return!1;for(var c=0;c<a.length;c++)-1===this.alphabet.indexOf(a[c])&&(b=!1);return b};module.exports=smallid;