@deciosfernandes/uuidv7-timestamp 0.1.0 → 0.1.2
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 +20 -20
- package/README.md +49 -42
- package/dist/index.cjs.js +3 -7
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +3 -7
- package/dist/index.esm.js.map +1 -1
- package/package.json +63 -47
- package/dist/index.js +0 -66
package/LICENSE
CHANGED
|
@@ -1,21 +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
|
|
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
21
|
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,43 +1,50 @@
|
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
+
```ts
|
|
33
|
+
// Returns epoch milliseconds encoded in the UUID v7
|
|
34
|
+
extractTimestampFromUuidV7(uuid: string): number
|
|
35
|
+
|
|
36
|
+
// Returns a Date instance
|
|
37
|
+
extractTimestampAsDate(uuid: string): Date
|
|
38
|
+
|
|
39
|
+
// Returns an ISO 8601 string (UTC)
|
|
40
|
+
extractTimestampAsISOString(uuid: string): string
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Notes
|
|
44
|
+
|
|
45
|
+
- The function validates the UUID format and ensures the version nibble is `7`. If the UUID is not v7, an Error is thrown.
|
|
46
|
+
- The timestamp is assumed to be the first 48 bits of the UUID per the v7 draft layout.
|
|
47
|
+
|
|
48
|
+
## License
|
|
49
|
+
|
|
43
50
|
MIT
|
package/dist/index.cjs.js
CHANGED
|
@@ -37,12 +37,8 @@ function extractTimestampFromUuidV7(uuid) {
|
|
|
37
37
|
}
|
|
38
38
|
// Timestamp for UUID v7 is the top 48 bits: time_low (8 hex) + time_mid (4 hex) => first 12 hex chars
|
|
39
39
|
const timeHex = normalized.slice(0, 12); // 12 hex chars => 48 bits
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
if (!Number.isFinite(millis)) {
|
|
43
|
-
throw new Error('extracted timestamp is not a finite number');
|
|
44
|
-
}
|
|
45
|
-
return millis;
|
|
40
|
+
// 48-bit integer is always within Number.MAX_SAFE_INTEGER (2^53 - 1), so parseInt is safe
|
|
41
|
+
return parseInt(timeHex, 16);
|
|
46
42
|
}
|
|
47
43
|
/**
|
|
48
44
|
* Same as extractTimestampFromUuidV7 but returns a Date instance.
|
|
@@ -66,7 +62,7 @@ function extractTimestampAsISOString(uuid) {
|
|
|
66
62
|
var index = {
|
|
67
63
|
extractTimestampFromUuidV7,
|
|
68
64
|
extractTimestampAsDate,
|
|
69
|
-
extractTimestampAsISOString
|
|
65
|
+
extractTimestampAsISOString,
|
|
70
66
|
};
|
|
71
67
|
|
|
72
68
|
exports.default = index;
|
package/dist/index.cjs.js.map
CHANGED
|
@@ -1 +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
|
|
1
|
+
{"version":3,"file":"index.cjs.js","sources":["../src/index.ts"],"sourcesContent":["/**\r\n * Extract timestamp from UUID v7 strings.\r\n *\r\n * The timestamp for UUID v7 is encoded in the first 48 bits (12 hex chars) of the UUID.\r\n * This module exposes three helpers:\r\n * - extractTimestampFromUuidV7(uuid): number (epoch millis)\r\n * - extractTimestampAsDate(uuid): Date\r\n * - extractTimestampAsISOString(uuid): string\r\n */\r\n\r\nconst HEX_32_RE = /^[0-9a-fA-F]{32}$/;\r\n\r\n/**\r\n * Extracts the epoch milliseconds timestamp from a UUID v7 string.\r\n * Caller must pass a UUID v7 (validation is performed and an error thrown if it's not v7).\r\n *\r\n * @param uuid - UUID string (hyphenated or not)\r\n * @returns epoch milliseconds (number)\r\n * @throws Error if input is not a valid UUID string or not version 7\r\n */\r\nexport function extractTimestampFromUuidV7(uuid: string): number {\r\n if (typeof uuid !== 'string' || uuid.length === 0) {\r\n throw new Error('uuid must be a non-empty string');\r\n }\r\n\r\n // Normalize: remove hyphens\r\n const normalized = uuid.replace(/-/g, '');\r\n\r\n if (!HEX_32_RE.test(normalized)) {\r\n throw new Error('invalid UUID format');\r\n }\r\n\r\n // Version nibble is the first hex char of the 3rd UUID segment.\r\n // In the normalized string it's at index 12 (0-based).\r\n const versionChar = normalized[12];\r\n if (versionChar !== '7') {\r\n throw new Error(`expected UUID v7 (version nibble = 7), got version nibble = ${versionChar}`);\r\n }\r\n\r\n // Timestamp for UUID v7 is the top 48 bits: time_low (8 hex) + time_mid (4 hex) => first 12 hex chars\r\n const timeHex = normalized.slice(0, 12); // 12 hex chars => 48 bits\r\n // 48-bit integer is always within Number.MAX_SAFE_INTEGER (2^53 - 1), so parseInt is safe\r\n return parseInt(timeHex, 16);\r\n}\r\n\r\n/**\r\n * Same as extractTimestampFromUuidV7 but returns a Date instance.\r\n *\r\n * @param uuid - UUID v7 string\r\n * @returns Date representing the timestamp encoded in the UUID\r\n */\r\nexport function extractTimestampAsDate(uuid: string): Date {\r\n const ms = extractTimestampFromUuidV7(uuid);\r\n return new Date(ms);\r\n}\r\n\r\n/**\r\n * Convenience: returns an ISO string for the timestamp contained in the UUID v7.\r\n *\r\n * @param uuid - UUID v7 string\r\n * @returns ISO 8601 string (UTC)\r\n */\r\nexport function extractTimestampAsISOString(uuid: string): string {\r\n return extractTimestampAsDate(uuid).toISOString();\r\n}\r\n\r\nexport default {\r\n extractTimestampFromUuidV7,\r\n extractTimestampAsDate,\r\n extractTimestampAsISOString,\r\n};\r\n"],"names":[],"mappings":";;;;AAAA;;;;;;;;AAQG;AAEH,MAAM,SAAS,GAAG,mBAAmB;AAErC;;;;;;;AAOG;AACG,SAAU,0BAA0B,CAAC,IAAY,EAAA;IACnD,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE;AAC/C,QAAA,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC;IACtD;;IAGA,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC;IAEzC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE;AAC7B,QAAA,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC;IAC1C;;;AAIA,IAAA,MAAM,WAAW,GAAG,UAAU,CAAC,EAAE,CAAC;AAClC,IAAA,IAAI,WAAW,KAAK,GAAG,EAAE;AACrB,QAAA,MAAM,IAAI,KAAK,CAAC,+DAA+D,WAAW,CAAA,CAAE,CAAC;IACjG;;AAGA,IAAA,MAAM,OAAO,GAAG,UAAU,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;;AAExC,IAAA,OAAO,QAAQ,CAAC,OAAO,EAAE,EAAE,CAAC;AAChC;AAEA;;;;;AAKG;AACG,SAAU,sBAAsB,CAAC,IAAY,EAAA;AAC/C,IAAA,MAAM,EAAE,GAAG,0BAA0B,CAAC,IAAI,CAAC;AAC3C,IAAA,OAAO,IAAI,IAAI,CAAC,EAAE,CAAC;AACvB;AAEA;;;;;AAKG;AACG,SAAU,2BAA2B,CAAC,IAAY,EAAA;AACpD,IAAA,OAAO,sBAAsB,CAAC,IAAI,CAAC,CAAC,WAAW,EAAE;AACrD;AAEA,YAAe;IACX,0BAA0B;IAC1B,sBAAsB;IACtB,2BAA2B;CAC9B;;;;;;;"}
|
package/dist/index.esm.js
CHANGED
|
@@ -33,12 +33,8 @@ function extractTimestampFromUuidV7(uuid) {
|
|
|
33
33
|
}
|
|
34
34
|
// Timestamp for UUID v7 is the top 48 bits: time_low (8 hex) + time_mid (4 hex) => first 12 hex chars
|
|
35
35
|
const timeHex = normalized.slice(0, 12); // 12 hex chars => 48 bits
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
if (!Number.isFinite(millis)) {
|
|
39
|
-
throw new Error('extracted timestamp is not a finite number');
|
|
40
|
-
}
|
|
41
|
-
return millis;
|
|
36
|
+
// 48-bit integer is always within Number.MAX_SAFE_INTEGER (2^53 - 1), so parseInt is safe
|
|
37
|
+
return parseInt(timeHex, 16);
|
|
42
38
|
}
|
|
43
39
|
/**
|
|
44
40
|
* Same as extractTimestampFromUuidV7 but returns a Date instance.
|
|
@@ -62,7 +58,7 @@ function extractTimestampAsISOString(uuid) {
|
|
|
62
58
|
var index = {
|
|
63
59
|
extractTimestampFromUuidV7,
|
|
64
60
|
extractTimestampAsDate,
|
|
65
|
-
extractTimestampAsISOString
|
|
61
|
+
extractTimestampAsISOString,
|
|
66
62
|
};
|
|
67
63
|
|
|
68
64
|
export { index as default, extractTimestampAsDate, extractTimestampAsISOString, extractTimestampFromUuidV7 };
|
package/dist/index.esm.js.map
CHANGED
|
@@ -1 +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
|
|
1
|
+
{"version":3,"file":"index.esm.js","sources":["../src/index.ts"],"sourcesContent":["/**\r\n * Extract timestamp from UUID v7 strings.\r\n *\r\n * The timestamp for UUID v7 is encoded in the first 48 bits (12 hex chars) of the UUID.\r\n * This module exposes three helpers:\r\n * - extractTimestampFromUuidV7(uuid): number (epoch millis)\r\n * - extractTimestampAsDate(uuid): Date\r\n * - extractTimestampAsISOString(uuid): string\r\n */\r\n\r\nconst HEX_32_RE = /^[0-9a-fA-F]{32}$/;\r\n\r\n/**\r\n * Extracts the epoch milliseconds timestamp from a UUID v7 string.\r\n * Caller must pass a UUID v7 (validation is performed and an error thrown if it's not v7).\r\n *\r\n * @param uuid - UUID string (hyphenated or not)\r\n * @returns epoch milliseconds (number)\r\n * @throws Error if input is not a valid UUID string or not version 7\r\n */\r\nexport function extractTimestampFromUuidV7(uuid: string): number {\r\n if (typeof uuid !== 'string' || uuid.length === 0) {\r\n throw new Error('uuid must be a non-empty string');\r\n }\r\n\r\n // Normalize: remove hyphens\r\n const normalized = uuid.replace(/-/g, '');\r\n\r\n if (!HEX_32_RE.test(normalized)) {\r\n throw new Error('invalid UUID format');\r\n }\r\n\r\n // Version nibble is the first hex char of the 3rd UUID segment.\r\n // In the normalized string it's at index 12 (0-based).\r\n const versionChar = normalized[12];\r\n if (versionChar !== '7') {\r\n throw new Error(`expected UUID v7 (version nibble = 7), got version nibble = ${versionChar}`);\r\n }\r\n\r\n // Timestamp for UUID v7 is the top 48 bits: time_low (8 hex) + time_mid (4 hex) => first 12 hex chars\r\n const timeHex = normalized.slice(0, 12); // 12 hex chars => 48 bits\r\n // 48-bit integer is always within Number.MAX_SAFE_INTEGER (2^53 - 1), so parseInt is safe\r\n return parseInt(timeHex, 16);\r\n}\r\n\r\n/**\r\n * Same as extractTimestampFromUuidV7 but returns a Date instance.\r\n *\r\n * @param uuid - UUID v7 string\r\n * @returns Date representing the timestamp encoded in the UUID\r\n */\r\nexport function extractTimestampAsDate(uuid: string): Date {\r\n const ms = extractTimestampFromUuidV7(uuid);\r\n return new Date(ms);\r\n}\r\n\r\n/**\r\n * Convenience: returns an ISO string for the timestamp contained in the UUID v7.\r\n *\r\n * @param uuid - UUID v7 string\r\n * @returns ISO 8601 string (UTC)\r\n */\r\nexport function extractTimestampAsISOString(uuid: string): string {\r\n return extractTimestampAsDate(uuid).toISOString();\r\n}\r\n\r\nexport default {\r\n extractTimestampFromUuidV7,\r\n extractTimestampAsDate,\r\n extractTimestampAsISOString,\r\n};\r\n"],"names":[],"mappings":"AAAA;;;;;;;;AAQG;AAEH,MAAM,SAAS,GAAG,mBAAmB;AAErC;;;;;;;AAOG;AACG,SAAU,0BAA0B,CAAC,IAAY,EAAA;IACnD,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE;AAC/C,QAAA,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC;IACtD;;IAGA,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC;IAEzC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE;AAC7B,QAAA,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC;IAC1C;;;AAIA,IAAA,MAAM,WAAW,GAAG,UAAU,CAAC,EAAE,CAAC;AAClC,IAAA,IAAI,WAAW,KAAK,GAAG,EAAE;AACrB,QAAA,MAAM,IAAI,KAAK,CAAC,+DAA+D,WAAW,CAAA,CAAE,CAAC;IACjG;;AAGA,IAAA,MAAM,OAAO,GAAG,UAAU,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;;AAExC,IAAA,OAAO,QAAQ,CAAC,OAAO,EAAE,EAAE,CAAC;AAChC;AAEA;;;;;AAKG;AACG,SAAU,sBAAsB,CAAC,IAAY,EAAA;AAC/C,IAAA,MAAM,EAAE,GAAG,0BAA0B,CAAC,IAAI,CAAC;AAC3C,IAAA,OAAO,IAAI,IAAI,CAAC,EAAE,CAAC;AACvB;AAEA;;;;;AAKG;AACG,SAAU,2BAA2B,CAAC,IAAY,EAAA;AACpD,IAAA,OAAO,sBAAsB,CAAC,IAAI,CAAC,CAAC,WAAW,EAAE;AACrD;AAEA,YAAe;IACX,0BAA0B;IAC1B,sBAAsB;IACtB,2BAA2B;CAC9B;;;;"}
|
package/package.json
CHANGED
|
@@ -1,47 +1,63 @@
|
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
|
|
47
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@deciosfernandes/uuidv7-timestamp",
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "Extract epoch timestamp from UUID v7 strings (millis, Date, ISO) — works with any TypeScript or JavaScript project.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.cjs.js",
|
|
7
|
+
"module": "dist/index.esm.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.esm.js",
|
|
13
|
+
"require": "./dist/index.cjs.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "rollup -c",
|
|
22
|
+
"clean": "rimraf dist .turbo cache",
|
|
23
|
+
"test": "vitest",
|
|
24
|
+
"lint": "eslint src test",
|
|
25
|
+
"prepare": "npm run build"
|
|
26
|
+
},
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/deciosfernandes/uuidv7-timestamp.git"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"uuid",
|
|
33
|
+
"uuid-v7",
|
|
34
|
+
"uuidv7",
|
|
35
|
+
"timestamp",
|
|
36
|
+
"extract",
|
|
37
|
+
"epoch",
|
|
38
|
+
"date",
|
|
39
|
+
"iso",
|
|
40
|
+
"typescript",
|
|
41
|
+
"javascript"
|
|
42
|
+
],
|
|
43
|
+
"author": "deciosfernandes",
|
|
44
|
+
"license": "MIT",
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=18"
|
|
47
|
+
},
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
53
|
+
"@types/node": "^22.0.0",
|
|
54
|
+
"eslint": "^9.0.0",
|
|
55
|
+
"rimraf": "^6.0.0",
|
|
56
|
+
"rollup": "^4.0.0",
|
|
57
|
+
"rollup-plugin-dts": "^6.0.0",
|
|
58
|
+
"tslib": "^2.8.0",
|
|
59
|
+
"typescript": "^5.0.0",
|
|
60
|
+
"typescript-eslint": "^8.0.0",
|
|
61
|
+
"vitest": "^3.0.0"
|
|
62
|
+
}
|
|
63
|
+
}
|
package/dist/index.js
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
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
|
-
};
|