-
-
Notifications
You must be signed in to change notification settings - Fork 914
/
Copy pathv6.ts
39 lines (33 loc) · 1.08 KB
/
v6.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
31
32
33
34
35
36
37
38
39
import { UUIDTypes, Version6Options } from './types.js';
import { unsafeStringify } from './stringify.js';
import v1 from './v1.js';
import v1ToV6 from './v1ToV6.js';
function v6(options?: Version6Options, buf?: undefined, offset?: number): string;
function v6<TBuf extends Uint8Array = Uint8Array>(
options: Version6Options | undefined,
buf: TBuf,
offset?: number
): TBuf;
function v6<TBuf extends Uint8Array = Uint8Array>(
options?: Version6Options,
buf?: TBuf,
offset?: number
): UUIDTypes<TBuf> {
options ??= {};
offset ??= 0;
// v6 is v1 with different field layout, so we start with a v1 UUID, albeit
// with slightly different behavior around how the clock_seq and node fields
// are randomized, which is why we call v1 with _v6: true.
let bytes = v1({ ...options, _v6: true }, new Uint8Array(16));
// Reorder the fields to v6 layout.
bytes = v1ToV6(bytes);
// Return as a byte array if requested
if (buf) {
for (let i = 0; i < 16; i++) {
buf[offset + i] = bytes[i];
}
return buf;
}
return unsafeStringify(bytes);
}
export default v6;