1 |
|
2 |
|
3 |
|
4 |
|
5 | 'use strict';
|
6 | const _index = Symbol('_index'),
|
7 | _prefix = Symbol('_prefix');
|
8 |
|
9 | class GenerateID {
|
10 | |
11 |
|
12 |
|
13 |
|
14 | constructor (prefix = `${(Date.now()/1000).toString(32)}`) {
|
15 | this[_index] = 0;
|
16 | this[_prefix] = prefix;
|
17 | }
|
18 |
|
19 | /*
|
20 | * Use ES6 generators to handle incrementation.
|
21 | */
|
22 | *gen () {
|
23 | yield `${this[_prefix]}-${this[_index]++}`;
|
24 | }
|
25 |
|
26 | /*
|
27 | * Get an incremented (prefixed) ID.
|
28 | */
|
29 | next () {
|
30 | return this.gen().next().value;
|
31 | }
|
32 |
|
33 | /*
|
34 | * Allow the class to have a custom object string tag.
|
35 | */
|
36 | get [Symbol.toStringTag]() {
|
37 | return (this.constructor.name || 'GenerateID');
|
38 | }
|
39 | }
|
40 |
|
41 | /*
|
42 | * Export the GenerateID class for use!
|
43 | */
|
44 | module.exports = GenerateID;
|
45 |
|
\ | No newline at end of file |