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CAAD,CAAR,GAAc,GAAd;AAEA;;AAED,QAAA,QAAQ,GAAG,QAAQ,CAAC,IAAT,CAAc,EAAd,CAAX;AAEA,eAAO,QAAP;AAEA;;;kCAEW,K,EAAO,Q,EAAU;AAE5B,YAAI,EAAE,GAAG,EAAT;;AAEA,WAAG;AACF,UAAA,EAAE,GAAG,QAAQ,CAAC,MAAT,CAAgB,KAAK,GAAG,QAAQ,CAAC,MAAjC,IAA2C,EAAhD;AACA,UAAA,KAAK,GAAG,QAAQ,CAAC,KAAK,GAAG,QAAQ,CAAC,MAAlB,EAA0B,EAA1B,CAAhB;AACA,SAHD,QAGS,KAHT;;AAKA,eAAO,EAAP;AAEA;;;oCAEa,K,EAAO,Q,EAAU;AAE9B,eAAO,KAAK,CAAC,KAAN,CAAY,EAAZ,EAAgB,GAAhB,CACN,UAAC,IAAD;AAAA,iBAAU,QAAQ,CAAC,OAAT,CAAiB,IAAjB,CAAV;AAAA,SADM,EAEL,MAFK,CAGN,UAAC,KAAD,EAAQ,IAAR;AAAA,iBAAiB,KAAK,GAAG,QAAQ,CAAC,MAAjB,GAA0B,IAA3C;AAAA,SAHM,EAIN,CAJM,CAAP;AAOA","file":"index.js","sourcesContent":["\nexport default class Hashids {\n\n\tconstructor(salt = '', minLength = 0, alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890') {\n\n\t\tconst minAlphabetLength = 16;\n\t\tconst sepDiv = 3.5;\n\t\tconst guardDiv = 12;\n\n\t\tconst errorAlphabetLength = 'error: alphabet must contain at least X unique characters';\n\t\tconst errorAlphabetSpace = 'error: alphabet cannot contain spaces';\n\n\t\tlet uniqueAlphabet = '', sepsLength, diff;\n\n\t\t/* funcs */\n\n\t\tthis.escapeRegExp = (s) => s.replace(/[-[\\]{}()*+?.,\\\\^$|#\\s]/g, '\\\\$&');\n\t\tthis.parseInt = (v, radix) => (/^(-|\\+)?([0-9]+|Infinity)$/.test(v)) ? parseInt(v, radix) : NaN;\n\n\t\t/* alphabet vars */\n\n\t\tthis.seps = 'cfhistuCFHISTU';\n\t\tthis.minLength = parseInt(minLength, 10) > 0 ? minLength : 0;\n\t\tthis.salt = (typeof salt === 'string') ? salt : '';\n\n\t\tif (typeof alphabet === 'string') {\n\t\t\tthis.alphabet = alphabet;\n\t\t}\n\n\t\tfor (let i = 0; i !== this.alphabet.length; i++) {\n\t\t\tif (uniqueAlphabet.indexOf(this.alphabet.charAt(i)) === -1) {\n\t\t\t\tuniqueAlphabet += this.alphabet.charAt(i);\n\t\t\t}\n\t\t}\n\n\t\tthis.alphabet = uniqueAlphabet;\n\n\t\tif (this.alphabet.length < minAlphabetLength) {\n\t\t\tthrow errorAlphabetLength.replace('X', minAlphabetLength);\n\t\t}\n\n\t\tif (this.alphabet.search(' ') !== -1) {\n\t\t\tthrow errorAlphabetSpace;\n\t\t}\n\n\t\t/*\n\t\t\t`this.seps` should contain only characters present in `this.alphabet`\n\t\t\t`this.alphabet` should not contains `this.seps`\n\t\t*/\n\n\t\tfor (let i = 0; i !== this.seps.length; i++) {\n\n\t\t\tconst j = this.alphabet.indexOf(this.seps.charAt(i));\n\t\t\tif (j === -1) {\n\t\t\t\tthis.seps = this.seps.substr(0, i) + ' ' + this.seps.substr(i + 1);\n\t\t\t} else {\n\t\t\t\tthis.alphabet = this.alphabet.substr(0, j) + ' ' + this.alphabet.substr(j + 1);\n\t\t\t}\n\n\t\t}\n\n\t\tthis.alphabet = this.alphabet.replace(/ /g, '');\n\n\t\tthis.seps = this.seps.replace(/ /g, '');\n\t\tthis.seps = this._shuffle(this.seps, this.salt);\n\n\t\tif (!this.seps.length || (this.alphabet.length / this.seps.length) > sepDiv) {\n\n\t\t\tsepsLength = Math.ceil(this.alphabet.length / sepDiv);\n\n\t\t\tif (sepsLength > this.seps.length) {\n\n\t\t\t\tdiff = sepsLength - this.seps.length;\n\t\t\t\tthis.seps += this.alphabet.substr(0, diff);\n\t\t\t\tthis.alphabet = this.alphabet.substr(diff);\n\n\t\t\t}\n\n\t\t}\n\n\t\tthis.alphabet = this._shuffle(this.alphabet, this.salt);\n\t\tconst guardCount = Math.ceil(this.alphabet.length / guardDiv);\n\n\t\tif (this.alphabet.length < 3) {\n\t\t\tthis.guards = this.seps.substr(0, guardCount);\n\t\t\tthis.seps = this.seps.substr(guardCount);\n\t\t} else {\n\t\t\tthis.guards = this.alphabet.substr(0, guardCount);\n\t\t\tthis.alphabet = this.alphabet.substr(guardCount);\n\t\t}\n\n\t}\n\n\tencode(...numbers) {\n\n\t\tconst ret = '';\n\n\t\tif (!numbers.length) {\n\t\t\treturn ret;\n\t\t}\n\n\t\tif (numbers[0] && numbers[0].constructor === Array) {\n\t\t\tnumbers = numbers[0];\n\t\t\tif (!numbers.length) {\n\t\t\t\treturn ret;\n\t\t\t}\n\t\t}\n\n\t\tfor (let i = 0; i !== numbers.length; i++) {\n\t\t\tnumbers[i] = this.parseInt(numbers[i], 10);\n\t\t\tif (numbers[i] >= 0) {\n\t\t\t\tcontinue;\n\t\t\t} else {\n\t\t\t\treturn ret;\n\t\t\t}\n\t\t}\n\n\t\treturn this._encode(numbers);\n\n\t}\n\n\tdecode(id) {\n\n\t\tconst ret = [];\n\n\t\tif (!id || !id.length || typeof id !== 'string') {\n\t\t\treturn ret;\n\t\t}\n\n\t\treturn this._decode(id, this.alphabet);\n\n\t}\n\n\tencodeHex(hex) {\n\n\t\thex = hex.toString();\n\t\tif (!/^[0-9a-fA-F]+$/.test(hex)) {\n\t\t\treturn '';\n\t\t}\n\n\t\tconst numbers = hex.match(/[\\w\\W]{1,12}/g);\n\n\t\tfor (let i = 0; i !== numbers.length; i++) {\n\t\t\tnumbers[i] = parseInt('1' + numbers[i], 16);\n\t\t}\n\n\t\treturn this.encode.apply(this, numbers);\n\n\t}\n\n\tdecodeHex(id) {\n\n\t\tlet ret = [];\n\n\t\tconst numbers = this.decode(id);\n\n\t\tfor (let i = 0; i !== numbers.length; i++) {\n\t\t\tret += (numbers[i]).toString(16).substr(1);\n\t\t}\n\n\t\treturn ret;\n\n\t}\n\n\t_encode(numbers) {\n\n\t\tlet ret,\n\t\t\talphabet = this.alphabet,\n\t\t\tnumbersIdInt = 0;\n\n\t\tfor (let i = 0; i !== numbers.length; i++) {\n\t\t\tnumbersIdInt += (numbers[i] % (i + 100));\n\t\t}\n\n\t\tret = alphabet.charAt(numbersIdInt % alphabet.length);\n\t\tconst lottery = ret;\n\n\t\tfor (let i = 0; i !== numbers.length; i++) {\n\n\t\t\tlet number = numbers[i];\n\t\t\tconst buffer = lottery + this.salt + alphabet;\n\n\t\t\talphabet = this._shuffle(alphabet, buffer.substr(0, alphabet.length));\n\t\t\tconst last = this._toAlphabet(number, alphabet);\n\n\t\t\tret += last;\n\n\t\t\tif (i + 1 < numbers.length) {\n\t\t\t\tnumber %= (last.charCodeAt(0) + i);\n\t\t\t\tconst sepsIndex = number % this.seps.length;\n\t\t\t\tret += this.seps.charAt(sepsIndex);\n\t\t\t}\n\n\t\t}\n\n\t\tif (ret.length < this.minLength) {\n\n\t\t\tlet guardIndex = (numbersIdInt + ret[0].charCodeAt(0)) % this.guards.length;\n\t\t\tlet guard = this.guards[guardIndex];\n\n\t\t\tret = guard + ret;\n\n\t\t\tif (ret.length < this.minLength) {\n\n\t\t\t\tguardIndex = (numbersIdInt + ret[2].charCodeAt(0)) % this.guards.length;\n\t\t\t\tguard = this.guards[guardIndex];\n\n\t\t\t\tret += guard;\n\n\t\t\t}\n\n\t\t}\n\n\t\tconst halfLength = parseInt(alphabet.length / 2, 10);\n\t\twhile (ret.length < this.minLength) {\n\n\t\t\talphabet = this._shuffle(alphabet, alphabet);\n\t\t\tret = alphabet.substr(halfLength) + ret + alphabet.substr(0, halfLength);\n\n\t\t\tconst excess = ret.length - this.minLength;\n\t\t\tif (excess > 0) {\n\t\t\t\tret = ret.substr(excess / 2, this.minLength);\n\t\t\t}\n\n\t\t}\n\n\t\treturn ret;\n\n\t}\n\n\t_decode(id, alphabet) {\n\n\t\tlet ret = [], i = 0,\n\t\t\tr = new RegExp(`[${this.escapeRegExp(this.guards)}]`, 'g'),\n\t\t\tidBreakdown = id.replace(r, ' '),\n\t\t\tidArray = idBreakdown.split(' ');\n\n\t\tif (idArray.length === 3 || idArray.length === 2) {\n\t\t\ti = 1;\n\t\t}\n\n\t\tidBreakdown = idArray[i];\n\t\tif (typeof idBreakdown[0] !== 'undefined') {\n\n\t\t\tconst lottery = idBreakdown[0];\n\t\t\tidBreakdown = idBreakdown.substr(1);\n\n\t\t\tr = new RegExp(`[${this.escapeRegExp(this.seps)}]`, 'g');\n\t\t\tidBreakdown = idBreakdown.replace(r, ' ');\n\t\t\tidArray = idBreakdown.split(' ');\n\n\t\t\tfor (let j = 0; j !== idArray.length; j++) {\n\n\t\t\t\tconst subId = idArray[j];\n\t\t\t\tconst buffer = lottery + this.salt + alphabet;\n\n\t\t\t\talphabet = this._shuffle(alphabet, buffer.substr(0, alphabet.length));\n\t\t\t\tret.push(this._fromAlphabet(subId, alphabet));\n\n\t\t\t}\n\n\t\t\tif (this.encode(ret) !== id) {\n\t\t\t\tret = [];\n\t\t\t}\n\n\t\t}\n\n\t\treturn ret;\n\n\t}\n\n\t_shuffle(alphabet, salt) {\n\n\t\tlet integer;\n\n\t\tif (!salt.length) {\n\t\t\treturn alphabet;\n\t\t}\n\n\t\talphabet = alphabet.split(\"\");\n\n\t\tfor (let i = alphabet.length - 1, v = 0, p = 0, j = 0; i > 0; i--, v++) {\n\n\t\t\tv %= salt.length;\n\t\t\tp += integer = salt.charCodeAt(v);\n\t\t\tj = (integer + v + p) % i;\n\n\t\t\tconst tmp = alphabet[j];\n\t\t\talphabet[j] = alphabet[i];\n\t\t\talphabet[i] = tmp;\n\n\t\t}\n\n\t\talphabet = alphabet.join(\"\");\n\n\t\treturn alphabet;\n\n\t}\n\n\t_toAlphabet(input, alphabet) {\n\n\t\tlet id = '';\n\n\t\tdo {\n\t\t\tid = alphabet.charAt(input % alphabet.length) + id;\n\t\t\tinput = parseInt(input / alphabet.length, 10);\n\t\t} while (input);\n\n\t\treturn id;\n\n\t}\n\n\t_fromAlphabet(input, alphabet) {\n\n\t\treturn input.split(\"\").map(\n\t\t\t(item) => alphabet.indexOf(item)\n\t\t).reduce(\n\t\t\t(carry, item) => carry * alphabet.length + item,\n\t\t\t0\n\t\t);\n\n\t}\n\n}\n"]}
|