UNPKG

509 BJavaScriptView Raw
1const BASE = 16;
2let 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 */
17function unique(prefix = 'zs') {
18 const hexadecimalTimestamp = new Date().getTime().toString(BASE);
19 const index = INDEX++;
20
21 return [prefix, hexadecimalTimestamp, index].join('-');
22}
23
24module.exports = unique;