@vizhub/viz-utils 1.1.0 → 1.4.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/README.md CHANGED
@@ -36,6 +36,16 @@ import { generateVizFileId } from "@vizhub/viz-utils";
36
36
  const newFileId = generateVizFileId(); // e.g. "550e8400"
37
37
  ```
38
38
 
39
+ #### `generateRunId(): string`
40
+
41
+ Generates a unique RunId (an 8-character hexadecimal string) for a run within a visualization, for use as the `vizContent.runId` property.
42
+
43
+ ```typescript
44
+ import { generateRunId } from "@vizhub/viz-utils";
45
+
46
+ const newRunId = generateRunId(); // e.g. "550e8400"
47
+ ```
48
+
39
49
  ### Validation
40
50
 
41
51
  #### `isVizId(str: string): boolean`
@@ -53,6 +63,30 @@ isVizId("12345678901234567890123456789012"); // true if valid
53
63
  isVizId("invalid-id"); // false
54
64
  ```
55
65
 
66
+ ### Time Utilities
67
+
68
+ #### `dateToTimestamp(date: Date): VizTimestamp`
69
+
70
+ Converts a JavaScript Date object to a Unix timestamp (seconds since epoch). The result is floored to remove milliseconds.
71
+
72
+ ```typescript
73
+ import { dateToTimestamp } from "@vizhub/viz-utils";
74
+
75
+ const date = new Date("2023-01-01T00:00:00.000Z");
76
+ const timestamp = dateToTimestamp(date); // 1672531200
77
+ ```
78
+
79
+ #### `timestampToDate(timestamp: VizTimestamp): Date`
80
+
81
+ Converts a Unix timestamp (seconds since epoch) to a JavaScript Date object.
82
+
83
+ ```typescript
84
+ import { timestampToDate } from "@vizhub/viz-utils";
85
+
86
+ const timestamp = 1672531200;
87
+ const date = timestampToDate(timestamp); // 2023-01-01T00:00:00.000Z
88
+ ```
89
+
56
90
  ### File Operations
57
91
 
58
92
  #### `getFileText(content: VizContent, fileName: string): string | null`
@@ -127,6 +161,7 @@ This package uses the following types from `@vizhub/viz-types`:
127
161
  - `VizContent`: The content of a visualization, including its files
128
162
  - `VizFile`: A file with a name and text content
129
163
  - `VizFiles`: A collection of files, indexed by their VizFileId
164
+ - `VizTimestamp`: A Unix timestamp representing seconds since epoch
130
165
 
131
166
  ## License
132
167
 
