-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUUID.js
More file actions
35 lines (28 loc) · 746 Bytes
/
UUID.js
File metadata and controls
35 lines (28 loc) · 746 Bytes
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
31
32
33
34
35
// https://ja.wikipedia.org/wiki/UUID
// UUID version 4
import { rnd } from "./rnd.js";
const UUID = {};
UUID.generate = () => {
const chars = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".split("");
const len = chars.length;
for (let i = 0; i < len; i++) {
const c = chars[i];
if (c === "x") {
chars[i] = rnd(16).toString(16);
} else if (c === "y") {
chars[i] = (rnd(4) + 8).toString(16);
}
}
return chars.join("");
};
UUID.getVersion = () => {
return p;
};
UUID.isValid = (uuid) => {
const UUID_MATCHER =
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
return UUID_MATCHER.test(uuid);
};
// const n = UUID.generate();
// console.log(n, UUID.isValid(n));
export { UUID };