id-generator.js 1008 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. * @fileoverview A class of identifiers generator for code path segments.
  3. *
  4. * Each rule uses the identifier of code path segments to store additional
  5. * information of the code path.
  6. *
  7. * @author Toru Nagashima
  8. */
  9. "use strict";
  10. //------------------------------------------------------------------------------
  11. // Public Interface
  12. //------------------------------------------------------------------------------
  13. /**
  14. * A generator for unique ids.
  15. */
  16. class IdGenerator {
  17. // eslint-disable-next-line jsdoc/require-description
  18. /**
  19. * @param {string} prefix Optional. A prefix of generated ids.
  20. */
  21. constructor(prefix) {
  22. this.prefix = String(prefix);
  23. this.n = 0;
  24. }
  25. /**
  26. * Generates id.
  27. * @returns {string} A generated id.
  28. */
  29. next() {
  30. this.n = 1 + this.n | 0;
  31. /* istanbul ignore if */
  32. if (this.n < 0) {
  33. this.n = 1;
  34. }
  35. return this.prefix + this.n;
  36. }
  37. }
  38. module.exports = IdGenerator;