index.umd.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  3. typeof define === 'function' && define.amd ? define(factory) :
  4. (global.Ids = factory());
  5. }(this, (function () {
  6. function createCommonjsModule(fn, module) {
  7. return module = { exports: {} }, fn(module, module.exports), module.exports;
  8. }
  9. var hat_1 = createCommonjsModule(function (module) {
  10. var hat = module.exports = function (bits, base) {
  11. if (!base) base = 16;
  12. if (bits === undefined) bits = 128;
  13. if (bits <= 0) return '0';
  14. var digits = Math.log(Math.pow(2, bits)) / Math.log(base);
  15. for (var i = 2; digits === Infinity; i *= 2) {
  16. digits = Math.log(Math.pow(2, bits / i)) / Math.log(base) * i;
  17. }
  18. var rem = digits - Math.floor(digits);
  19. var res = '';
  20. for (var i = 0; i < Math.floor(digits); i++) {
  21. var x = Math.floor(Math.random() * base).toString(base);
  22. res = x + res;
  23. }
  24. if (rem) {
  25. var b = Math.pow(base, rem);
  26. var x = Math.floor(Math.random() * b).toString(base);
  27. res = x + res;
  28. }
  29. var parsed = parseInt(res, base);
  30. if (parsed !== Infinity && parsed >= Math.pow(2, bits)) {
  31. return hat(bits, base)
  32. }
  33. else return res;
  34. };
  35. hat.rack = function (bits, base, expandBy) {
  36. var fn = function (data) {
  37. var iters = 0;
  38. do {
  39. if (iters ++ > 10) {
  40. if (expandBy) bits += expandBy;
  41. else throw new Error('too many ID collisions, use more bits')
  42. }
  43. var id = hat(bits, base);
  44. } while (Object.hasOwnProperty.call(hats, id));
  45. hats[id] = data;
  46. return id;
  47. };
  48. var hats = fn.hats = {};
  49. fn.get = function (id) {
  50. return fn.hats[id];
  51. };
  52. fn.set = function (id, value) {
  53. fn.hats[id] = value;
  54. return fn;
  55. };
  56. fn.bits = bits || 128;
  57. fn.base = base || 16;
  58. return fn;
  59. };
  60. });
  61. /**
  62. * Create a new id generator / cache instance.
  63. *
  64. * You may optionally provide a seed that is used internally.
  65. *
  66. * @param {Seed} seed
  67. */
  68. function Ids(seed) {
  69. if (!(this instanceof Ids)) {
  70. return new Ids(seed);
  71. }
  72. seed = seed || [128, 36, 1];
  73. this._seed = seed.length ? hat_1.rack(seed[0], seed[1], seed[2]) : seed;
  74. }
  75. /**
  76. * Generate a next id.
  77. *
  78. * @param {Object} [element] element to bind the id to
  79. *
  80. * @return {String} id
  81. */
  82. Ids.prototype.next = function (element) {
  83. return this._seed(element || true);
  84. };
  85. /**
  86. * Generate a next id with a given prefix.
  87. *
  88. * @param {Object} [element] element to bind the id to
  89. *
  90. * @return {String} id
  91. */
  92. Ids.prototype.nextPrefixed = function (prefix, element) {
  93. var id;
  94. do {
  95. id = prefix + this.next(true);
  96. } while (this.assigned(id)); // claim {prefix}{random}
  97. this.claim(id, element); // return
  98. return id;
  99. };
  100. /**
  101. * Manually claim an existing id.
  102. *
  103. * @param {String} id
  104. * @param {String} [element] element the id is claimed by
  105. */
  106. Ids.prototype.claim = function (id, element) {
  107. this._seed.set(id, element || true);
  108. };
  109. /**
  110. * Returns true if the given id has already been assigned.
  111. *
  112. * @param {String} id
  113. * @return {Boolean}
  114. */
  115. Ids.prototype.assigned = function (id) {
  116. return this._seed.get(id) || false;
  117. };
  118. /**
  119. * Unclaim an id.
  120. *
  121. * @param {String} id the id to unclaim
  122. */
  123. Ids.prototype.unclaim = function (id) {
  124. delete this._seed.hats[id];
  125. };
  126. /**
  127. * Clear all claimed ids.
  128. */
  129. Ids.prototype.clear = function () {
  130. var hats = this._seed.hats,
  131. id;
  132. for (id in hats) {
  133. this.unclaim(id);
  134. }
  135. };
  136. return Ids;
  137. })));
  138. //# sourceMappingURL=index.umd.js.map