UNPKG

959 BJavaScriptView Raw
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
10"use strict";
11
12//------------------------------------------------------------------------------
13// Public Interface
14//------------------------------------------------------------------------------
15
16/**
17 * A generator for unique ids.
18 */
19class IdGenerator {
20
21 /**
22 * @param {string} prefix - Optional. A prefix of generated ids.
23 */
24 constructor(prefix) {
25 this.prefix = String(prefix);
26 this.n = 0;
27 }
28
29 /**
30 * Generates id.
31 *
32 * @returns {string} A generated id.
33 */
34 next() {
35 this.n = 1 + this.n | 0;
36
37 /* istanbul ignore if */
38 if (this.n < 0) {
39 this.n = 1;
40 }
41
42 return this.prefix + this.n;
43 }
44}
45
46module.exports = IdGenerator;