@deciosfernandes/uuidv7-timestamp 0.1.1 → 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 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
- - 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
-
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
- 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;
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;
@@ -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 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;;;;;;;"}
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
- 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;
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 };
@@ -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 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;;;;"}
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,48 +1,63 @@
1
- {
2
- "name": "@deciosfernandes/uuidv7-timestamp",
3
- "version": "0.1.1",
4
- "description": "Extract epoch timestamp from UUID v7 strings (millis, Date, ISO) for TypeScript/Angular apps.",
5
- "type": "module",
6
- "main": "dist/index.cjs.js",
7
- "module": "dist/index.esm.js",
8
- "types": "dist/index.d.ts",
9
- "files": [
10
- "dist"
11
- ],
12
- "scripts": {
13
- "build": "tsc && rollup -c",
14
- "clean": "rm -rf dist .turbo cache",
15
- "test": "vitest",
16
- "lint": "eslint . --ext .ts",
17
- "prepare": "npm run build"
18
- },
19
- "repository": {
20
- "type": "git",
21
- "url": "git+https://github.com/deciosfernandes/uuidv7-timestamp.git"
22
- },
23
- "keywords": [
24
- "uuid",
25
- "uuidv7",
26
- "timestamp",
27
- "typescript",
28
- "angular"
29
- ],
30
- "author": "deciosfernandes",
31
- "license": "MIT",
32
- "publishConfig": {
33
- "access": "public"
34
- },
35
- "devDependencies": {
36
- "@rollup/plugin-commonjs": "^24.0.0",
37
- "@rollup/plugin-node-resolve": "^15.0.0",
38
- "@rollup/plugin-typescript": "^12.3.0",
39
- "@types/node": "^20.0.0",
40
- "@typescript-eslint/eslint-plugin": "^5.0.0",
41
- "@typescript-eslint/parser": "^5.0.0",
42
- "eslint": "^8.0.0",
43
- "rollup": "^3.0.0",
44
- "rollup-plugin-dts": "^5.0.0",
45
- "typescript": "^5.0.0",
46
- "vitest": "^2.1.0"
47
- }
48
- }
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
- };