@@ -0,0 +1,3 @@
1
+ import type { VizTimestamp } from "@vizhub/viz-types";
2
+ export declare const dateToTimestamp: (date: Date) => VizTimestamp;
3
+ //# sourceMappingURL=dateToTimestamp.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dateToTimestamp.d.ts","sourceRoot":"","sources":["../src/dateToTimestamp.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEtD,eAAO,MAAM,eAAe,GAAI,MAAM,IAAI,KAAG,YACV,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=dateToTimestamp.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dateToTimestamp.test.d.ts","sourceRoot":"","sources":["../src/dateToTimestamp.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export declare const generateRunId: () => import("@vizhub/viz-types").VizFileId;
2
+ //# sourceMappingURL=generateRunId.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generateRunId.d.ts","sourceRoot":"","sources":["../src/generateRunId.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,aAAa,6CAAoB,CAAC"}
package/dist/index.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=e=>e.length!==32||!/^[0-9a-f]{32}$/i.test(e)?!1:e[12]==="4",s=()=>typeof globalThis.crypto<"u"?globalThis.crypto:require("node:crypto").webcrypto,u=s(),o=()=>{const e=new Uint8Array(16);return u.getRandomValues(e),e[6]=e[6]&15|64,e[8]=e[8]&63|128,Array.from(e,t=>t.toString(16).padStart(2,"0")).join("")},l=()=>o().substring(0,8),f=(e,t)=>{if(e&&e.files)for(const i of Object.keys(e.files)){const r=e.files[i];if(r.name===t)return r.text}return null},c=e=>{const t={};if(!e)return t;for(const i of Object.values(e))t[i.name]=i.text;return t},a=e=>Object.entries(e).reduce((t,[i,r])=>(t[l()]={name:i,text:r},t),{});exports.fileCollectionToVizFiles=a;exports.generateVizFileId=l;exports.generateVizId=o;exports.getFileText=f;exports.isVizId=n;exports.vizFilesToFileCollection=c;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=e=>e.length!==32||!/^[0-9a-f]{32}$/i.test(e)?!1:e[12]==="4",s=()=>typeof globalThis.crypto<"u"?globalThis.crypto:require("node:crypto").webcrypto,a=s(),n=()=>{const e=new Uint8Array(16);return a.getRandomValues(e),e[6]=e[6]&15|64,e[8]=e[8]&63|128,Array.from(e,t=>t.toString(16).padStart(2,"0")).join("")},r=()=>n().substring(0,8),u=r,c=(e,t)=>{if(e&&e.files)for(const i of Object.keys(e.files)){const o=e.files[i];if(o.name===t)return o.text}return null},f=e=>{const t={};if(!e)return t;for(const i of Object.values(e))t[i.name]=i.text;return t},d=e=>Object.entries(e).reduce((t,[i,o])=>(t[r()]={name:i,text:o},t),{}),g=e=>Math.floor(e.getTime()/1e3),T=e=>new Date(e*1e3);exports.dateToTimestamp=g;exports.fileCollectionToVizFiles=d;exports.generateRunId=u;exports.generateVizFileId=r;exports.generateVizId=n;exports.getFileText=c;exports.isVizId=l;exports.timestampToDate=T;exports.vizFilesToFileCollection=f;
2
2
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/isVizId.ts","../src/generateVizId.ts","../src/generateVizFileId.ts","../src/getFileText.ts","../src/vizFilesToFileCollection.ts","../src/fileCollectionToVizFiles.ts"],"sourcesContent":["import type { VizId } from \"@vizhub/viz-types\";\n\n// Checks if a string was generated by `generateId` from interactors.\nexport const isVizId = (str: string): boolean => {\n // First check if the length is exactly 32 characters\n if (str.length !== 32) {\n return false;\n }\n\n // Regular expression for a 32-character hexadecimal string\n let uuidV4NoDashRegex = /^[0-9a-f]{32}$/i;\n\n // Check if the string matches the regular expression\n if (!uuidV4NoDashRegex.test(str)) {\n return false;\n }\n\n // Check if the 13th character is '4' (indicating UUID v4)\n // and the 17th character is one of '8', '9', 'a', 'b' (indicating the variant)\n return (\n str[12] === \"4\"\n // &&\n // ['8', '9', 'a', 'b'].includes(str[16].toLowerCase())\n );\n};\n","import type { VizId } from \"@vizhub/viz-types\";\n\n// Resolve a Web‑Crypto‑compatible `crypto` object.\n// • Browsers & Node 19 + → globalThis.crypto\n// • Anything else (rare) → use node:crypto's webcrypto\nconst getCryptoObj = (): Crypto => {\n if (typeof globalThis.crypto !== \"undefined\") {\n return globalThis.crypto as Crypto;\n }\n // Use require instead of import to avoid top-level await\n // This will only run in Node.js environments\n return require(\"node:crypto\").webcrypto as Crypto;\n};\n\nconst cryptoObj = getCryptoObj();\n\n/**\n * Generates a dash‑free RFC 4122‑compliant UUID v4 (32 hex chars).\n * Works in all evergreen browsers and Node 19 + without extra deps.\n */\nexport const generateVizId = (): VizId => {\n const bytes = new Uint8Array(16);\n cryptoObj.getRandomValues(bytes);\n\n // RFC 4122: set version (4) and variant (10xx)\n bytes[6] = (bytes[6] & 0x0f) | 0x40;\n bytes[8] = (bytes[8] & 0x3f) | 0x80;\n\n return Array.from(bytes, (b) => b.toString(16).padStart(2, \"0\")).join(\n \"\",\n ) as VizId;\n};\n","import type { VizFileId } from \"@vizhub/viz-types\";\nimport { generateVizId } from \"./generateVizId\";\n\n// Generates a file id\nexport const generateVizFileId = (): VizFileId =>\n generateVizId().substring(0, 8);\n","import { VizContent } from \"@vizhub/viz-types\";\n\n// Gets the text content of a file with the given name.\n// Returns null if not found.\nexport const getFileText = (\n content: VizContent,\n fileName: string,\n): string | null => {\n if (content && content.files) {\n for (const fileId of Object.keys(content.files)) {\n const file = content.files[fileId];\n if (file.name === fileName) {\n return file.text;\n }\n }\n }\n return null;\n};\n","import type { VizFiles, FileCollection } from \"@vizhub/viz-types\";\n\n/**\n * Converts VizContent to FileCollection format.\n */\nexport const vizFilesToFileCollection = (files?: VizFiles): FileCollection => {\n const fileCollection: FileCollection = {};\n\n // Return empty object if files is undefined\n if (!files) {\n return fileCollection;\n }\n\n // Convert each VizFile to the FileCollection format\n for (const file of Object.values(files)) {\n fileCollection[file.name] = file.text;\n }\n\n return fileCollection;\n};\n","import { FileCollection, VizFiles } from \"@vizhub/viz-types\";\nimport { generateVizFileId } from \"./generateVizFileId\";\n\nexport const fileCollectionToVizFiles = (files: FileCollection): VizFiles => {\n return Object.entries(files).reduce((acc, [name, text]) => {\n acc[generateVizFileId()] = { name, text };\n return acc;\n }, {} as VizFiles);\n};\n"],"names":["isVizId","str","getCryptoObj","cryptoObj","generateVizId","bytes","b","generateVizFileId","getFileText","content","fileName","fileId","file","vizFilesToFileCollection","files","fileCollection","fileCollectionToVizFiles","acc","name","text"],"mappings":"gFAGa,MAAAA,EAAWC,GAElBA,EAAI,SAAW,IAQf,CAHoB,kBAGD,KAAKA,CAAG,EACtB,GAMPA,EAAI,EAAE,IAAM,ICfVC,EAAe,IACf,OAAO,WAAW,OAAW,IACxB,WAAW,OAIb,QAAQ,aAAa,EAAE,UAG1BC,EAAYD,EAAa,EAMlBE,EAAgB,IAAa,CAClC,MAAAC,EAAQ,IAAI,WAAW,EAAE,EAC/B,OAAAF,EAAU,gBAAgBE,CAAK,EAG/BA,EAAM,CAAC,EAAKA,EAAM,CAAC,EAAI,GAAQ,GAC/BA,EAAM,CAAC,EAAKA,EAAM,CAAC,EAAI,GAAQ,IAExB,MAAM,KAAKA,EAAQC,GAAMA,EAAE,SAAS,EAAE,EAAE,SAAS,EAAG,GAAG,CAAC,EAAE,KAC/D,EACF,CACF,EC3BaC,EAAoB,IAC/BH,EAAA,EAAgB,UAAU,EAAG,CAAC,ECDnBI,EAAc,CACzBC,EACAC,IACkB,CACd,GAAAD,GAAWA,EAAQ,MACrB,UAAWE,KAAU,OAAO,KAAKF,EAAQ,KAAK,EAAG,CACzC,MAAAG,EAAOH,EAAQ,MAAME,CAAM,EAC7B,GAAAC,EAAK,OAASF,EAChB,OAAOE,EAAK,IACd,CAGG,OAAA,IACT,ECZaC,EAA4BC,GAAqC,CAC5E,MAAMC,EAAiC,CAAC,EAGxC,GAAI,CAACD,EACI,OAAAC,EAIT,UAAWH,KAAQ,OAAO,OAAOE,CAAK,EACrBC,EAAAH,EAAK,IAAI,EAAIA,EAAK,KAG5B,OAAAG,CACT,EChBaC,EAA4BF,GAChC,OAAO,QAAQA,CAAK,EAAE,OAAO,CAACG,EAAK,CAACC,EAAMC,CAAI,KACnDF,EAAIV,EAAmB,CAAA,EAAI,CAAE,KAAAW,EAAM,KAAAC,CAAK,EACjCF,GACN,EAAc"}
1
+ {"version":3,"file":"index.cjs","sources":["../src/isVizId.ts","../src/generateVizId.ts","../src/generateVizFileId.ts","../src/generateRunId.ts","../src/getFileText.ts","../src/vizFilesToFileCollection.ts","../src/fileCollectionToVizFiles.ts","../src/dateToTimestamp.ts","../src/timestampToDate.ts"],"sourcesContent":["import type { VizId } from \"@vizhub/viz-types\";\n\n// Checks if a string was generated by `generateId` from interactors.\nexport const isVizId = (str: string): boolean => {\n // First check if the length is exactly 32 characters\n if (str.length !== 32) {\n return false;\n }\n\n // Regular expression for a 32-character hexadecimal string\n let uuidV4NoDashRegex = /^[0-9a-f]{32}$/i;\n\n // Check if the string matches the regular expression\n if (!uuidV4NoDashRegex.test(str)) {\n return false;\n }\n\n // Check if the 13th character is '4' (indicating UUID v4)\n // and the 17th character is one of '8', '9', 'a', 'b' (indicating the variant)\n return (\n str[12] === \"4\"\n // &&\n // ['8', '9', 'a', 'b'].includes(str[16].toLowerCase())\n );\n};\n","import type { VizId } from \"@vizhub/viz-types\";\n\n// Resolve a Web‑Crypto‑compatible `crypto` object.\n// • Browsers & Node 19 + → globalThis.crypto\n// • Anything else (rare) → use node:crypto's webcrypto\nconst getCryptoObj = (): Crypto => {\n if (typeof globalThis.crypto !== \"undefined\") {\n return globalThis.crypto as Crypto;\n }\n // Use require instead of import to avoid top-level await\n // This will only run in Node.js environments\n return require(\"node:crypto\").webcrypto as Crypto;\n};\n\nconst cryptoObj = getCryptoObj();\n\n/**\n * Generates a dash‑free RFC 4122‑compliant UUID v4 (32 hex chars).\n * Works in all evergreen browsers and Node 19 + without extra deps.\n */\nexport const generateVizId = (): VizId => {\n const bytes = new Uint8Array(16);\n cryptoObj.getRandomValues(bytes);\n\n // RFC 4122: set version (4) and variant (10xx)\n bytes[6] = (bytes[6] & 0x0f) | 0x40;\n bytes[8] = (bytes[8] & 0x3f) | 0x80;\n\n return Array.from(bytes, (b) => b.toString(16).padStart(2, \"0\")).join(\n \"\",\n ) as VizId;\n};\n","import type { VizFileId } from \"@vizhub/viz-types\";\nimport { generateVizId } from \"./generateVizId\";\n\n// Generates a file id\nexport const generateVizFileId = (): VizFileId =>\n generateVizId().substring(0, 8);\n","import { generateVizFileId } from \"./generateVizFileId\";\nexport const generateRunId = generateVizFileId;\n","import { VizContent } from \"@vizhub/viz-types\";\n\n// Gets the text content of a file with the given name.\n// Returns null if not found.\nexport const getFileText = (\n content: VizContent,\n fileName: string,\n): string | null => {\n if (content && content.files) {\n for (const fileId of Object.keys(content.files)) {\n const file = content.files[fileId];\n if (file.name === fileName) {\n return file.text;\n }\n }\n }\n return null;\n};\n","import type { VizFiles, FileCollection } from \"@vizhub/viz-types\";\n\n/**\n * Converts VizContent to FileCollection format.\n */\nexport const vizFilesToFileCollection = (files?: VizFiles): FileCollection => {\n const fileCollection: FileCollection = {};\n\n // Return empty object if files is undefined\n if (!files) {\n return fileCollection;\n }\n\n // Convert each VizFile to the FileCollection format\n for (const file of Object.values(files)) {\n fileCollection[file.name] = file.text;\n }\n\n return fileCollection;\n};\n","import { FileCollection, VizFiles } from \"@vizhub/viz-types\";\nimport { generateVizFileId } from \"./generateVizFileId\";\n\nexport const fileCollectionToVizFiles = (files: FileCollection): VizFiles => {\n return Object.entries(files).reduce((acc, [name, text]) => {\n acc[generateVizFileId()] = { name, text };\n return acc;\n }, {} as VizFiles);\n};\n","import type { VizTimestamp } from \"@vizhub/viz-types\";\n\nexport const dateToTimestamp = (date: Date): VizTimestamp =>\n Math.floor(date.getTime() / 1000);\n","import type { VizTimestamp } from \"@vizhub/viz-types\";\n\nexport const timestampToDate = (timestamp: VizTimestamp): Date =>\n new Date(timestamp * 1000);\n"],"names":["isVizId","str","getCryptoObj","cryptoObj","generateVizId","bytes","b","generateVizFileId","generateRunId","getFileText","content","fileName","fileId","file","vizFilesToFileCollection","files","fileCollection","fileCollectionToVizFiles","acc","name","text","dateToTimestamp","date","timestampToDate","timestamp"],"mappings":"gFAGO,MAAMA,EAAWC,GAElBA,EAAI,SAAW,IAQf,CAHoB,kBAGD,KAAKA,CAAG,EACtB,GAMPA,EAAI,EAAE,IAAM,ICfVC,EAAe,IACf,OAAO,WAAW,OAAW,IACxB,WAAW,OAIb,QAAQ,aAAa,EAAE,UAG1BC,EAAYD,EAAA,EAMLE,EAAgB,IAAa,CACxC,MAAMC,EAAQ,IAAI,WAAW,EAAE,EAC/B,OAAAF,EAAU,gBAAgBE,CAAK,EAG/BA,EAAM,CAAC,EAAKA,EAAM,CAAC,EAAI,GAAQ,GAC/BA,EAAM,CAAC,EAAKA,EAAM,CAAC,EAAI,GAAQ,IAExB,MAAM,KAAKA,EAAQC,GAAMA,EAAE,SAAS,EAAE,EAAE,SAAS,EAAG,GAAG,CAAC,EAAE,KAC/D,EAAA,CAEJ,EC3BaC,EAAoB,IAC/BH,EAAA,EAAgB,UAAU,EAAG,CAAC,ECJnBI,EAAgBD,ECGhBE,EAAc,CACzBC,EACAC,IACkB,CAClB,GAAID,GAAWA,EAAQ,MACrB,UAAWE,KAAU,OAAO,KAAKF,EAAQ,KAAK,EAAG,CAC/C,MAAMG,EAAOH,EAAQ,MAAME,CAAM,EACjC,GAAIC,EAAK,OAASF,EAChB,OAAOE,EAAK,IAEhB,CAEF,OAAO,IACT,ECZaC,EAA4BC,GAAqC,CAC5E,MAAMC,EAAiC,CAAA,EAGvC,GAAI,CAACD,EACH,OAAOC,EAIT,UAAWH,KAAQ,OAAO,OAAOE,CAAK,EACpCC,EAAeH,EAAK,IAAI,EAAIA,EAAK,KAGnC,OAAOG,CACT,EChBaC,EAA4BF,GAChC,OAAO,QAAQA,CAAK,EAAE,OAAO,CAACG,EAAK,CAACC,EAAMC,CAAI,KACnDF,EAAIX,EAAA,CAAmB,EAAI,CAAE,KAAAY,EAAM,KAAAC,CAAA,EAC5BF,GACN,CAAA,CAAc,ECLNG,EAAmBC,GAC9B,KAAK,MAAMA,EAAK,QAAA,EAAY,GAAI,ECDrBC,EAAmBC,GAC9B,IAAI,KAAKA,EAAY,GAAI"}
package/dist/index.d.ts CHANGED
@@ -1,7 +1,10 @@
1
1
  export { isVizId } from "./isVizId";
