UNPKG

1.18 kBJavaScriptView Raw
1/*!
2 * A MongoDB inspired ES6 Map() query language. - Copyright (c) 2017 Louis T. (https://lou.ist/)
3 * Licensed under the MIT license https://raw.githubusercontent.com/LouisT/MapQL/master/LICENSE
4 */
5'use strict';
6const _index = Symbol('_index'),
7 _prefix = Symbol('_prefix');
8
9class GenerateID {
10 /*
11 * The default prefix is the current UNIX timestamp, converted to a string with
12 * a radix of 32. You can reverse the string with parseInt(<String>, 32) if needed.
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 */
44module.exports = GenerateID;
45
\No newline at end of file