hashids-esm.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  2. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  3. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  4. var Hashids =
  5. /*#__PURE__*/
  6. function () {
  7. function Hashids() {
  8. var salt = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  9. var minLength = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  10. var alphabet = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
  11. _classCallCheck(this, Hashids);
  12. var minAlphabetLength = 16;
  13. var sepDiv = 3.5;
  14. var guardDiv = 12;
  15. var errorAlphabetLength = 'error: alphabet must contain at least X unique characters';
  16. var errorAlphabetSpace = 'error: alphabet cannot contain spaces';
  17. var uniqueAlphabet = '',
  18. sepsLength,
  19. diff;
  20. /* funcs */
  21. this.escapeRegExp = function (s) {
  22. return s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
  23. };
  24. this.parseInt = function (v, radix) {
  25. return /^(-|\+)?([0-9]+|Infinity)$/.test(v) ? parseInt(v, radix) : NaN;
  26. };
  27. /* alphabet vars */
  28. this.seps = 'cfhistuCFHISTU';
  29. this.minLength = parseInt(minLength, 10) > 0 ? minLength : 0;
  30. this.salt = typeof salt === 'string' ? salt : '';
  31. if (typeof alphabet === 'string') {
  32. this.alphabet = alphabet;
  33. }
  34. for (var i = 0; i !== this.alphabet.length; i++) {
  35. if (uniqueAlphabet.indexOf(this.alphabet.charAt(i)) === -1) {
  36. uniqueAlphabet += this.alphabet.charAt(i);
  37. }
  38. }
  39. this.alphabet = uniqueAlphabet;
  40. if (this.alphabet.length < minAlphabetLength) {
  41. throw errorAlphabetLength.replace('X', minAlphabetLength);
  42. }
  43. if (this.alphabet.search(' ') !== -1) {
  44. throw errorAlphabetSpace;
  45. }
  46. /*
  47. `this.seps` should contain only characters present in `this.alphabet`
  48. `this.alphabet` should not contains `this.seps`
  49. */
  50. for (var _i = 0; _i !== this.seps.length; _i++) {
  51. var j = this.alphabet.indexOf(this.seps.charAt(_i));
  52. if (j === -1) {
  53. this.seps = this.seps.substr(0, _i) + ' ' + this.seps.substr(_i + 1);
  54. } else {
  55. this.alphabet = this.alphabet.substr(0, j) + ' ' + this.alphabet.substr(j + 1);
  56. }
  57. }
  58. this.alphabet = this.alphabet.replace(/ /g, '');
  59. this.seps = this.seps.replace(/ /g, '');
  60. this.seps = this._shuffle(this.seps, this.salt);
  61. if (!this.seps.length || this.alphabet.length / this.seps.length > sepDiv) {
  62. sepsLength = Math.ceil(this.alphabet.length / sepDiv);
  63. if (sepsLength > this.seps.length) {
  64. diff = sepsLength - this.seps.length;
  65. this.seps += this.alphabet.substr(0, diff);
  66. this.alphabet = this.alphabet.substr(diff);
  67. }
  68. }
  69. this.alphabet = this._shuffle(this.alphabet, this.salt);
  70. var guardCount = Math.ceil(this.alphabet.length / guardDiv);
  71. if (this.alphabet.length < 3) {
  72. this.guards = this.seps.substr(0, guardCount);
  73. this.seps = this.seps.substr(guardCount);
  74. } else {
  75. this.guards = this.alphabet.substr(0, guardCount);
  76. this.alphabet = this.alphabet.substr(guardCount);
  77. }
  78. }
  79. _createClass(Hashids, [{
  80. key: "encode",
  81. value: function encode() {
  82. for (var _len = arguments.length, numbers = new Array(_len), _key = 0; _key < _len; _key++) {
  83. numbers[_key] = arguments[_key];
  84. }
  85. var ret = '';
  86. if (!numbers.length) {
  87. return ret;
  88. }
  89. if (numbers[0] && numbers[0].constructor === Array) {
  90. numbers = numbers[0];
  91. if (!numbers.length) {
  92. return ret;
  93. }
  94. }
  95. for (var i = 0; i !== numbers.length; i++) {
  96. numbers[i] = this.parseInt(numbers[i], 10);
  97. if (numbers[i] >= 0) {
  98. continue;
  99. } else {
  100. return ret;
  101. }
  102. }
  103. return this._encode(numbers);
  104. }
  105. }, {
  106. key: "decode",
  107. value: function decode(id) {
  108. var ret = [];
  109. if (!id || !id.length || typeof id !== 'string') {
  110. return ret;
  111. }
  112. return this._decode(id, this.alphabet);
  113. }
  114. }, {
  115. key: "encodeHex",
  116. value: function encodeHex(hex) {
  117. hex = hex.toString();
  118. if (!/^[0-9a-fA-F]+$/.test(hex)) {
  119. return '';
  120. }
  121. var numbers = hex.match(/[\w\W]{1,12}/g);
  122. for (var i = 0; i !== numbers.length; i++) {
  123. numbers[i] = parseInt('1' + numbers[i], 16);
  124. }
  125. return this.encode.apply(this, numbers);
  126. }
  127. }, {
  128. key: "decodeHex",
  129. value: function decodeHex(id) {
  130. var ret = [];
  131. var numbers = this.decode(id);
  132. for (var i = 0; i !== numbers.length; i++) {
  133. ret += numbers[i].toString(16).substr(1);
  134. }
  135. return ret;
  136. }
  137. }, {
  138. key: "_encode",
  139. value: function _encode(numbers) {
  140. var ret,
  141. alphabet = this.alphabet,
  142. numbersIdInt = 0;
  143. for (var i = 0; i !== numbers.length; i++) {
  144. numbersIdInt += numbers[i] % (i + 100);
  145. }
  146. ret = alphabet.charAt(numbersIdInt % alphabet.length);
  147. var lottery = ret;
  148. for (var _i2 = 0; _i2 !== numbers.length; _i2++) {
  149. var number = numbers[_i2];
  150. var buffer = lottery + this.salt + alphabet;
  151. alphabet = this._shuffle(alphabet, buffer.substr(0, alphabet.length));
  152. var last = this._toAlphabet(number, alphabet);
  153. ret += last;
  154. if (_i2 + 1 < numbers.length) {
  155. number %= last.charCodeAt(0) + _i2;
  156. var sepsIndex = number % this.seps.length;
  157. ret += this.seps.charAt(sepsIndex);
  158. }
  159. }
  160. if (ret.length < this.minLength) {
  161. var guardIndex = (numbersIdInt + ret[0].charCodeAt(0)) % this.guards.length;
  162. var guard = this.guards[guardIndex];
  163. ret = guard + ret;
  164. if (ret.length < this.minLength) {
  165. guardIndex = (numbersIdInt + ret[2].charCodeAt(0)) % this.guards.length;
  166. guard = this.guards[guardIndex];
  167. ret += guard;
  168. }
  169. }
  170. var halfLength = parseInt(alphabet.length / 2, 10);
  171. while (ret.length < this.minLength) {
  172. alphabet = this._shuffle(alphabet, alphabet);
  173. ret = alphabet.substr(halfLength) + ret + alphabet.substr(0, halfLength);
  174. var excess = ret.length - this.minLength;
  175. if (excess > 0) {
  176. ret = ret.substr(excess / 2, this.minLength);
  177. }
  178. }
  179. return ret;
  180. }
  181. }, {
  182. key: "_decode",
  183. value: function _decode(id, alphabet) {
  184. var ret = [],
  185. i = 0,
  186. r = new RegExp("[".concat(this.escapeRegExp(this.guards), "]"), 'g'),
  187. idBreakdown = id.replace(r, ' '),
  188. idArray = idBreakdown.split(' ');
  189. if (idArray.length === 3 || idArray.length === 2) {
  190. i = 1;
  191. }
  192. idBreakdown = idArray[i];
  193. if (typeof idBreakdown[0] !== 'undefined') {
  194. var lottery = idBreakdown[0];
  195. idBreakdown = idBreakdown.substr(1);
  196. r = new RegExp("[".concat(this.escapeRegExp(this.seps), "]"), 'g');
  197. idBreakdown = idBreakdown.replace(r, ' ');
  198. idArray = idBreakdown.split(' ');
  199. for (var j = 0; j !== idArray.length; j++) {
  200. var subId = idArray[j];
  201. var buffer = lottery + this.salt + alphabet;
  202. alphabet = this._shuffle(alphabet, buffer.substr(0, alphabet.length));
  203. ret.push(this._fromAlphabet(subId, alphabet));
  204. }
  205. if (this.encode(ret) !== id) {
  206. ret = [];
  207. }
  208. }
  209. return ret;
  210. }
  211. }, {
  212. key: "_shuffle",
  213. value: function _shuffle(alphabet, salt) {
  214. var integer;
  215. if (!salt.length) {
  216. return alphabet;
  217. }
  218. alphabet = alphabet.split("");
  219. for (var i = alphabet.length - 1, v = 0, p = 0, j = 0; i > 0; i--, v++) {
  220. v %= salt.length;
  221. p += integer = salt.charCodeAt(v);
  222. j = (integer + v + p) % i;
  223. var tmp = alphabet[j];
  224. alphabet[j] = alphabet[i];
  225. alphabet[i] = tmp;
  226. }
  227. alphabet = alphabet.join("");
  228. return alphabet;
  229. }
  230. }, {
  231. key: "_toAlphabet",
  232. value: function _toAlphabet(input, alphabet) {
  233. var id = '';
  234. do {
  235. id = alphabet.charAt(input % alphabet.length) + id;
  236. input = parseInt(input / alphabet.length, 10);
  237. } while (input);
  238. return id;
  239. }
  240. }, {
  241. key: "_fromAlphabet",
  242. value: function _fromAlphabet(input, alphabet) {
  243. return input.split("").map(function (item) {
  244. return alphabet.indexOf(item);
  245. }).reduce(function (carry, item) {
  246. return carry * alphabet.length + item;
  247. }, 0);
  248. }
  249. }]);
  250. return Hashids;
  251. }();
  252. export { Hashids as default };
  253. //# sourceMappingURL=hashids.mjs.map