index.esm.js 3.4 KB

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