1 | const BASE = 16;
|
2 | let INDEX = 0;
|
3 |
|
4 | /**
|
5 | * Generate an identifier from
|
6 | * - customizable prefix
|
7 | * - hexadecimal timestap
|
8 | * - local incrementor
|
9 | * that is
|
10 | * - cheap
|
11 | * - platform agnostic
|
12 | * - unique enough to prevent accidental collision
|
13 | *
|
14 | * @param {string} [prefix=zs]
|
15 | * @return {string}
|
16 | */
|
17 | function unique(prefix = 'zs') {
|
18 | const hexadecimalTimestamp = new Date().getTime().toString(BASE);
|
19 | const index = INDEX++;
|
20 |
|
21 | return [prefix, hexadecimalTimestamp, index].join('-');
|
22 | }
|
23 |
|
24 | module.exports = unique;
|