-
Notifications
You must be signed in to change notification settings - Fork 9
/
uid.ts
30 lines (26 loc) · 878 Bytes
/
uid.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const SEQUENCE_KEY: unique symbol = ((window.Symbol || String)('__esl_sequences__')) as any;
const ns = window || global;
const sequences = ns[SEQUENCE_KEY] || new Map<string, number>();
ns[SEQUENCE_KEY] = sequences;
declare global {
interface Window {
[SEQUENCE_KEY]: Map<string, number>;
}
}
/** Create and return sequential id */
export const sequentialUID = (name: string, prefix: string = name): string => {
const uid = (sequences.get(name) || 0) + 1;
sequences.set(name, uid);
return prefix + uid;
};
/** Return random unique identifier */
export const randUID = (prefix: string = ''): string => {
const time = Date.now().toString(32);
const rand = Math.round(Math.random() * 1024 * 1024).toString(32);
return prefix + time + '-' + rand;
};
/**
* Generate unique id
* @deprecated Alias for {@link randUID}
*/
export const generateUId = randUID;