File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change
1
+ class ID {
2
+ // Generate an hex ID based on timestamp
3
+ // Recreated from https://www.php.net/manual/en/function.uniqid.php
4
+ static #hexTimestamp() {
5
+ const now = new Date();
6
+ const sec = Math.floor(now.getTime() / 1000);
7
+ const msec = now.getMilliseconds();
8
+
9
+ // Convert to hexadecimal
10
+ const hexTimestamp = sec.toString(16) + msec.toString(16).padStart(5, '0');
11
+ return hexTimestamp;
12
+ }
13
+
14
+ static custom(id) {
15
+ return id
16
+ }
17
+
18
+ static unique(padding = 7) {
19
+ // Generate a unique ID with padding to have a longer ID
20
+ const baseId = ID.#hexTimestamp();
21
+ let randomPadding = '';
22
+ for (let i = 0; i < padding; i++) {
23
+ const randomHexDigit = Math.floor(Math.random() * 16).toString(16);
24
+ randomPadding += randomHexDigit;
25
+ }
26
+ return baseId + randomPadding;
27
+ }
28
+ }
29
+
30
+ module.exports = ID;
You can’t perform that action at this time.
0 commit comments