@deciosfernandes/uuidv7-timestamp 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 deciosfernandes
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # @deciosfernandes/uuidv7-timestamp
2
+
3
+ Extract epoch timestamp from UUID v7 strings (TypeScript).
4
+
5
+ This tiny library extracts the timestamp encoded in UUID v7 values (the first 48 bits / first 12 hex chars) and returns it as milliseconds, a Date, or an ISO string.
6
+
7
+ ## Installation
8
+
9
+ npm:
10
+ ```bash
11
+ npm install @deciosfernandes/uuidv7-timestamp
12
+ ```
13
+
14
+ yarn:
15
+ ```bash
16
+ yarn add @deciosfernandes/uuidv7-timestamp
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ```ts
22
+ import { extractTimestampFromUuidV7, extractTimestampAsDate } from '@deciosfernandes/uuidv7-timestamp';
23
+
24
+ const uuid = '017f7f58-89ab-7000-8123-0123456789ab';
25
+ const millis = extractTimestampFromUuidV7(uuid);
26
+ const date = extractTimestampAsDate(uuid);
27
+ console.log(millis, date.toISOString());
28
+ ```
29
+
30
+ ## API
31
+
32
+ - extractTimestampFromUuidV7(uuid: string): number — returns epoch milliseconds encoded in the UUID v7.
33
+ - extractTimestampAsDate(uuid: string): Date — returns a Date instance.
34
+ - extractTimestampAsISOString(uuid: string): string — returns an ISO 8601 string.
35
+
36
+ ## Notes
37
+
38
+ - The function validates the UUID format and ensures the version nibble is `7`. If the UUID is not v7, an Error is thrown.
39
+ - The timestamp is assumed to be the first 48 bits of the UUID per the v7 draft layout.
40
+
41
+ ## License
42
+
43
+ MIT
@@ -0,0 +1,76 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ /**
6
+ * Extract timestamp from UUID v7 strings.
7
+ *
8
+ * The timestamp for UUID v7 is encoded in the first 48 bits (12 hex chars) of the UUID.
9
+ * This module exposes three helpers:
10
+ * - extractTimestampFromUuidV7(uuid): number (epoch millis)
11
+ * - extractTimestampAsDate(uuid): Date
12
+ * - extractTimestampAsISOString(uuid): string
13
+ */
14
+ const HEX_32_RE = /^[0-9a-fA-F]{32}$/;
15
+ /**
16
+ * Extracts the epoch milliseconds timestamp from a UUID v7 string.
17
+ * Caller must pass a UUID v7 (validation is performed and an error thrown if it's not v7).
18
+ *
19
+ * @param uuid - UUID string (hyphenated or not)
20
+ * @returns epoch milliseconds (number)
21
+ * @throws Error if input is not a valid UUID string or not version 7
22
+ */
23
+ function extractTimestampFromUuidV7(uuid) {
24
+ if (typeof uuid !== 'string' || uuid.length === 0) {
25
+ throw new Error('uuid must be a non-empty string');
26
+ }
27
+ // Normalize: remove hyphens
28
+ const normalized = uuid.replace(/-/g, '');
29
+ if (!HEX_32_RE.test(normalized)) {
30
+ throw new Error('invalid UUID format');
31
+ }
32
+ // Version nibble is the first hex char of the 3rd UUID segment.
33
+ // In the normalized string it's at index 12 (0-based).
34
+ const versionChar = normalized[12];
35
+ if (versionChar !== '7') {
36
+ throw new Error(`expected UUID v7 (version nibble = 7), got version nibble = ${versionChar}`);
37
+ }
38
+ // Timestamp for UUID v7 is the top 48 bits: time_low (8 hex) + time_mid (4 hex) => first 12 hex chars
39
+ const timeHex = normalized.slice(0, 12); // 12 hex chars => 48 bits
40
+ const millisBig = BigInt(`0x${timeHex}`);
41
+ const millis = Number(millisBig);
42
+ if (!Number.isFinite(millis)) {
43
+ throw new Error('extracted timestamp is not a finite number');
44
+ }
45
+ return millis;
46
+ }
47
+ /**
48
+ * Same as extractTimestampFromUuidV7 but returns a Date instance.
49
+ *
50
+ * @param uuid - UUID v7 string
51
+ * @returns Date representing the timestamp encoded in the UUID
52
+ */
53
+ function extractTimestampAsDate(uuid) {
54
+ const ms = extractTimestampFromUuidV7(uuid);
55
+ return new Date(ms);
56
+ }
57
+ /**
58
+ * Convenience: returns an ISO string for the timestamp contained in the UUID v7.
59
+ *
60
+ * @param uuid - UUID v7 string
61
+ * @returns ISO 8601 string (UTC)
62
+ */
63
+ function extractTimestampAsISOString(uuid) {
64
+ return extractTimestampAsDate(uuid).toISOString();
65
+ }
66
+ var index = {
67
+ extractTimestampFromUuidV7,
68
+ extractTimestampAsDate,
69
+ extractTimestampAsISOString
70
+ };
71
+
72
+ exports.default = index;
73
+ exports.extractTimestampAsDate = extractTimestampAsDate;
74
+ exports.extractTimestampAsISOString = extractTimestampAsISOString;
75
+ exports.extractTimestampFromUuidV7 = extractTimestampFromUuidV7;
76
+ //# sourceMappingURL=index.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs.js","sources":["../src/index.ts"],"sourcesContent":["/**\n * Extract timestamp from UUID v7 strings.\n *\n * The timestamp for UUID v7 is encoded in the first 48 bits (12 hex chars) of the UUID.\n * This module exposes three helpers:\n * - extractTimestampFromUuidV7(uuid): number (epoch millis)\n * - extractTimestampAsDate(uuid): Date\n * - extractTimestampAsISOString(uuid): string\n */\n\nconst HEX_32_RE = /^[0-9a-fA-F]{32}$/;\n\n/**\n * Extracts the epoch milliseconds timestamp from a UUID v7 string.\n * Caller must pass a UUID v7 (validation is performed and an error thrown if it's not v7).\n *\n * @param uuid - UUID string (hyphenated or not)\n * @returns epoch milliseconds (number)\n * @throws Error if input is not a valid UUID string or not version 7\n */\nexport function extractTimestampFromUuidV7(uuid: string): number {\n if (typeof uuid !== 'string' || uuid.length === 0) {\n throw new Error('uuid must be a non-empty string');\n }\n\n // Normalize: remove hyphens\n const normalized = uuid.replace(/-/g, '');\n\n if (!HEX_32_RE.test(normalized)) {\n throw new Error('invalid UUID format');\n }\n\n // Version nibble is the first hex char of the 3rd UUID segment.\n // In the normalized string it's at index 12 (0-based).\n const versionChar = normalized[12];\n if (versionChar !== '7') {\n throw new Error(`expected UUID v7 (version nibble = 7), got version nibble = ${versionChar}`);\n }\n\n // Timestamp for UUID v7 is the top 48 bits: time_low (8 hex) + time_mid (4 hex) => first 12 hex chars\n const timeHex = normalized.slice(0, 12); // 12 hex chars => 48 bits\n const millisBig = BigInt(`0x${timeHex}`);\n const millis = Number(millisBig);\n\n if (!Number.isFinite(millis)) {\n throw new Error('extracted timestamp is not a finite number');\n }\n\n return millis;\n}\n\n/**\n * Same as extractTimestampFromUuidV7 but returns a Date instance.\n *\n * @param uuid - UUID v7 string\n * @returns Date representing the timestamp encoded in the UUID\n */\nexport function extractTimestampAsDate(uuid: string): Date {\n const ms = extractTimestampFromUuidV7(uuid);\n return new Date(ms);\n}\n\n/**\n * Convenience: returns an ISO string for the timestamp contained in the UUID v7.\n *\n * @param uuid - UUID v7 string\n * @returns ISO 8601 string (UTC)\n */\nexport function extractTimestampAsISOString(uuid: string): string {\n return extractTimestampAsDate(uuid).toISOString();\n}\n\nexport default {\n extractTimestampFromUuidV7,\n extractTimestampAsDate,\n extractTimestampAsISOString\n};"],"names":[],"mappings":";;;;AAAA;;;;;;;;AAQG;AAEH,MAAM,SAAS,GAAG,mBAAmB,CAAC;AAEtC;;;;;;;AAOG;AACG,SAAU,0BAA0B,CAAC,IAAY,EAAA;IACrD,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE;AACjD,QAAA,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;KACpD;;IAGD,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;IAE1C,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE;AAC/B,QAAA,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;KACxC;;;AAID,IAAA,MAAM,WAAW,GAAG,UAAU,CAAC,EAAE,CAAC,CAAC;AACnC,IAAA,IAAI,WAAW,KAAK,GAAG,EAAE;AACvB,QAAA,MAAM,IAAI,KAAK,CAAC,+DAA+D,WAAW,CAAA,CAAE,CAAC,CAAC;KAC/F;;AAGD,IAAA,MAAM,OAAO,GAAG,UAAU,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IACxC,MAAM,SAAS,GAAG,MAAM,CAAC,KAAK,OAAO,CAAA,CAAE,CAAC,CAAC;AACzC,IAAA,MAAM,MAAM,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC;IAEjC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE;AAC5B,QAAA,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;KAC/D;AAED,IAAA,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;AAKG;AACG,SAAU,sBAAsB,CAAC,IAAY,EAAA;AACjD,IAAA,MAAM,EAAE,GAAG,0BAA0B,CAAC,IAAI,CAAC,CAAC;AAC5C,IAAA,OAAO,IAAI,IAAI,CAAC,EAAE,CAAC,CAAC;AACtB,CAAC;AAED;;;;;AAKG;AACG,SAAU,2BAA2B,CAAC,IAAY,EAAA;AACtD,IAAA,OAAO,sBAAsB,CAAC,IAAI,CAAC,CAAC,WAAW,EAAE,CAAC;AACpD,CAAC;AAED,YAAe;IACb,0BAA0B;IAC1B,sBAAsB;IACtB,2BAA2B;CAC5B;;;;;;;"}
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Extract timestamp from UUID v7 strings.
3
+ *
4
+ * The timestamp for UUID v7 is encoded in the first 48 bits (12 hex chars) of the UUID.
5
+ * This module exposes three helpers:
6
+ * - extractTimestampFromUuidV7(uuid): number (epoch millis)
7
+ * - extractTimestampAsDate(uuid): Date
8
+ * - extractTimestampAsISOString(uuid): string
9
+ */
10
+ /**
11
+ * Extracts the epoch milliseconds timestamp from a UUID v7 string.
12
+ * Caller must pass a UUID v7 (validation is performed and an error thrown if it's not v7).
13
+ *
14
+ * @param uuid - UUID string (hyphenated or not)
15
+ * @returns epoch milliseconds (number)
16
+ * @throws Error if input is not a valid UUID string or not version 7
17
+ */
18
+ declare function extractTimestampFromUuidV7(uuid: string): number;
19
+ /**
20
+ * Same as extractTimestampFromUuidV7 but returns a Date instance.
21
+ *
22
+ * @param uuid - UUID v7 string
23
+ * @returns Date representing the timestamp encoded in the UUID
24
+ */
25
+ declare function extractTimestampAsDate(uuid: string): Date;
26
+ /**
27
+ * Convenience: returns an ISO string for the timestamp contained in the UUID v7.
28
+ *
29
+ * @param uuid - UUID v7 string
30
+ * @returns ISO 8601 string (UTC)
31
+ */
32
+ declare function extractTimestampAsISOString(uuid: string): string;
33
+ declare const _default: {
34
+ extractTimestampFromUuidV7: typeof extractTimestampFromUuidV7;
35
+ extractTimestampAsDate: typeof extractTimestampAsDate;
36
+ extractTimestampAsISOString: typeof extractTimestampAsISOString;
37
+ };
38
+
39
+ export { _default as default, extractTimestampAsDate, extractTimestampAsISOString, extractTimestampFromUuidV7 };
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Extract timestamp from UUID v7 strings.
3
+ *
4
+ * The timestamp for UUID v7 is encoded in the first 48 bits (12 hex chars) of the UUID.
5
+ * This module exposes three helpers:
6
+ * - extractTimestampFromUuidV7(uuid): number (epoch millis)
7
+ * - extractTimestampAsDate(uuid): Date
8
+ * - extractTimestampAsISOString(uuid): string
9
+ */
10
+ const HEX_32_RE = /^[0-9a-fA-F]{32}$/;
11
+ /**
12
+ * Extracts the epoch milliseconds timestamp from a UUID v7 string.
13
+ * Caller must pass a UUID v7 (validation is performed and an error thrown if it's not v7).
14
+ *
15
+ * @param uuid - UUID string (hyphenated or not)
16
+ * @returns epoch milliseconds (number)
17
+ * @throws Error if input is not a valid UUID string or not version 7
18
+ */
19
+ function extractTimestampFromUuidV7(uuid) {
20
+ if (typeof uuid !== 'string' || uuid.length === 0) {
21
+ throw new Error('uuid must be a non-empty string');
22
+ }
23
+ // Normalize: remove hyphens
24
+ const normalized = uuid.replace(/-/g, '');
25
+ if (!HEX_32_RE.test(normalized)) {
26
+ throw new Error('invalid UUID format');
27
+ }
28
+ // Version nibble is the first hex char of the 3rd UUID segment.
29
+ // In the normalized string it's at index 12 (0-based).
30
+ const versionChar = normalized[12];
31
+ if (versionChar !== '7') {
32
+ throw new Error(`expected UUID v7 (version nibble = 7), got version nibble = ${versionChar}`);
33
+ }
34
+ // Timestamp for UUID v7 is the top 48 bits: time_low (8 hex) + time_mid (4 hex) => first 12 hex chars
35
+ const timeHex = normalized.slice(0, 12); // 12 hex chars => 48 bits
36
+ const millisBig = BigInt(`0x${timeHex}`);
37
+ const millis = Number(millisBig);
38
+ if (!Number.isFinite(millis)) {
39
+ throw new Error('extracted timestamp is not a finite number');
40
+ }
41
+ return millis;
42
+ }
43
+ /**
44
+ * Same as extractTimestampFromUuidV7 but returns a Date instance.
45
+ *
46
+ * @param uuid - UUID v7 string
47
+ * @returns Date representing the timestamp encoded in the UUID
48
+ */
49
+ function extractTimestampAsDate(uuid) {
50
+ const ms = extractTimestampFromUuidV7(uuid);
51
+ return new Date(ms);
52
+ }
53
+ /**
54
+ * Convenience: returns an ISO string for the timestamp contained in the UUID v7.
55
+ *
56
+ * @param uuid - UUID v7 string
57
+ * @returns ISO 8601 string (UTC)
58
+ */
59
+ function extractTimestampAsISOString(uuid) {
60
+ return extractTimestampAsDate(uuid).toISOString();
61
+ }
62
+ var index = {
63
+ extractTimestampFromUuidV7,
64
+ extractTimestampAsDate,
65
+ extractTimestampAsISOString
66
+ };
67
+
68
+ export { index as default, extractTimestampAsDate, extractTimestampAsISOString, extractTimestampFromUuidV7 };
69
+ //# sourceMappingURL=index.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.esm.js","sources":["../src/index.ts"],"sourcesContent":["/**\n * Extract timestamp from UUID v7 strings.\n *\n * The timestamp for UUID v7 is encoded in the first 48 bits (12 hex chars) of the UUID.\n * This module exposes three helpers:\n * - extractTimestampFromUuidV7(uuid): number (epoch millis)\n * - extractTimestampAsDate(uuid): Date\n * - extractTimestampAsISOString(uuid): string\n */\n\nconst HEX_32_RE = /^[0-9a-fA-F]{32}$/;\n\n/**\n * Extracts the epoch milliseconds timestamp from a UUID v7 string.\n * Caller must pass a UUID v7 (validation is performed and an error thrown if it's not v7).\n *\n * @param uuid - UUID string (hyphenated or not)\n * @returns epoch milliseconds (number)\n * @throws Error if input is not a valid UUID string or not version 7\n */\nexport function extractTimestampFromUuidV7(uuid: string): number {\n if (typeof uuid !== 'string' || uuid.length === 0) {\n throw new Error('uuid must be a non-empty string');\n }\n\n // Normalize: remove hyphens\n const normalized = uuid.replace(/-/g, '');\n\n if (!HEX_32_RE.test(normalized)) {\n throw new Error('invalid UUID format');\n }\n\n // Version nibble is the first hex char of the 3rd UUID segment.\n // In the normalized string it's at index 12 (0-based).\n const versionChar = normalized[12];\n if (versionChar !== '7') {\n throw new Error(`expected UUID v7 (version nibble = 7), got version nibble = ${versionChar}`);\n }\n\n // Timestamp for UUID v7 is the top 48 bits: time_low (8 hex) + time_mid (4 hex) => first 12 hex chars\n const timeHex = normalized.slice(0, 12); // 12 hex chars => 48 bits\n const millisBig = BigInt(`0x${timeHex}`);\n const millis = Number(millisBig);\n\n if (!Number.isFinite(millis)) {\n throw new Error('extracted timestamp is not a finite number');\n }\n\n return millis;\n}\n\n/**\n * Same as extractTimestampFromUuidV7 but returns a Date instance.\n *\n * @param uuid - UUID v7 string\n * @returns Date representing the timestamp encoded in the UUID\n */\nexport function extractTimestampAsDate(uuid: string): Date {\n const ms = extractTimestampFromUuidV7(uuid);\n return new Date(ms);\n}\n\n/**\n * Convenience: returns an ISO string for the timestamp contained in the UUID v7.\n *\n * @param uuid - UUID v7 string\n * @returns ISO 8601 string (UTC)\n */\nexport function extractTimestampAsISOString(uuid: string): string {\n return extractTimestampAsDate(uuid).toISOString();\n}\n\nexport default {\n extractTimestampFromUuidV7,\n extractTimestampAsDate,\n extractTimestampAsISOString\n};"],"names":[],"mappings":"AAAA;;;;;;;;AAQG;AAEH,MAAM,SAAS,GAAG,mBAAmB,CAAC;AAEtC;;;;;;;AAOG;AACG,SAAU,0BAA0B,CAAC,IAAY,EAAA;IACrD,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE;AACjD,QAAA,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;KACpD;;IAGD,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;IAE1C,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE;AAC/B,QAAA,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;KACxC;;;AAID,IAAA,MAAM,WAAW,GAAG,UAAU,CAAC,EAAE,CAAC,CAAC;AACnC,IAAA,IAAI,WAAW,KAAK,GAAG,EAAE;AACvB,QAAA,MAAM,IAAI,KAAK,CAAC,+DAA+D,WAAW,CAAA,CAAE,CAAC,CAAC;KAC/F;;AAGD,IAAA,MAAM,OAAO,GAAG,UAAU,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IACxC,MAAM,SAAS,GAAG,MAAM,CAAC,KAAK,OAAO,CAAA,CAAE,CAAC,CAAC;AACzC,IAAA,MAAM,MAAM,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC;IAEjC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE;AAC5B,QAAA,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;KAC/D;AAED,IAAA,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;AAKG;AACG,SAAU,sBAAsB,CAAC,IAAY,EAAA;AACjD,IAAA,MAAM,EAAE,GAAG,0BAA0B,CAAC,IAAI,CAAC,CAAC;AAC5C,IAAA,OAAO,IAAI,IAAI,CAAC,EAAE,CAAC,CAAC;AACtB,CAAC;AAED;;;;;AAKG;AACG,SAAU,2BAA2B,CAAC,IAAY,EAAA;AACtD,IAAA,OAAO,sBAAsB,CAAC,IAAI,CAAC,CAAC,WAAW,EAAE,CAAC;AACpD,CAAC;AAED,YAAe;IACb,0BAA0B;IAC1B,sBAAsB;IACtB,2BAA2B;CAC5B;;;;"}
package/dist/index.js ADDED
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Extract timestamp from UUID v7 strings.
3
+ *
4
+ * The timestamp for UUID v7 is encoded in the first 48 bits (12 hex chars) of the UUID.
5
+ * This module exposes three helpers:
6
+ * - extractTimestampFromUuidV7(uuid): number (epoch millis)
7
+ * - extractTimestampAsDate(uuid): Date
8
+ * - extractTimestampAsISOString(uuid): string
9
+ */
10
+ const HEX_32_RE = /^[0-9a-fA-F]{32}$/;
11
+ /**
12
+ * Extracts the epoch milliseconds timestamp from a UUID v7 string.
13
+ * Caller must pass a UUID v7 (validation is performed and an error thrown if it's not v7).
14
+ *
15
+ * @param uuid - UUID string (hyphenated or not)
16
+ * @returns epoch milliseconds (number)
17
+ * @throws Error if input is not a valid UUID string or not version 7
18
+ */
19
+ export function extractTimestampFromUuidV7(uuid) {
20
+ if (typeof uuid !== 'string' || uuid.length === 0) {
21
+ throw new Error('uuid must be a non-empty string');
22
+ }
23
+ // Normalize: remove hyphens
24
+ const normalized = uuid.replace(/-/g, '');
25
+ if (!HEX_32_RE.test(normalized)) {
26
+ throw new Error('invalid UUID format');
27
+ }
28
+ // Version nibble is the first hex char of the 3rd UUID segment.
29
+ // In the normalized string it's at index 12 (0-based).
30
+ const versionChar = normalized[12];
31
+ if (versionChar !== '7') {
32
+ throw new Error(`expected UUID v7 (version nibble = 7), got version nibble = ${versionChar}`);
33
+ }
34
+ // Timestamp for UUID v7 is the top 48 bits: time_low (8 hex) + time_mid (4 hex) => first 12 hex chars
35
+ const timeHex = normalized.slice(0, 12); // 12 hex chars => 48 bits
36
+ const millisBig = BigInt(`0x${timeHex}`);
37
+ const millis = Number(millisBig);
38
+ if (!Number.isFinite(millis)) {
39
+ throw new Error('extracted timestamp is not a finite number');
40
+ }
41
+ return millis;
42
+ }
43
+ /**
44
+ * Same as extractTimestampFromUuidV7 but returns a Date instance.
45
+ *
46
+ * @param uuid - UUID v7 string
47
+ * @returns Date representing the timestamp encoded in the UUID
48
+ */
49
+ export function extractTimestampAsDate(uuid) {
50
+ const ms = extractTimestampFromUuidV7(uuid);
51
+ return new Date(ms);
52
+ }
53
+ /**
54
+ * Convenience: returns an ISO string for the timestamp contained in the UUID v7.
55
+ *
56
+ * @param uuid - UUID v7 string
57
+ * @returns ISO 8601 string (UTC)
58
+ */
59
+ export function extractTimestampAsISOString(uuid) {
60
+ return extractTimestampAsDate(uuid).toISOString();
61
+ }
62
+ export default {
63
+ extractTimestampFromUuidV7,
64
+ extractTimestampAsDate,
65
+ extractTimestampAsISOString
66
+ };
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@deciosfernandes/uuidv7-timestamp",
3
+ "version": "0.1.0",
4
+ "description": "Extract epoch timestamp from UUID v7 strings (millis, Date, ISO) for TypeScript/Angular apps.",
5
+ "main": "dist/index.cjs.js",
6
+ "module": "dist/index.esm.js",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "scripts": {
12
+ "build": "tsc && rollup -c",
13
+ "clean": "rm -rf dist .turbo cache",
14
+ "test": "vitest",
15
+ "lint": "eslint . --ext .ts",
16
+ "prepare": "npm run build"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "https://github.com/deciosfernandes/uuidv7-timestamp.git"
21
+ },
22
+ "keywords": [
23
+ "uuid",
24
+ "uuidv7",
25
+ "timestamp",
26
+ "typescript",
27
+ "angular"
28
+ ],
29
+ "author": "deciosfernandes",
30
+ "license": "MIT",
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "devDependencies": {
35
+ "@rollup/plugin-commonjs": "^24.0.0",
36
+ "@rollup/plugin-node-resolve": "^15.0.0",
37
+ "@rollup/plugin-typescript": "^12.3.0",
38
+ "@types/node": "^20.0.0",
39
+ "@typescript-eslint/eslint-plugin": "^5.0.0",
40
+ "@typescript-eslint/parser": "^5.0.0",
41
+ "eslint": "^8.0.0",
42
+ "rollup": "^3.0.0",
43
+ "rollup-plugin-dts": "^5.0.0",
44
+ "typescript": "^5.0.0",
45
+ "vitest": "^1.3.0"
46
+ }
47
+ }