IdGenerator.js 665 B

123456789101112131415161718192021222324252627
  1. /**
  2. * Util that provides unique IDs.
  3. *
  4. * @class djs.util.IdGenerator
  5. * @constructor
  6. * @memberOf djs.util
  7. *
  8. * The ids can be customized via a given prefix and contain a random value to avoid collisions.
  9. *
  10. * @param {String} prefix a prefix to prepend to generated ids (for better readability)
  11. */
  12. export default function IdGenerator(prefix) {
  13. this._counter = 0;
  14. this._prefix = (prefix ? prefix + '-' : '') + Math.floor(Math.random() * 1000000000) + '-';
  15. }
  16. /**
  17. * Returns a next unique ID.
  18. *
  19. * @method djs.util.IdGenerator#next
  20. *
  21. * @returns {String} the id
  22. */
  23. IdGenerator.prototype.next = function() {
  24. return this._prefix + (++this._counter);
  25. };