2
2
  export { generateVizId } from "./generateVizId";
3
3
  export { generateVizFileId } from "./generateVizFileId";
4
+ export { generateRunId } from "./generateRunId";
4
5
  export { getFileText } from "./getFileText";
5
6
  export { vizFilesToFileCollection } from "./vizFilesToFileCollection";
6
7
  export { fileCollectionToVizFiles } from "./fileCollectionToVizFiles";
8
+ export { dateToTimestamp } from "./dateToTimestamp";
9
+ export { timestampToDate } from "./timestampToDate";
7
10
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,wBAAwB,EAAE,MAAM,4BAA4B,CAAC;AACtE,OAAO,EAAE,wBAAwB,EAAE,MAAM,4BAA4B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,wBAAwB,EAAE,MAAM,4BAA4B,CAAC;AACtE,OAAO,EAAE,wBAAwB,EAAE,MAAM,4BAA4B,CAAC;AACtE,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC"}
package/dist/index.js CHANGED
@@ -1,14 +1,14 @@
1
- const f = (e) => e.length !== 32 || !/^[0-9a-f]{32}$/i.test(e) ? !1 : e[12] === "4", n = () => typeof globalThis.crypto < "u" ? globalThis.crypto : require("node:crypto").webcrypto, o = n(), l = () => {
1
+ const a = (e) => e.length !== 32 || !/^[0-9a-f]{32}$/i.test(e) ? !1 : e[12] === "4", i = () => typeof globalThis.crypto < "u" ? globalThis.crypto : require("node:crypto").webcrypto, s = i(), l = () => {
2
2
  const e = new Uint8Array(16);
3
- return o.getRandomValues(e), e[6] = e[6] & 15 | 64, e[8] = e[8] & 63 | 128, Array.from(e, (t) => t.toString(16).padStart(2, "0")).join(
3
+ return s.getRandomValues(e), e[6] = e[6] & 15 | 64, e[8] = e[8] & 63 | 128, Array.from(e, (t) => t.toString(16).padStart(2, "0")).join(
4
4
  ""
5
5
  );
6
- }, s = () => l().substring(0, 8), u = (e, t) => {
6
+ }, n = () => l().substring(0, 8), f = n, u = (e, t) => {
7
7
  if (e && e.files)
8
8
  for (const r of Object.keys(e.files)) {
9
- const i = e.files[r];
10
- if (i.name === t)
11
- return i.text;
9
+ const o = e.files[r];
10
+ if (o.name === t)
11
+ return o.text;
12
12
  }
13
13
  return null;
14
14
  }, c = (e) => {
@@ -18,13 +18,16 @@ const f = (e) => e.length !== 32 || !/^[0-9a-f]{32}$/i.test(e) ? !1 : e[12] ===
18
18
  for (const r of Object.values(e))
19
19
  t[r.name] = r.text;
20
20
  return t;
21
- }, a = (e) => Object.entries(e).reduce((t, [r, i]) => (t[s()] = { name: r, text: i }, t), {});
21
+ }, g = (e) => Object.entries(e).reduce((t, [r, o]) => (t[n()] = { name: r, text: o }, t), {}), d = (e) => Math.floor(e.getTime() / 1e3), b = (e) => new Date(e * 1e3);
22
22
  export {
23
- a as fileCollectionToVizFiles,
24
- s as generateVizFileId,
23
+ d as dateToTimestamp,
24
+ g as fileCollectionToVizFiles,
25
+ f as generateRunId,
26
+ n as generateVizFileId,
25
27
  l as generateVizId,
26
28
  u as getFileText,
27
- f as isVizId,
29
+ a as isVizId,
30
+ b as timestampToDate,
28
31
  c as vizFilesToFileCollection
29
32
  };
30
33
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/isVizId.ts","../src/generateVizId.ts","../src/generateVizFileId.ts","../src/getFileText.ts","../src/vizFilesToFileCollection.ts","../src/fileCollectionToVizFiles.ts"],"sourcesContent":["import type { VizId } from \"@vizhub/viz-types\";\n\n// Checks if a string was generated by `generateId` from interactors.\nexport const isVizId = (str: string): boolean => {\n // First check if the length is exactly 32 characters\n if (str.length !== 32) {\n return false;\n }\n\n // Regular expression for a 32-character hexadecimal string\n let uuidV4NoDashRegex = /^[0-9a-f]{32}$/i;\n\n // Check if the string matches the regular expression\n if (!uuidV4NoDashRegex.test(str)) {\n return false;\n }\n\n // Check if the 13th character is '4' (indicating UUID v4)\n // and the 17th character is one of '8', '9', 'a', 'b' (indicating the variant)\n return (\n str[12] === \"4\"\n // &&\n // ['8', '9', 'a', 'b'].includes(str[16].toLowerCase())\n );\n};\n","import type { VizId } from \"@vizhub/viz-types\";\n\n// Resolve a Web‑Crypto‑compatible `crypto` object.\n// • Browsers & Node 19 + → globalThis.crypto\n// • Anything else (rare) → use node:crypto's webcrypto\nconst getCryptoObj = (): Crypto => {\n if (typeof globalThis.crypto !== \"undefined\") {\n return globalThis.crypto as Crypto;\n }\n // Use require instead of import to avoid top-level await\n // This will only run in Node.js environments\n return require(\"node:crypto\").webcrypto as Crypto;\n};\n\nconst cryptoObj = getCryptoObj();\n\n/**\n * Generates a dash‑free RFC 4122‑compliant UUID v4 (32 hex chars).\n * Works in all evergreen browsers and Node 19 + without extra deps.\n */\nexport const generateVizId = (): VizId => {\n const bytes = new Uint8Array(16);\n cryptoObj.getRandomValues(bytes);\n\n // RFC 4122: set version (4) and variant (10xx)\n bytes[6] = (bytes[6] & 0x0f) | 0x40;\n bytes[8] = (bytes[8] & 0x3f) | 0x80;\n\n return Array.from(bytes, (b) => b.toString(16).padStart(2, \"0\")).join(\n \"\",\n ) as VizId;\n};\n","import type { VizFileId } from \"@vizhub/viz-types\";\nimport { generateVizId } from \"./generateVizId\";\n\n// Generates a file id\nexport const generateVizFileId = (): VizFileId =>\n generateVizId().substring(0, 8);\n","import { VizContent } from \"@vizhub/viz-types\";\n\n// Gets the text content of a file with the given name.\n// Returns null if not found.\nexport const getFileText = (\n content: VizContent,\n fileName: string,\n): string | null => {\n if (content && content.files) {\n for (const fileId of Object.keys(content.files)) {\n const file = content.files[fileId];\n if (file.name === fileName) {\n return file.text;\n }\n }\n }\n return null;\n};\n","import type { VizFiles, FileCollection } from \"@vizhub/viz-types\";\n\n/**\n * Converts VizContent to FileCollection format.\n */\nexport const vizFilesToFileCollection = (files?: VizFiles): FileCollection => {\n const fileCollection: FileCollection = {};\n\n // Return empty object if files is undefined\n if (!files) {\n return fileCollection;\n }\n\n // Convert each VizFile to the FileCollection format\n for (const file of Object.values(files)) {\n fileCollection[file.name] = file.text;\n }\n\n return fileCollection;\n};\n","import { FileCollection, VizFiles } from \"@vizhub/viz-types\";\nimport { generateVizFileId } from \"./generateVizFileId\";\n\nexport const fileCollectionToVizFiles = (files: FileCollection): VizFiles => {\n return Object.entries(files).reduce((acc, [name, text]) => {\n acc[generateVizFileId()] = { name, text };\n return acc;\n }, {} as VizFiles);\n};\n"],"names":["isVizId","str","getCryptoObj","cryptoObj","generateVizId","bytes","b","generateVizFileId","getFileText","content","fileName","fileId","file","vizFilesToFileCollection","files","fileCollection","fileCollectionToVizFiles","acc","name","text"],"mappings":"AAGa,MAAAA,IAAU,CAACC,MAElBA,EAAI,WAAW,MAQf,CAHoB,kBAGD,KAAKA,CAAG,IACtB,KAMPA,EAAI,EAAE,MAAM,KCfVC,IAAe,MACf,OAAO,WAAW,SAAW,MACxB,WAAW,SAIb,QAAQ,aAAa,EAAE,WAG1BC,IAAYD,EAAa,GAMlBE,IAAgB,MAAa;AAClC,QAAAC,IAAQ,IAAI,WAAW,EAAE;AAC/B,SAAAF,EAAU,gBAAgBE,CAAK,GAG/BA,EAAM,CAAC,IAAKA,EAAM,CAAC,IAAI,KAAQ,IAC/BA,EAAM,CAAC,IAAKA,EAAM,CAAC,IAAI,KAAQ,KAExB,MAAM,KAAKA,GAAO,CAACC,MAAMA,EAAE,SAAS,EAAE,EAAE,SAAS,GAAG,GAAG,CAAC,EAAE;AAAA,IAC/D;AAAA,EACF;AACF,GC3BaC,IAAoB,MAC/BH,EAAA,EAAgB,UAAU,GAAG,CAAC,GCDnBI,IAAc,CACzBC,GACAC,MACkB;AACd,MAAAD,KAAWA,EAAQ;AACrB,eAAWE,KAAU,OAAO,KAAKF,EAAQ,KAAK,GAAG;AACzC,YAAAG,IAAOH,EAAQ,MAAME,CAAM;AAC7B,UAAAC,EAAK,SAASF;AAChB,eAAOE,EAAK;AAAA,IACd;AAGG,SAAA;AACT,GCZaC,IAA2B,CAACC,MAAqC;AAC5E,QAAMC,IAAiC,CAAC;AAGxC,MAAI,CAACD;AACI,WAAAC;AAIT,aAAWH,KAAQ,OAAO,OAAOE,CAAK;AACrB,IAAAC,EAAAH,EAAK,IAAI,IAAIA,EAAK;AAG5B,SAAAG;AACT,GChBaC,IAA2B,CAACF,MAChC,OAAO,QAAQA,CAAK,EAAE,OAAO,CAACG,GAAK,CAACC,GAAMC,CAAI,OACnDF,EAAIV,EAAmB,CAAA,IAAI,EAAE,MAAAW,GAAM,MAAAC,EAAK,GACjCF,IACN,EAAc;"}
1
+ {"version":3,"file":"index.js","sources":["../src/isVizId.ts","../src/generateVizId.ts","../src/generateVizFileId.ts","../src/generateRunId.ts","../src/getFileText.ts","../src/vizFilesToFileCollection.ts","../src/fileCollectionToVizFiles.ts","../src/dateToTimestamp.ts","../src/timestampToDate.ts"],"sourcesContent":["import type { VizId } from \"@vizhub/viz-types\";\n\n// Checks if a string was generated by `generateId` from interactors.\nexport const isVizId = (str: string): boolean => {\n // First check if the length is exactly 32 characters\n if (str.length !== 32) {\n return false;\n }\n\n // Regular expression for a 32-character hexadecimal string\n let uuidV4NoDashRegex = /^[0-9a-f]{32}$/i;\n\n // Check if the string matches the regular expression\n if (!uuidV4NoDashRegex.test(str)) {\n return false;\n }\n\n // Check if the 13th character is '4' (indicating UUID v4)\n // and the 17th character is one of '8', '9', 'a', 'b' (indicating the variant)\n return (\n str[12] === \"4\"\n // &&\n // ['8', '9', 'a', 'b'].includes(str[16].toLowerCase())\n );\n};\n","import type { VizId } from \"@vizhub/viz-types\";\n\n// Resolve a Web‑Crypto‑compatible `crypto` object.\n// • Browsers & Node 19 + → globalThis.crypto\n// • Anything else (rare) → use node:crypto's webcrypto\nconst getCryptoObj = (): Crypto => {\n if (typeof globalThis.crypto !== \"undefined\") {\n return globalThis.crypto as Crypto;\n }\n // Use require instead of import to avoid top-level await\n // This will only run in Node.js environments\n return require(\"node:crypto\").webcrypto as Crypto;\n};\n\nconst cryptoObj = getCryptoObj();\n\n/**\n * Generates a dash‑free RFC 4122‑compliant UUID v4 (32 hex chars).\n * Works in all evergreen browsers and Node 19 + without extra deps.\n */\nexport const generateVizId = (): VizId => {\n const bytes = new Uint8Array(16);\n cryptoObj.getRandomValues(bytes);\n\n // RFC 4122: set version (4) and variant (10xx)\n bytes[6] = (bytes[6] & 0x0f) | 0x40;\n bytes[8] = (bytes[8] & 0x3f) | 0x80;\n\n return Array.from(bytes, (b) => b.toString(16).padStart(2, \"0\")).join(\n \"\",\n ) as VizId;\n};\n","import type { VizFileId } from \"@vizhub/viz-types\";\nimport { generateVizId } from \"./generateVizId\";\n\n// Generates a file id\nexport const generateVizFileId = (): VizFileId =>\n generateVizId().substring(0, 8);\n","import { generateVizFileId } from \"./generateVizFileId\";\nexport const generateRunId = generateVizFileId;\n","import { VizContent } from \"@vizhub/viz-types\";\n\n// Gets the text content of a file with the given name.\n// Returns null if not found.\nexport const getFileText = (\n content: VizContent,\n fileName: string,\n): string | null => {\n if (content && content.files) {\n for (const fileId of Object.keys(content.files)) {\n const file = content.files[fileId];\n if (file.name === fileName) {\n return file.text;\n }\n }\n }\n return null;\n};\n","import type { VizFiles, FileCollection } from \"@vizhub/viz-types\";\n\n/**\n * Converts VizContent to FileCollection format.\n */\nexport const vizFilesToFileCollection = (files?: VizFiles): FileCollection => {\n const fileCollection: FileCollection = {};\n\n // Return empty object if files is undefined\n if (!files) {\n return fileCollection;\n }\n\n // Convert each VizFile to the FileCollection format\n for (const file of Object.values(files)) {\n fileCollection[file.name] = file.text;\n }\n\n return fileCollection;\n};\n","import { FileCollection, VizFiles } from \"@vizhub/viz-types\";\nimport { generateVizFileId } from \"./generateVizFileId\";\n\nexport const fileCollectionToVizFiles = (files: FileCollection): VizFiles => {\n return Object.entries(files).reduce((acc, [name, text]) => {\n acc[generateVizFileId()] = { name, text };\n return acc;\n }, {} as VizFiles);\n};\n","import type { VizTimestamp } from \"@vizhub/viz-types\";\n\nexport const dateToTimestamp = (date: Date): VizTimestamp =>\n Math.floor(date.getTime() / 1000);\n","import type { VizTimestamp } from \"@vizhub/viz-types\";\n\nexport const timestampToDate = (timestamp: VizTimestamp): Date =>\n new Date(timestamp * 1000);\n"],"names":["isVizId","str","getCryptoObj","cryptoObj","generateVizId","bytes","b","generateVizFileId","generateRunId","getFileText","content","fileName","fileId","file","vizFilesToFileCollection","files","fileCollection","fileCollectionToVizFiles","acc","name","text","dateToTimestamp","date","timestampToDate","timestamp"],"mappings":"AAGO,MAAMA,IAAU,CAACC,MAElBA,EAAI,WAAW,MAQf,CAHoB,kBAGD,KAAKA,CAAG,IACtB,KAMPA,EAAI,EAAE,MAAM,KCfVC,IAAe,MACf,OAAO,WAAW,SAAW,MACxB,WAAW,SAIb,QAAQ,aAAa,EAAE,WAG1BC,IAAYD,EAAA,GAMLE,IAAgB,MAAa;AACxC,QAAMC,IAAQ,IAAI,WAAW,EAAE;AAC/B,SAAAF,EAAU,gBAAgBE,CAAK,GAG/BA,EAAM,CAAC,IAAKA,EAAM,CAAC,IAAI,KAAQ,IAC/BA,EAAM,CAAC,IAAKA,EAAM,CAAC,IAAI,KAAQ,KAExB,MAAM,KAAKA,GAAO,CAACC,MAAMA,EAAE,SAAS,EAAE,EAAE,SAAS,GAAG,GAAG,CAAC,EAAE;AAAA,IAC/D;AAAA,EAAA;AAEJ,GC3BaC,IAAoB,MAC/BH,EAAA,EAAgB,UAAU,GAAG,CAAC,GCJnBI,IAAgBD,GCGhBE,IAAc,CACzBC,GACAC,MACkB;AAClB,MAAID,KAAWA,EAAQ;AACrB,eAAWE,KAAU,OAAO,KAAKF,EAAQ,KAAK,GAAG;AAC/C,YAAMG,IAAOH,EAAQ,MAAME,CAAM;AACjC,UAAIC,EAAK,SAASF;AAChB,eAAOE,EAAK;AAAA,IAEhB;AAEF,SAAO;AACT,GCZaC,IAA2B,CAACC,MAAqC;AAC5E,QAAMC,IAAiC,CAAA;AAGvC,MAAI,CAACD;AACH,WAAOC;AAIT,aAAWH,KAAQ,OAAO,OAAOE,CAAK;AACpC,IAAAC,EAAeH,EAAK,IAAI,IAAIA,EAAK;AAGnC,SAAOG;AACT,GChBaC,IAA2B,CAACF,MAChC,OAAO,QAAQA,CAAK,EAAE,OAAO,CAACG,GAAK,CAACC,GAAMC,CAAI,OACnDF,EAAIX,EAAA,CAAmB,IAAI,EAAE,MAAAY,GAAM,MAAAC,EAAA,GAC5BF,IACN,CAAA,CAAc,GCLNG,IAAkB,CAACC,MAC9B,KAAK,MAAMA,EAAK,QAAA,IAAY,GAAI,GCDrBC,IAAkB,CAACC,MAC9B,IAAI,KAAKA,IAAY,GAAI;"}
@@ -0,0 +1,3 @@
1
+ import type { VizTimestamp } from "@vizhub/viz-types";
2
+ export declare const timestampToDate: (timestamp: VizTimestamp) => Date;
3
+ //# sourceMappingURL=timestampToDate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"timestampToDate.d.ts","sourceRoot":"","sources":["../src/timestampToDate.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEtD,eAAO,MAAM,eAAe,GAAI,WAAW,YAAY,KAAG,IAC9B,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=timestampToDate.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"timestampToDate.test.d.ts","sourceRoot":"","sources":["../src/timestampToDate.test.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vizhub/viz-utils",
3
- "version": "1.1.0",
3
+ "version": "1.4.0",
4
4
  "description": "Utility functions for use across VizHub packages.",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -36,13 +36,15 @@
36
36
  },
37
37
  "homepage": "https://github.com/vizhub-core/viz-utils#readme",
38
38
  "dependencies": {
39
- "@vizhub/viz-types": "^0.1.0"
39
+ "@vizhub/viz-types": "^0.4.0"
40
40
  },
41
41
  "devDependencies": {
42
- "npm-check-updates": "^17.1.16",
43
- "prettier": "^3.5.3",
44
- "vite": "^6.2.5",
45
- "vitest": "^3.1.1"
42
+ "@types/node": "^24.1.0",
43
+ "npm-check-updates": "^18.0.2",
44
+ "prettier": "^3.6.2",
45
+ "typescript": "^5.9.2",
46
+ "vite": "^7.0.6",
47
+ "vitest": "^3.2.4"
46
48
  },
47
49
  "publishConfig": {
48
50
  "access": "public"