@ac-kit/format-uuid 0.0.0-stage → 0.1.0

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/CHANGELOG.md ADDED
@@ -0,0 +1,31 @@
1
+ # @ac-kit/format-uuid
2
+
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 2a0fe74: First release: UUIDs as RFC 9562 defines them. Needs the `Temporal` global (Node 26, or a polyfill on Node 24) for `getUuidV7Timestamp` and the generator's default clock.
8
+
9
+ - `parseUuid` reads 8-4-4-4-12 text of either case into 16 bytes and throws `UuidSyntaxError` (a `SourceSyntaxError`) with the offset of the fault (`tryParseUuid` answers `null`, `isUuid` answers a boolean); `printUuid` writes lowercase text, with the `urn:uuid:` prefix on request. Printed text sorts as the bytes do.
10
+ - `getUuidVersion`, `getUuidVariant` and `compareUuids` take text or bytes; `NIL_UUID` and `MAX_UUID` are the two special values.
11
+ - `createUuidV7Generator({ now?, randomBytes? })` returns a function producing UUIDv7 text that is strictly increasing in value and in text, whatever the clock does: ids within a millisecond are told apart by a counter, a clock that stands still or goes back keeps the last millisecond, and an exhausted counter moves on by one millisecond. None of these throws. The function throws `RangeError`, leaving the generator exactly as it was, when the clock reads outside the 48-bit Unix millisecond range while ahead of the last id, when `randomBytes` returns a different number of bytes than asked for, or when the timestamp has no successor in 48 bits. `now` is an `InstantSource` returning a `Temporal.Instant`, the system clock by default.
12
+ - `getUuidV7Timestamp` returns a UUIDv7's creation time as a `Temporal.Instant` and throws `UuidVersionError` for any other UUID, including a version 7 one of another variant than RFC 9562.
13
+
14
+ ### Patch Changes
15
+
16
+ - Updated dependencies [2a0fe74]
17
+ - Updated dependencies [5152230]
18
+ - Updated dependencies [2a0fe74]
19
+ - Updated dependencies [5152230]
20
+ - Updated dependencies [5152230]
21
+ - Updated dependencies [2a0fe74]
22
+ - Updated dependencies [2a0fe74]
23
+ - Updated dependencies [5152230]
24
+ - Updated dependencies [5152230]
25
+ - Updated dependencies [5152230]
26
+ - Updated dependencies [5152230]
27
+ - Updated dependencies [5152230]
28
+ - Updated dependencies [5152230]
29
+ - Updated dependencies [5152230]
30
+ - @ac-kit/core@0.10.0
31
+ - @ac-kit/crypto-random@0.2.2
package/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2024-2026 Anthony Champagne and contributors
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md CHANGED
@@ -1,3 +1,114 @@
1
- # Temporary Holding Version
1
+ # @ac-kit/format-uuid
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ UUIDs as RFC 9562 defines them: read and write the text form, inspect the
4
+ version and variant, compare, and generate version 7 identifiers that sort by
5
+ creation time.
6
+
7
+ ```ts
8
+ import {
9
+ createUuidV7Generator,
10
+ getUuidV7Timestamp,
11
+ parseUuid,
12
+ printUuid,
13
+ } from "@ac-kit/format-uuid";
14
+
15
+ const nextId = createUuidV7Generator();
16
+
17
+ const first = nextId(); // "01926f3c-8a2e-7b41-9d05-3c7e5a8f1b20"
18
+ const second = nextId();
19
+ first < second; // true, always: the text sorts in creation order
20
+
21
+ getUuidV7Timestamp(first); // Temporal.Instant, to the millisecond
22
+ printUuid(parseUuid("017F22E2-79B0-7CC3-98C4-DC0C0C07398F"));
23
+ // "017f22e2-79b0-7cc3-98c4-dc0c0c07398f"
24
+ ```
25
+
26
+ A UUID is handled as **text** (36 characters, 8-4-4-4-12) or as its **16
27
+ bytes**. The functions that inspect one — `getUuidVersion`, `getUuidVariant`,
28
+ `compareUuids`, `getUuidV7Timestamp` — take either.
29
+
30
+ ## Requirements
31
+
32
+ **Needs the `Temporal` global**: `getUuidV7Timestamp` returns a
33
+ `Temporal.Instant`, and `createUuidV7Generator` reads a `Temporal.Instant` from
34
+ its clock. Node 26 ships `Temporal`; on Node 24 (this package's `engines` allow
35
+ `^24.19.0 || >=26.5.0`) load a polyfill such as
36
+ [`temporal-polyfill`](https://www.npmjs.com/package/temporal-polyfill) first.
37
+
38
+ ## Text and bytes
39
+
40
+ - `isUuid(value)` — whether a value is text in the 8-4-4-4-12 form, in either
41
+ case. It returns a plain `boolean` and never narrows, so a string that fails
42
+ the check stays a `string` in your `else` branch.
43
+ - `parseUuid(text)` — the 16 bytes; any case, any version and variant. Throws
44
+ `UuidSyntaxError` carrying the offset it gave up at; `tryParseUuid` answers
45
+ `null` instead. `parseUuid(text, { urn: true })` reads the `urn:uuid:` form.
46
+ - `printUuid(bytes, { urn? })` — lowercase text, which sorts as the bytes do.
47
+ - `NIL_UUID` and `MAX_UUID` — all bits zero, all bits one.
48
+
49
+ ## Inspecting
50
+
51
+ - `getUuidVersion(uuid)` — the version nibble, 0 to 15.
52
+ - `getUuidVariant(uuid)` — `"rfc9562"` for UUIDs of the RFC's layout,
53
+ `"ncs"`, `"microsoft"` or `"future"` for the others.
54
+ - `compareUuids(a, b)` — orders by the bytes as an unsigned 128-bit integer.
55
+ Text of either case and bytes can be mixed.
56
+
57
+ ## Version 7
58
+
59
+ `createUuidV7Generator({ now?, randomBytes? })` returns a function producing a
60
+ new UUIDv7 text per call. Each id carries a 48-bit Unix millisecond timestamp,
61
+ a 42-bit counter and 32 random bits, so ids made by one generator are
62
+ **strictly increasing**, in value and in text, however many are made in one
63
+ millisecond, and even when the clock stands still or goes backwards. That makes
64
+ them good primary keys: inserts land at the end of an index, and a sort by id is
65
+ a sort by creation. The counter running out moves the timestamp on by one
66
+ millisecond rather than throwing.
67
+
68
+ The ordering holds within one generator. Ids from two generators, or two
69
+ processes, agree by millisecond but are not ordered within it, and the
70
+ timestamp is the clock's reading, so do not treat it as proof of when
71
+ something happened.
72
+
73
+ The returned function throws `RangeError`, leaving the generator exactly as it
74
+ was, when:
75
+
76
+ - the clock reads outside the 48-bit Unix millisecond range (below 0, or 2^48
77
+ and above) while it is ahead of the last id: a clock that stands still or goes
78
+ back never throws, but a negative reading before the first id does;
79
+ - `randomBytes` returns a different number of bytes than asked for (6 when a
80
+ millisecond starts, 4 for every id);
81
+ - the counter is exhausted and the timestamp has no successor in 48 bits.
82
+
83
+ `getUuidV7Timestamp(uuid)` returns the creation time as a `Temporal.Instant`,
84
+ and throws `UuidVersionError` when the UUID is not a version 7 one, or is a
85
+ version 7 one of another variant than RFC 9562.
86
+
87
+ For tests, inject `now` (an `InstantSource`: a function returning a
88
+ `Temporal.Instant`) and `randomBytes` (a function taking a length and returning
89
+ that many bytes). `@ac-kit/test-util`'s `FakeWallClock` is a clock you set and
90
+ advance, backwards included, and `@ac-kit/math-random`'s `randomBytes` turns a
91
+ seeded generator into reproducible bytes:
92
+
93
+ ```ts
94
+ import { pcg32, randomBytes } from "@ac-kit/math-random";
95
+ import { FakeWallClock } from "@ac-kit/test-util";
96
+
97
+ const clock = new FakeWallClock(1_700_000_000_000);
98
+ const random = pcg32(1);
99
+
100
+ const nextId = createUuidV7Generator({
101
+ now: clock.instant,
102
+ randomBytes: (length) => randomBytes(random, length),
103
+ });
104
+ ```
105
+
106
+ ## Random UUIDs
107
+
108
+ For a random (version 4) UUID, use the platform's `crypto.randomUUID()`. It is
109
+ available in every runtime this package supports, and a wrapper would add
110
+ nothing.
111
+
112
+ See the
113
+ [generated API docs](https://anthochamp.pages.grenadine.io/node-essentials/api/format-uuid/)
114
+ for the full reference.
@@ -0,0 +1,4 @@
1
+ import { type UuidInput } from "./uuid.js";
2
+ /** The 16 bytes of `uuid`: a byte input is used as is, never copied. */
3
+ export declare function toUuidBytes(uuid: UuidInput): Uint8Array;
4
+ //# sourceMappingURL=_uuid-bytes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_uuid-bytes.d.ts","sourceRoot":"","sources":["../src/_uuid-bytes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,KAAK,SAAS,EAAE,MAAM,WAAW,CAAC;AAItD,wEAAwE;AACxE,wBAAgB,WAAW,CAAC,IAAI,EAAE,SAAS,GAAG,UAAU,CAUvD"}
@@ -0,0 +1,13 @@
1
+ import { parseUuid } from "./uuid.js";
2
+ const BYTE_LENGTH = 16;
3
+ /** The 16 bytes of `uuid`: a byte input is used as is, never copied. */
4
+ export function toUuidBytes(uuid) {
5
+ if (typeof uuid === "string") {
6
+ return parseUuid(uuid);
7
+ }
8
+ if (uuid.length !== BYTE_LENGTH) {
9
+ throw new RangeError(`expected 16 UUID bytes, got ${uuid.length}`);
10
+ }
11
+ return uuid;
12
+ }
13
+ //# sourceMappingURL=_uuid-bytes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_uuid-bytes.js","sourceRoot":"","sources":["../src/_uuid-bytes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAkB,MAAM,WAAW,CAAC;AAEtD,MAAM,WAAW,GAAG,EAAE,CAAC;AAEvB,wEAAwE;AACxE,MAAM,UAAU,WAAW,CAAC,IAAe;IAC1C,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,SAAS,CAAC,IAAI,CAAC,CAAC;IACxB,CAAC;IAED,IAAI,IAAI,CAAC,MAAM,KAAK,WAAW,EAAE,CAAC;QACjC,MAAM,IAAI,UAAU,CAAC,+BAA+B,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;IACpE,CAAC;IAED,OAAO,IAAI,CAAC;AACb,CAAC"}
@@ -0,0 +1,10 @@
1
+ import { type Callable, type CallableNoArgs, type InstantSource } from "@ac-kit/core";
2
+ /** The counter has 42 bits, so it can take `2 ** 42` values. */
3
+ export declare const UUID_V7_COUNTER_LIMIT: number;
4
+ /**
5
+ * The monotonic generator behind `createUuidV7Generator`, with the counter's
6
+ * range a parameter so a test can exhaust it: at the real 2^42 it is out of
7
+ * reach, 2^41 ids in one millisecond.
8
+ */
9
+ export declare function createMonotonicUuidV7Generator(now: InstantSource, randomBytes: Callable<[length: number], Uint8Array>, counterLimit: number): CallableNoArgs<string>;
10
+ //# sourceMappingURL=_uuid-v7-generator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_uuid-v7-generator.d.ts","sourceRoot":"","sources":["../src/_uuid-v7-generator.ts"],"names":[],"mappings":"AAAA,OAAO,EAKN,KAAK,QAAQ,EACb,KAAK,cAAc,EACnB,KAAK,aAAa,EAClB,MAAM,cAAc,CAAC;AAUtB,gEAAgE;AAChE,eAAO,MAAM,qBAAqB,QAAU,CAAC;AAiB7C;;;;GAIG;AACH,wBAAgB,8BAA8B,CAC7C,GAAG,EAAE,aAAa,EAClB,WAAW,EAAE,QAAQ,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,UAAU,CAAC,EACnD,YAAY,EAAE,MAAM,GAClB,cAAc,CAAC,MAAM,CAAC,CAqDxB"}
@@ -0,0 +1,66 @@
1
+ import { getUint48Be, isIntegerInRange, setUint32Be, setUint48Be, } from "@ac-kit/core";
2
+ import { printUuid } from "./uuid.js";
3
+ const MILLISECONDS_LIMIT = 2 ** 48;
4
+ // 12 bits of `rand_a`, then the high bits of `rand_b`.
5
+ const COUNTER_LOW_LIMIT = 2 ** 30;
6
+ const SEED_BYTE_LENGTH = 6;
7
+ const TAIL_BYTE_LENGTH = 4;
8
+ /** The counter has 42 bits, so it can take `2 ** 42` values. */
9
+ export const UUID_V7_COUNTER_LIMIT = 2 ** 42;
10
+ function takeRandomBytes(randomBytes, length) {
11
+ const bytes = randomBytes(length);
12
+ if (bytes.length !== length) {
13
+ throw new RangeError(`randomBytes(${length}) returned ${bytes.length} bytes`);
14
+ }
15
+ return bytes;
16
+ }
17
+ /**
18
+ * The monotonic generator behind `createUuidV7Generator`, with the counter's
19
+ * range a parameter so a test can exhaust it: at the real 2^42 it is out of
20
+ * reach, 2^41 ids in one millisecond.
21
+ */
22
+ export function createMonotonicUuidV7Generator(now, randomBytes, counterLimit) {
23
+ const bytes = new Uint8Array(16);
24
+ const seedLimit = counterLimit / 2;
25
+ let lastMilliseconds = Number.NEGATIVE_INFINITY;
26
+ let counter = 0;
27
+ function seedCounter() {
28
+ return (getUint48Be(takeRandomBytes(randomBytes, SEED_BYTE_LENGTH), 0) % seedLimit);
29
+ }
30
+ return () => {
31
+ const milliseconds = now().epochMilliseconds;
32
+ let nextMilliseconds = lastMilliseconds;
33
+ let nextCounter = counter;
34
+ // Every assignment to the generator's state waits for the last throwing
35
+ // call, so a failure leaves it as it was.
36
+ if (milliseconds > lastMilliseconds) {
37
+ if (!isIntegerInRange(milliseconds, 0, MILLISECONDS_LIMIT)) {
38
+ throw new RangeError("createUuidV7Generator: the clock is outside the 48-bit Unix millisecond range");
39
+ }
40
+ nextCounter = seedCounter();
41
+ nextMilliseconds = milliseconds;
42
+ }
43
+ else if (counter + 1 < counterLimit) {
44
+ nextCounter += 1;
45
+ }
46
+ else if (lastMilliseconds + 1 < MILLISECONDS_LIMIT) {
47
+ nextCounter = seedCounter();
48
+ nextMilliseconds += 1;
49
+ }
50
+ else {
51
+ throw new RangeError("createUuidV7Generator: the timestamp has no successor in 48 bits");
52
+ }
53
+ const tail = takeRandomBytes(randomBytes, TAIL_BYTE_LENGTH);
54
+ const randomA = Math.floor(nextCounter / COUNTER_LOW_LIMIT);
55
+ const randomBHigh = nextCounter % COUNTER_LOW_LIMIT;
56
+ setUint48Be(bytes, 0, nextMilliseconds);
57
+ bytes[6] = 0x70 | (randomA >>> 8);
58
+ bytes[7] = randomA & 0xff;
59
+ setUint32Be(bytes, 8, 2147483648 | randomBHigh);
60
+ bytes.set(tail, 12);
61
+ lastMilliseconds = nextMilliseconds;
62
+ counter = nextCounter;
63
+ return printUuid(bytes);
64
+ };
65
+ }
66
+ //# sourceMappingURL=_uuid-v7-generator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_uuid-v7-generator.js","sourceRoot":"","sources":["../src/_uuid-v7-generator.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,WAAW,EACX,gBAAgB,EAChB,WAAW,EACX,WAAW,GAIX,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEtC,MAAM,kBAAkB,GAAG,CAAC,IAAI,EAAE,CAAC;AACnC,uDAAuD;AACvD,MAAM,iBAAiB,GAAG,CAAC,IAAI,EAAE,CAAC;AAClC,MAAM,gBAAgB,GAAG,CAAC,CAAC;AAC3B,MAAM,gBAAgB,GAAG,CAAC,CAAC;AAE3B,gEAAgE;AAChE,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,IAAI,EA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C,WAAW,IAAI,CAAC,CAAC;QAClB,CAAC;aAAM,IAAI,gBAAgB,GAAG,CAAC,GAAG,kBAAkB,EAAE,CAAC;YACtD,WAAW,GAAG,WAAW,EAAE,CAAC;YAC5B,gBAAgB,IAAI,CAAC,CAAC;QACvB,CAAC;aAAM,CAAC;YACP,MAAM,IAAI,UAAU,CACnB,kEAAkE,CAClE,CAAC;QACH,CAAC;QAED,MAAM,IAAI,GAAG,eAAe,CAAC,WAAW,EAAE,gBAAgB,CAAC,CAAC;QAC5D,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,WAAW,GAAG,iBAAiB,CAAC,CAAC;QAC5D,MAAM,WAAW,GAAG,WAAW,GAAG,iBAAiB,CAAC;QAEpD,WAAW,CAAC,KAAK,EAAE,CAAC,EAAE,gBAAgB,CAAC,CAAC;QACxC,KAAK,CAAC,CAAC,CAAC,GAAG,IAAI,GAAG,CAAC,OAAO,KAAK,CAAC,CAAC,CAAC;QAClC,KAAK,CAAC,CAAC,CAAC,GAAG,OAAO,GAAG,IAAI,CAAC;QAC1B,WAAW,CAAC,KAAK,EAAE,CAAC,EAAE,UAAa,GAAG,WAAW,CAAC,CAAC;QACnD,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;QACpB,gBAAgB,GAAG,gBAAgB,CAAC;QACpC,OAAO,GAAG,WAAW,CAAC;QAEtB,OAAO,SAAS,CAAC,KAAK,CAAC,CAAC;IACzB,CAAC,CAAC;AACH,CAAC"}
@@ -0,0 +1,25 @@
1
+ import { SourceSyntaxError } from "@ac-kit/core";
2
+ /**
3
+ * Text that is not a UUID in the 8-4-4-4-12 form, with the offset the parse
4
+ * gave up at.
5
+ */
6
+ export declare class UuidSyntaxError extends SourceSyntaxError {
7
+ readonly name = "UuidSyntaxError";
8
+ }
9
+ /** A UUID that is not of the version, or not of the variant, an operation needs. */
10
+ export declare class UuidVersionError extends TypeError {
11
+ /** The version the operation needs. */
12
+ readonly expectedVersion: number;
13
+ /** The UUID's version nibble. */
14
+ readonly version: number;
15
+ /** The UUID's variant. */
16
+ readonly variant: string;
17
+ constructor(
18
+ /** The version the operation needs. */
19
+ expectedVersion: number,
20
+ /** The UUID's version nibble. */
21
+ version: number,
22
+ /** The UUID's variant. */
23
+ variant: string);
24
+ }
25
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEjD;;;GAGG;AACH,qBAAa,eAAgB,SAAQ,iBAAiB;IACrD,SAAkB,IAAI,qBAAqB;CAC3C;AAED,oFAAoF;AACpF,qBAAa,gBAAiB,SAAQ,SAAS;IAE7C,uCAAuC;IACvC,QAAQ,CAAC,eAAe,EAAE,MAAM;IAChC,iCAAiC;IACjC,QAAQ,CAAC,OAAO,EAAE,MAAM;IACxB,0BAA0B;IAC1B,QAAQ,CAAC,OAAO,EAAE,MAAM;IANzB;IACC,uCAAuC;IAC9B,eAAe,EAAE,MAAM;IAChC,iCAAiC;IACxB,OAAO,EAAE,MAAM;IACxB,0BAA0B;IACjB,OAAO,EAAE,MAAM,EAMxB;CACD"}
package/dist/errors.js ADDED
@@ -0,0 +1,28 @@
1
+ import { SourceSyntaxError } from "@ac-kit/core";
2
+ /**
3
+ * Text that is not a UUID in the 8-4-4-4-12 form, with the offset the parse
4
+ * gave up at.
5
+ */
6
+ export class UuidSyntaxError extends SourceSyntaxError {
7
+ name = "UuidSyntaxError";
8
+ }
9
+ /** A UUID that is not of the version, or not of the variant, an operation needs. */
10
+ export class UuidVersionError extends TypeError {
11
+ expectedVersion;
12
+ version;
13
+ variant;
14
+ constructor(
15
+ /** The version the operation needs. */
16
+ expectedVersion,
17
+ /** The UUID's version nibble. */
18
+ version,
19
+ /** The UUID's variant. */
20
+ variant) {
21
+ super(`expected a version ${expectedVersion} UUID of the RFC 9562 variant, found version ${version} of variant "${variant}"`);
22
+ this.expectedVersion = expectedVersion;
23
+ this.version = version;
24
+ this.variant = variant;
25
+ this.name = "UuidVersionError";
26
+ }
27
+ }
28
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEjD;;;GAGG;AACH,MAAM,OAAO,eAAgB,SAAQ,iBAAiB;IACnC,IAAI,GAAG,iBAAiB,CAAC;CAC3C;AAED,oFAAoF;AACpF,MAAM,OAAO,gBAAiB,SAAQ,SAAS;IAGpC,eAAe;IAEf,OAAO;IAEP,OAAO;IANjB;IACC,uCAAuC;IAC9B,eAAuB;IAChC,iCAAiC;IACxB,OAAe;IACxB,0BAA0B;IACjB,OAAe;QAExB,KAAK,CACJ,sBAAsB,eAAe,gDAAgD,OAAO,gBAAgB,OAAO,GAAG,CACtH,CAAC;+BARO,eAAe;uBAEf,OAAO;uBAEP,OAAO;QAKhB,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAC;IAChC,CAAC;CACD"}
@@ -0,0 +1,5 @@
1
+ export * from "./errors.js";
2
+ export * from "./uuid.js";
3
+ export * from "./uuid-fields.js";
4
+ export * from "./uuid-v7.js";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export * from "./errors.js";
2
+ export * from "./uuid.js";
3
+ export * from "./uuid-fields.js";
4
+ export * from "./uuid-v7.js";
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC"}
@@ -0,0 +1,35 @@
1
+ import { type ComparatorResult } from "@ac-kit/core";
2
+ import type { UuidInput } from "./uuid.js";
3
+ /**
4
+ * Which layout the rest of a UUID follows, from the top bits of its ninth byte:
5
+ * `"rfc9562"` for the layout of RFC 9562 (and RFC 4122), `"ncs"` and
6
+ * `"microsoft"` for the older reserved ones, `"future"` for the reserved one
7
+ * the max UUID carries.
8
+ */
9
+ export type UuidVariant = "ncs" | "rfc9562" | "microsoft" | "future";
10
+ /**
11
+ * The version nibble, 0 to 15: the 4 high bits of the seventh byte. It is
12
+ * meaningful only for the `"rfc9562"` variant; the nil UUID reads 0 and the max
13
+ * UUID 15 without being of those versions.
14
+ *
15
+ * @throws {UuidSyntaxError} When text is not a UUID.
16
+ * @throws {RangeError} When bytes are not 16 long.
17
+ */
18
+ export declare function getUuidVersion(uuid: UuidInput): number;
19
+ /**
20
+ * The variant, from the 1 to 3 high bits of the ninth byte.
21
+ *
22
+ * @throws {UuidSyntaxError} When text is not a UUID.
23
+ * @throws {RangeError} When bytes are not 16 long.
24
+ */
25
+ export declare function getUuidVariant(uuid: UuidInput): UuidVariant;
26
+ /**
27
+ * Orders UUIDs by their bytes, as the unsigned 128-bit integers they are. Text
28
+ * and bytes mix freely, and text case is ignored. For the UUIDv7 text of one
29
+ * generator this is creation order.
30
+ *
31
+ * @throws {UuidSyntaxError} When text is not a UUID.
32
+ * @throws {RangeError} When bytes are not 16 long.
33
+ */
34
+ export declare function compareUuids(a: UuidInput, b: UuidInput): ComparatorResult;
35
+ //# sourceMappingURL=uuid-fields.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uuid-fields.d.ts","sourceRoot":"","sources":["../src/uuid-fields.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,KAAK,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAGnE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAE3C;;;;;GAKG;AACH,MAAM,MAAM,WAAW,GAAG,KAAK,GAAG,SAAS,GAAG,WAAW,GAAG,QAAQ,CAAC;AAErE;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,SAAS,GAAG,MAAM,CAEtD;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,SAAS,GAAG,WAAW,CAc3D;AAED;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,SAAS,GAAG,gBAAgB,CAEzE"}
@@ -0,0 +1,44 @@
1
+ import { compareBytes } from "@ac-kit/core";
2
+ import { toUuidBytes } from "./_uuid-bytes.js";
3
+ /**
4
+ * The version nibble, 0 to 15: the 4 high bits of the seventh byte. It is
5
+ * meaningful only for the `"rfc9562"` variant; the nil UUID reads 0 and the max
6
+ * UUID 15 without being of those versions.
7
+ *
8
+ * @throws {UuidSyntaxError} When text is not a UUID.
9
+ * @throws {RangeError} When bytes are not 16 long.
10
+ */
11
+ export function getUuidVersion(uuid) {
12
+ return toUuidBytes(uuid)[6] >> 4;
13
+ }
14
+ /**
15
+ * The variant, from the 1 to 3 high bits of the ninth byte.
16
+ *
17
+ * @throws {UuidSyntaxError} When text is not a UUID.
18
+ * @throws {RangeError} When bytes are not 16 long.
19
+ */
20
+ export function getUuidVariant(uuid) {
21
+ const byte = toUuidBytes(uuid)[8];
22
+ if ((byte & 0x80) === 0) {
23
+ return "ncs";
24
+ }
25
+ if ((byte & 0xc0) === 0x80) {
26
+ return "rfc9562";
27
+ }
28
+ if ((byte & 0xe0) === 0xc0) {
29
+ return "microsoft";
30
+ }
31
+ return "future";
32
+ }
33
+ /**
34
+ * Orders UUIDs by their bytes, as the unsigned 128-bit integers they are. Text
35
+ * and bytes mix freely, and text case is ignored. For the UUIDv7 text of one
36
+ * generator this is creation order.
37
+ *
38
+ * @throws {UuidSyntaxError} When text is not a UUID.
39
+ * @throws {RangeError} When bytes are not 16 long.
40
+ */
41
+ export function compareUuids(a, b) {
42
+ return compareBytes(toUuidBytes(a), toUuidBytes(b));
43
+ }
44
+ //# sourceMappingURL=uuid-fields.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uuid-fields.js","sourceRoot":"","sources":["../src/uuid-fields.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAyB,MAAM,cAAc,CAAC;AAEnE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAW/C;;;;;;;GAOG;AACH,MAAM,UAAU,cAAc,CAAC,IAAe;IAC7C,OAAO,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,CAAE,IAAI,CAAC,CAAC;AACnC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAC,IAAe;IAC7C,MAAM,IAAI,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,CAAE,CAAC;IAEnC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO,KAAK,CAAC;IACd,CAAC;IACD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC;QAC5B,OAAO,SAAS,CAAC;IAClB,CAAC;IACD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC;QAC5B,OAAO,WAAW,CAAC;IACpB,CAAC;IAED,OAAO,QAAQ,CAAC;AACjB,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,YAAY,CAAC,CAAY,EAAE,CAAY;IACtD,OAAO,YAAY,CAAC,WAAW,CAAC,CAAC,CAAC,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC;AACrD,CAAC"}
@@ -0,0 +1,48 @@
1
+ import { type Callable, type CallableNoArgs, type InstantSource } from "@ac-kit/core";
2
+ import type { UuidInput } from "./uuid.js";
3
+ export type UuidV7GeneratorOptions = {
4
+ /** The wall clock. Default: `SYSTEM_INSTANT_SOURCE` from `@ac-kit/core`. */
5
+ readonly now?: InstantSource;
6
+ /**
7
+ * Cryptographically secure random bytes. Default: `getRandomBytes` from
8
+ * `@ac-kit/crypto-random`. Called with 4 for every id and with 6 when a new
9
+ * millisecond starts; it must return exactly that many.
10
+ */
11
+ readonly randomBytes?: Callable<[length: number], Uint8Array>;
12
+ };
13
+ /**
14
+ * Creates a generator of UUIDv7 text (RFC 9562 §5.7) that is strictly
15
+ * increasing, in value and in text, however fast it is called and whatever the
16
+ * clock does: the monotonic method of §6.2 with a fixed-length counter.
17
+ *
18
+ * Each id is the 48-bit Unix millisecond timestamp, the version and variant
19
+ * bits, a 42-bit counter and 32 fresh random bits. The counter starts at a
20
+ * random value below 2^41 in every new millisecond and grows by one per id
21
+ * within it. When it runs out the timestamp moves to the next millisecond; when
22
+ * the clock stands still or goes back the last millisecond is kept. Neither
23
+ * case throws, and the timestamp may then run ahead of the clock.
24
+ *
25
+ * Monotonic within one generator only: two generators — two processes — agree
26
+ * on the order by millisecond, not within it. Each id costs constant time and
27
+ * one `randomBytes` call, plus one more when a new millisecond starts.
28
+ *
29
+ * @example
30
+ * const nextId = createUuidV7Generator();
31
+ * nextId(); // "01926f3c-8a2e-7b41-9d05-3c7e5a8f1b20"
32
+ *
33
+ * @throws {RangeError} From the returned function, when the clock reads outside
34
+ * the 48-bit Unix millisecond range while it is ahead of the last id,
35
+ * `randomBytes` returns the wrong length, or the last millisecond has no
36
+ * successor in 48 bits. The generator is then as it was before the call.
37
+ */
38
+ export declare function createUuidV7Generator(options?: UuidV7GeneratorOptions): CallableNoArgs<string>;
39
+ /**
40
+ * The creation time a UUIDv7 carries, to the millisecond.
41
+ *
42
+ * @throws {UuidVersionError} When `uuid` is not a version 7 UUID of the RFC
43
+ * 9562 variant.
44
+ * @throws {UuidSyntaxError} When text is not a UUID.
45
+ * @throws {RangeError} When bytes are not 16 long.
46
+ */
47
+ export declare function getUuidV7Timestamp(uuid: UuidInput): Temporal.Instant;
48
+ //# sourceMappingURL=uuid-v7.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uuid-v7.d.ts","sourceRoot":"","sources":["../src/uuid-v7.ts"],"names":[],"mappings":"AAAA,OAAO,EAGN,KAAK,QAAQ,EACb,KAAK,cAAc,EACnB,KAAK,aAAa,EAClB,MAAM,cAAc,CAAC;AAUtB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAE3C,MAAM,MAAM,sBAAsB,GAAG;IACpC,4EAA4E;IAC5E,QAAQ,CAAC,GAAG,CAAC,EAAE,aAAa,CAAC;IAC7B;;;;OAIG;IACH,QAAQ,CAAC,WAAW,CAAC,EAAE,QAAQ,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,UAAU,CAAC,CAAC;CAC9D,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,qBAAqB,CACpC,OAAO,CAAC,EAAE,sBAAsB,GAC9B,cAAc,CAAC,MAAM,CAAC,CAMxB;AAED;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,SAAS,GAAG,QAAQ,CAAC,OAAO,CAUpE"}
@@ -0,0 +1,52 @@
1
+ import { getUint48Be, SYSTEM_INSTANT_SOURCE, } from "@ac-kit/core";
2
+ import { getRandomBytes } from "@ac-kit/crypto-random";
3
+ import { toUuidBytes } from "./_uuid-bytes.js";
4
+ import { createMonotonicUuidV7Generator, UUID_V7_COUNTER_LIMIT, } from "./_uuid-v7-generator.js";
5
+ import { UuidVersionError } from "./errors.js";
6
+ import { getUuidVariant, getUuidVersion } from "./uuid-fields.js";
7
+ /**
8
+ * Creates a generator of UUIDv7 text (RFC 9562 §5.7) that is strictly
9
+ * increasing, in value and in text, however fast it is called and whatever the
10
+ * clock does: the monotonic method of §6.2 with a fixed-length counter.
11
+ *
12
+ * Each id is the 48-bit Unix millisecond timestamp, the version and variant
13
+ * bits, a 42-bit counter and 32 fresh random bits. The counter starts at a
14
+ * random value below 2^41 in every new millisecond and grows by one per id
15
+ * within it. When it runs out the timestamp moves to the next millisecond; when
16
+ * the clock stands still or goes back the last millisecond is kept. Neither
17
+ * case throws, and the timestamp may then run ahead of the clock.
18
+ *
19
+ * Monotonic within one generator only: two generators — two processes — agree
20
+ * on the order by millisecond, not within it. Each id costs constant time and
21
+ * one `randomBytes` call, plus one more when a new millisecond starts.
22
+ *
23
+ * @example
24
+ * const nextId = createUuidV7Generator();
25
+ * nextId(); // "01926f3c-8a2e-7b41-9d05-3c7e5a8f1b20"
26
+ *
27
+ * @throws {RangeError} From the returned function, when the clock reads outside
28
+ * the 48-bit Unix millisecond range while it is ahead of the last id,
29
+ * `randomBytes` returns the wrong length, or the last millisecond has no
30
+ * successor in 48 bits. The generator is then as it was before the call.
31
+ */
32
+ export function createUuidV7Generator(options) {
33
+ return createMonotonicUuidV7Generator(options?.now ?? SYSTEM_INSTANT_SOURCE, options?.randomBytes ?? getRandomBytes, UUID_V7_COUNTER_LIMIT);
34
+ }
35
+ /**
36
+ * The creation time a UUIDv7 carries, to the millisecond.
37
+ *
38
+ * @throws {UuidVersionError} When `uuid` is not a version 7 UUID of the RFC
39
+ * 9562 variant.
40
+ * @throws {UuidSyntaxError} When text is not a UUID.
41
+ * @throws {RangeError} When bytes are not 16 long.
42
+ */
43
+ export function getUuidV7Timestamp(uuid) {
44
+ const bytes = toUuidBytes(uuid);
45
+ const variant = getUuidVariant(bytes);
46
+ const version = getUuidVersion(bytes);
47
+ if (version !== 7 || variant !== "rfc9562") {
48
+ throw new UuidVersionError(7, version, variant);
49
+ }
50
+ return Temporal.Instant.fromEpochMilliseconds(getUint48Be(bytes, 0));
51
+ }
52
+ //# sourceMappingURL=uuid-v7.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uuid-v7.js","sourceRoot":"","sources":["../src/uuid-v7.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,WAAW,EACX,qBAAqB,GAIrB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAEvD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EACN,8BAA8B,EAC9B,qBAAqB,GACrB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAclE;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,qBAAqB,CACpC,OAAgC;IAEhC,OAAO,8BAA8B,CACpC,OAAO,EAAE,GAAG,IAAI,qBAAqB,EACrC,OAAO,EAAE,WAAW,IAAI,cAAc,EACtC,qBAAqB,CACrB,CAAC;AACH,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,kBAAkB,CAAC,IAAe;IACjD,MAAM,KAAK,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;IAChC,MAAM,OAAO,GAAG,cAAc,CAAC,KAAK,CAAC,CAAC;IACtC,MAAM,OAAO,GAAG,cAAc,CAAC,KAAK,CAAC,CAAC;IAEtC,IAAI,OAAO,KAAK,CAAC,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QAC5C,MAAM,IAAI,gBAAgB,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;IACjD,CAAC;IAED,OAAO,QAAQ,CAAC,OAAO,CAAC,qBAAqB,CAAC,WAAW,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC;AACtE,CAAC"}
package/dist/uuid.d.ts ADDED
@@ -0,0 +1,43 @@
1
+ /** A UUID as 36 characters of text (8-4-4-4-12, any case) or as its 16 bytes. */
2
+ export type UuidInput = string | Uint8Array;
3
+ export type ParseUuidOptions = {
4
+ /**
5
+ * Expect the `urn:uuid:` form (RFC 9562 §4) that {@link printUuid} writes with
6
+ * its `urn` option, the prefix in any case. Default `false`: the bare
7
+ * 8-4-4-4-12 text only.
8
+ */
9
+ readonly urn?: boolean;
10
+ };
11
+ export type PrintUuidOptions = {
12
+ /** Write `urn:uuid:` before the text. Default `false`. */
13
+ readonly urn?: boolean;
14
+ };
15
+ /** The nil UUID: all 128 bits zero. */
16
+ export declare const NIL_UUID = "00000000-0000-0000-0000-000000000000";
17
+ /** The max UUID: all 128 bits one. */
18
+ export declare const MAX_UUID = "ffffffff-ffff-ffff-ffff-ffffffffffff";
19
+ /**
20
+ * Whether `value` is a string in the 8-4-4-4-12 form, in either case — the text
21
+ * {@link parseUuid} accepts without options.
22
+ *
23
+ * Answers a plain `boolean` rather than narrowing: a string that fails the
24
+ * check is still a `string`.
25
+ */
26
+ export declare function isUuid(value: unknown): boolean;
27
+ /**
28
+ * Reads a UUID from its 8-4-4-4-12 text, in either case, into its 16 bytes. Any
29
+ * version and variant is accepted, the nil and max UUIDs included.
30
+ *
31
+ * @throws {UuidSyntaxError} When `text` is not a UUID.
32
+ */
33
+ export declare function parseUuid(text: string, options?: ParseUuidOptions): Uint8Array<ArrayBuffer>;
34
+ /** Like {@link parseUuid}, answering `null` for malformed text. */
35
+ export declare function tryParseUuid(text: string, options?: ParseUuidOptions): Uint8Array<ArrayBuffer> | null;
36
+ /**
37
+ * Writes 16 bytes as lowercase 8-4-4-4-12 text. For lowercase text, string
38
+ * order is byte order, so printed UUIDs sort as their bytes do.
39
+ *
40
+ * @throws {RangeError} When `bytes` is not 16 bytes long.
41
+ */
42
+ export declare function printUuid(bytes: Uint8Array, options?: PrintUuidOptions): string;
43
+ //# sourceMappingURL=uuid.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uuid.d.ts","sourceRoot":"","sources":["../src/uuid.ts"],"names":[],"mappings":"AASA,iFAAiF;AACjF,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,UAAU,CAAC;AAE5C,MAAM,MAAM,gBAAgB,GAAG;IAC9B;;;;OAIG;IACH,QAAQ,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC;CACvB,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC9B,0DAA0D;IAC1D,QAAQ,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC;CACvB,CAAC;AAEF,uCAAuC;AACvC,eAAO,MAAM,QAAQ,yCAAyC,CAAC;AAE/D,sCAAsC;AACtC,eAAO,MAAM,QAAQ,yCAAyC,CAAC;AAsD/D;;;;;;GAMG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAE9C;AAED;;;;;GAKG;AACH,wBAAgB,SAAS,CACxB,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,gBAAgB,GACxB,UAAU,CAAC,WAAW,CAAC,CAQzB;AAED,mEAAmE;AACnE,wBAAgB,YAAY,CAC3B,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,gBAAgB,GACxB,UAAU,CAAC,WAAW,CAAC,GAAG,IAAI,CAIhC;AAED;;;;;GAKG;AACH,wBAAgB,SAAS,CACxB,KAAK,EAAE,UAAU,EACjB,OAAO,CAAC,EAAE,gBAAgB,GACxB,MAAM,CASR"}
package/dist/uuid.js ADDED
@@ -0,0 +1,87 @@
1
+ import { equalsAsciiIgnoreCase, HYPHEN_MINUS, isAsciiHexDigit, } from "@ac-kit/core";
2
+ import { UuidSyntaxError } from "./errors.js";
3
+ /** The nil UUID: all 128 bits zero. */
4
+ export const NIL_UUID = "00000000-0000-0000-0000-000000000000";
5
+ /** The max UUID: all 128 bits one. */
6
+ export const MAX_UUID = "ffffffff-ffff-ffff-ffff-ffffffffffff";
7
+ const URN_PREFIX = "urn:uuid:";
8
+ const TEXT_LENGTH = 36;
9
+ const BYTE_LENGTH = 16;
10
+ const DASH_OFFSETS = [8, 13, 18, 23];
11
+ // Linear in the text length, and never reads past the 36th character.
12
+ function findFault_(text, start) {
13
+ const end = start + TEXT_LENGTH;
14
+ const scanEnd = Math.min(text.length, end);
15
+ for (let index = start; index < scanEnd; index++) {
16
+ const code = text.charCodeAt(index);
17
+ if (DASH_OFFSETS.includes(index - start)) {
18
+ if (code !== HYPHEN_MINUS) {
19
+ return { offset: index, detail: "expected '-'" };
20
+ }
21
+ }
22
+ else if (!isAsciiHexDigit(code)) {
23
+ return { offset: index, detail: "expected a hexadecimal digit" };
24
+ }
25
+ }
26
+ if (text.length < end) {
27
+ return { offset: text.length, detail: "unexpected end of text" };
28
+ }
29
+ if (text.length > end) {
30
+ return { offset: end, detail: "unexpected text after the UUID" };
31
+ }
32
+ return null;
33
+ }
34
+ function readUuid_(text, urn) {
35
+ let start = 0;
36
+ if (urn) {
37
+ if (!equalsAsciiIgnoreCase(text.slice(0, URN_PREFIX.length), URN_PREFIX)) {
38
+ return { offset: 0, detail: `expected "${URN_PREFIX}"` };
39
+ }
40
+ start = URN_PREFIX.length;
41
+ }
42
+ return (findFault_(text, start) ??
43
+ Uint8Array.fromHex(text.slice(start).replaceAll("-", "")));
44
+ }
45
+ /**
46
+ * Whether `value` is a string in the 8-4-4-4-12 form, in either case — the text
47
+ * {@link parseUuid} accepts without options.
48
+ *
49
+ * Answers a plain `boolean` rather than narrowing: a string that fails the
50
+ * check is still a `string`.
51
+ */
52
+ export function isUuid(value) {
53
+ return typeof value === "string" && findFault_(value, 0) === null;
54
+ }
55
+ /**
56
+ * Reads a UUID from its 8-4-4-4-12 text, in either case, into its 16 bytes. Any
57
+ * version and variant is accepted, the nil and max UUIDs included.
58
+ *
59
+ * @throws {UuidSyntaxError} When `text` is not a UUID.
60
+ */
61
+ export function parseUuid(text, options) {
62
+ const result = readUuid_(text, options?.urn ?? false);
63
+ if ("detail" in result) {
64
+ throw new UuidSyntaxError(text, result.offset, result.detail);
65
+ }
66
+ return result;
67
+ }
68
+ /** Like {@link parseUuid}, answering `null` for malformed text. */
69
+ export function tryParseUuid(text, options) {
70
+ const result = readUuid_(text, options?.urn ?? false);
71
+ return "detail" in result ? null : result;
72
+ }
73
+ /**
74
+ * Writes 16 bytes as lowercase 8-4-4-4-12 text. For lowercase text, string
75
+ * order is byte order, so printed UUIDs sort as their bytes do.
76
+ *
77
+ * @throws {RangeError} When `bytes` is not 16 bytes long.
78
+ */
79
+ export function printUuid(bytes, options) {
80
+ if (bytes.length !== BYTE_LENGTH) {
81
+ throw new RangeError(`printUuid: expected 16 bytes, got ${bytes.length}`);
82
+ }
83
+ const hex = bytes.toHex();
84
+ const text = `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
85
+ return options?.urn === true ? `${URN_PREFIX}${text}` : text;
86
+ }
87
+ //# sourceMappingURL=uuid.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uuid.js","sourceRoot":"","sources":["../src/uuid.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,qBAAqB,EACrB,YAAY,EACZ,eAAe,GAEf,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAmB9C,uCAAuC;AACvC,MAAM,CAAC,MAAM,QAAQ,GAAG,sCAAsC,CAAC;AAE/D,sCAAsC;AACtC,MAAM,CAAC,MAAM,QAAQ,GAAG,sCAAsC,CAAC;AAE/D,MAAM,UAAU,GAAG,WAAW,CAAC;AAC/B,MAAM,WAAW,GAAG,EAAE,CAAC;AACvB,MAAM,WAAW,GAAG,EAAE,CAAC;AACvB,MAAM,YAAY,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;AAErC,sEAAsE;AACtE,SAAS,UAAU,CAAC,IAAY,EAAE,KAAa;IAC9C,MAAM,GAAG,GAAG,KAAK,GAAG,WAAW,CAAC;IAChC,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAE3C,KAAK,IAAI,KAA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AK,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,EAAE,CAAC;IAEjH,OAAO,OAAO,EAAE,GAAG,KAAK,IAAI,CAAC,CAAC,CAAC,GAAG,UAAU,GAAG,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;AAC9D,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,65 @@
1
1
  {
2
2
  "name": "@ac-kit/format-uuid",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "UUIDs (RFC 9562): parse, print, compare, inspect, and generate monotonic version 7 identifiers.",
5
+ "keywords": [
6
+ "guid",
7
+ "rfc-9562",
8
+ "uuid",
9
+ "uuidv7"
10
+ ],
11
+ "homepage": "https://anthochamp.pages.grenadine.io/node-essentials/api/format-uuid/",
12
+ "bugs": {
13
+ "url": "https://git.grenadine.io/anthochamp/node-essentials/issues"
14
+ },
15
+ "license": "BSD-3-Clause",
16
+ "author": "Anthony Champagne <dev@anthonychampagne.fr>",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://git.grenadine.io/anthochamp/node-essentials.git",
20
+ "directory": "packages/format-uuid"
21
+ },
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "type": "module",
26
+ "sideEffects": false,
27
+ "main": "./dist/index.js",
28
+ "exports": {
29
+ ".": "./dist/index.js"
30
+ },
31
+ "publishConfig": {
32
+ "main": "./dist/index.js",
33
+ "exports": {
34
+ ".": "./dist/index.js"
35
+ },
36
+ "access": "public"
37
+ },
38
+ "scripts": {
39
+ "compile": "tsc -p ./tsconfig.build.json -outDir dist",
40
+ "compile:check": "tsc -noEmit -p ./tsconfig.json",
41
+ "clean": "rimraf dist",
42
+ "build": "yarn clean && yarn compile",
43
+ "test": "yarn run -T vitest run",
44
+ "prepack": "yarn build"
45
+ },
46
+ "dependencies": {
47
+ "@ac-kit/core": "^0.10.0",
48
+ "@ac-kit/crypto-random": "^0.2.2",
49
+ "@types/web": "^0.0.367"
50
+ },
51
+ "devDependencies": {
52
+ "@ac-kit/math-random": "0.2.0",
53
+ "@ac-kit/test-util": "0.3.0",
54
+ "@ac-kit/tsconfig": "0.1.0",
55
+ "@ac-kit/typedoc-config": "0.1.0",
56
+ "@ac-kit/vitest-config": "0.1.1",
57
+ "@typescript/native": "typescript@7.0.2",
58
+ "rimraf": "6.1.4",
59
+ "typedoc": "0.28.20",
60
+ "typescript": "@typescript/typescript6@6.0.2"
61
+ },
62
+ "engines": {
63
+ "node": "^24.19.0 || >=26.5.0"
64
+ }
6
65
  }