1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- 'use strict';
- var hat = require('hat');
- /**
- * Create a new id generator / cache instance.
- *
- * You may optionally provide a seed that is used internally.
- *
- * @param {Seed} seed
- */
- function Ids(seed) {
- if (!(this instanceof Ids)) {
- return new Ids(seed);
- }
- seed = seed || [ 128, 36, 1 ];
- this._seed = seed.length ? hat.rack(seed[0], seed[1], seed[2]) : seed;
- }
- module.exports = Ids;
- /**
- * Generate a next id.
- *
- * @param {Object} [element] element to bind the id to
- *
- * @return {String} id
- */
- Ids.prototype.next = function(element) {
- return this._seed(element || true);
- };
- /**
- * Generate a next id with a given prefix.
- *
- * @param {Object} [element] element to bind the id to
- *
- * @return {String} id
- */
- Ids.prototype.nextPrefixed = function(prefix, element) {
- var id;
- do {
- id = prefix + this.next(true);
- } while (this.assigned(id));
- // claim {prefix}{random}
- this.claim(id, element);
- // return
- return id;
- };
- /**
- * Manually claim an existing id.
- *
- * @param {String} id
- * @param {String} [element] element the id is claimed by
- */
- Ids.prototype.claim = function(id, element) {
- this._seed.set(id, element || true);
- };
- /**
- * Returns true if the given id has already been assigned.
- *
- * @param {String} id
- * @return {Boolean}
- */
- Ids.prototype.assigned = function(id) {
- return this._seed.get(id) || false;
- };
- /**
- * Unclaim an id.
- *
- * @param {String} id the id to unclaim
- */
- Ids.prototype.unclaim = function(id) {
- delete this._seed.hats[id];
- };
- /**
- * Clear all claimed ids.
- */
- Ids.prototype.clear = function() {
- var hats = this._seed.hats,
- id;
- for (id in hats) {
- this.unclaim(id);
- }
- };
|