@sdxc/uuid 2026.9.14 → 2026.9.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -1
- package/dist/index.d.ts +11 -0
- package/dist/index.js +29 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -116,7 +116,17 @@ characters, and `InvalidUUIDFormatError` for 36 characters in the wrong shape.
|
|
|
116
116
|
|
|
117
117
|
Returns a new random UUID from `crypto.randomUUID()`, already narrowed to `UUID`. That is a
|
|
118
118
|
version 4 UUID as defined by [RFC 9562](https://www.rfc-editor.org/rfc/rfc9562), random in
|
|
119
|
-
every position the format leaves free
|
|
119
|
+
every position the format leaves free — including when it was created, so two values carry
|
|
120
|
+
no ordering.
|
|
121
|
+
|
|
122
|
+
### `generateUUIDv7(): UUID`
|
|
123
|
+
|
|
124
|
+
Returns a new version 7 UUID: a 48-bit millisecond timestamp followed by 74 random bits, so
|
|
125
|
+
values sort the way they were created. Use this one where
|
|
126
|
+
[`@sdxc/typeid`](https://www.npmjs.com/package/@sdxc/typeid) is going to prefix the result
|
|
127
|
+
and something reads that prefixed id's order — a paginated listing, a table with no separate
|
|
128
|
+
`created_at` to sort by. Reach for `generateUUID` instead when the id must not reveal when it
|
|
129
|
+
was minted, such as a value handed to someone outside the system.
|
|
120
130
|
|
|
121
131
|
### Errors
|
|
122
132
|
|
package/dist/index.d.ts
CHANGED
|
@@ -10,4 +10,15 @@ export type UUID = BaseUUID & {
|
|
|
10
10
|
export declare function isUUID(value: string): value is UUID;
|
|
11
11
|
export declare function assertUUID(value: string): asserts value is UUID;
|
|
12
12
|
export declare function generateUUID(): UUID;
|
|
13
|
+
/**
|
|
14
|
+
* Generates a UUID version 7: a 48-bit big-endian Unix millisecond timestamp
|
|
15
|
+
* followed by 74 random bits, so values sort the way they were created without
|
|
16
|
+
* a separate ordering column.
|
|
17
|
+
*
|
|
18
|
+
* @returns A UUIDv7, narrowed to {@link UUID}.
|
|
19
|
+
* @example
|
|
20
|
+
* let id = generateUUIDv7();
|
|
21
|
+
* // "018f4d2e-...": sorts after a UUIDv7 minted a millisecond earlier
|
|
22
|
+
*/
|
|
23
|
+
export declare function generateUUIDv7(): UUID;
|
|
13
24
|
export { InvalidUUIDFormatError, InvalidUUIDLengthError, InvalidUUIDTypeError };
|
package/dist/index.js
CHANGED
|
@@ -26,4 +26,33 @@ export function generateUUID() {
|
|
|
26
26
|
assertUUID(id);
|
|
27
27
|
return id;
|
|
28
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* Generates a UUID version 7: a 48-bit big-endian Unix millisecond timestamp
|
|
31
|
+
* followed by 74 random bits, so values sort the way they were created without
|
|
32
|
+
* a separate ordering column.
|
|
33
|
+
*
|
|
34
|
+
* @returns A UUIDv7, narrowed to {@link UUID}.
|
|
35
|
+
* @example
|
|
36
|
+
* let id = generateUUIDv7();
|
|
37
|
+
* // "018f4d2e-...": sorts after a UUIDv7 minted a millisecond earlier
|
|
38
|
+
*/
|
|
39
|
+
export function generateUUIDv7() {
|
|
40
|
+
let timestamp = BigInt(Date.now());
|
|
41
|
+
let random = crypto.getRandomValues(new Uint8Array(10));
|
|
42
|
+
let bytes = new Uint8Array(16);
|
|
43
|
+
bytes[0] = Number((timestamp >> 40n) & 0xffn);
|
|
44
|
+
bytes[1] = Number((timestamp >> 32n) & 0xffn);
|
|
45
|
+
bytes[2] = Number((timestamp >> 24n) & 0xffn);
|
|
46
|
+
bytes[3] = Number((timestamp >> 16n) & 0xffn);
|
|
47
|
+
bytes[4] = Number((timestamp >> 8n) & 0xffn);
|
|
48
|
+
bytes[5] = Number(timestamp & 0xffn);
|
|
49
|
+
bytes[6] = 0x70 | (random[0] & 0x0f);
|
|
50
|
+
bytes[7] = random[1];
|
|
51
|
+
bytes[8] = 0x80 | (random[2] & 0x3f);
|
|
52
|
+
bytes.set(random.subarray(3), 9);
|
|
53
|
+
let hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
54
|
+
let id = `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
55
|
+
assertUUID(id);
|
|
56
|
+
return id;
|
|
57
|
+
}
|
|
29
58
|
export { InvalidUUIDFormatError, InvalidUUIDLengthError, InvalidUUIDTypeError };
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sdxc/uuid",
|
|
3
|
-
"version": "2026.9.
|
|
3
|
+
"version": "2026.9.23",
|
|
4
4
|
"description": "Validate, assert and generate UUIDs behind a branded UUID type",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"exports": {
|
|
8
8
|
".": "./dist/index.js"
|
|
9
9
|
},
|
|
10
|
-
"gitHead": "
|
|
10
|
+
"gitHead": "00c5691b2e444a348956d1879ab676b07d3f9c8f",
|
|
11
11
|
"publishConfig": {
|
|
12
12
|
"access": "public"
|
|
13
13
|
},
|