main
js 37 lines 748 Bytes
Raw
1 'use strict';
2
3 /* eslint-disable react-internal/safe-string-coercion */
4
5 //------------------------------------------------------------------------------
6 // Public Interface
7 //------------------------------------------------------------------------------
8
9 /**
10 * A generator for unique ids.
11 */
12 class IdGenerator {
13 /**
14 * @param {string} prefix Optional. A prefix of generated ids.
15 */
16 constructor(prefix) {
17 this.prefix = String(prefix);
18 this.n = 0;
19 }
20
21 /**
22 * Generates id.
23 * @returns {string} A generated id.
24 */
25 next() {
26 this.n = (1 + this.n) | 0;
27
28 /* c8 ignore start */
29 if (this.n < 0) {
30 this.n = 1;
31 } /* c8 ignore stop */
32
33 return this.prefix + this.n;
34 }
35 }
36
37 module.exports = IdGenerator;