@vizhub/viz-utils 1.5.0 → 1.6.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
@@ -198,6 +198,37 @@ const vizFiles = fileCollectionToVizFiles(fileCollection);
198
198
  // }
199
199
  ```
200
200
 
201
+ #### `setFileText(content: VizContent, fileName: string, fileText: string): VizContent`
202
+
203
+ Sets the text content of a file using an immutable update pattern. If a file with the given fileName already exists, its text is updated while preserving its file ID. If no file with the given fileName exists, a new file is created with a generated file ID.
204
+
205
+ Uses immutable updates, so the original content object is never modified.
206
+
207
+ Throws an error if:
208
+
209
+ - `content` is null or undefined
210
+ - `fileName` is empty or not a string
211
+ - `fileText` is empty or not a string
212
+
213
+ ```typescript
214
+ import { setFileText } from "@vizhub/viz-utils";
215
+
216
+ const vizContent = {
217
+ id: "my-viz-123",
218
+ files: {
219
+ f1: { name: "index.html", text: "<html>Old</html>" },
220
+ },
221
+ };
222
+
223
+ // Update existing file - preserves file ID
224
+ const updated = setFileText(vizContent, "index.html", "<html>New</html>");
225
+ // Result: Original unchanged, new object has updated content
226
+
227
+ // Add new file - generates new file ID
228
+ const withNewFile = setFileText(updated, "style.css", "body { color: red; }");
229
+ // Result: Original unchanged, new object has both files
230
+ ```
231
+
201
232
  ## Types
202
233
 
203
234
  This package uses the following types from `@vizhub/viz-types`:
package/dist/index.cjs CHANGED
@@ -1,2 +1,2 @@
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",a=e=>!e||typeof e!="string"||e.trim()===""?!1:/^.+\.(png|jpg|jpeg|gif|bmp|svg|webp)$/i.test(e),u=()=>typeof globalThis.crypto<"u"?globalThis.crypto:require("node:crypto").webcrypto,f=u(),n=()=>{const e=new Uint8Array(16);return f.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),g=r,s=(e,t)=>{if(e&&e.files){for(const i of Object.keys(e.files))if(e.files[i].name===t)return i}return null},c=(e,t)=>{const i=s(e,t);return i&&e&&e.files?e.files[i].text:null},d=e=>{const t={};if(!e)return t;for(const i of Object.values(e))t[i.name]=i.text;return t},p=e=>Object.entries(e).reduce((t,[i,o])=>(t[r()]={name:i,text:o},t),{}),m=e=>Math.floor(e.getTime()/1e3),T=e=>new Date(e*1e3);exports.dateToTimestamp=m;exports.fileCollectionToVizFiles=p;exports.generateRunId=g;exports.generateVizFileId=r;exports.generateVizId=n;exports.getFileId=s;exports.getFileText=c;exports.isImageFileName=a;exports.isVizId=l;exports.timestampToDate=T;exports.vizFilesToFileCollection=d;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=e=>e.length!==32||!/^[0-9a-f]{32}$/i.test(e)?!1:e[12]==="4",a=e=>!e||typeof e!="string"||e.trim()===""?!1:/^.+\.(png|jpg|jpeg|gif|bmp|svg|webp)$/i.test(e),g=()=>typeof globalThis.crypto<"u"?globalThis.crypto:require("node:crypto").webcrypto,d=g(),l=()=>{const e=new Uint8Array(16);return d.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("")},n=()=>l().substring(0,8),c=n,o=(e,t)=>{if(e&&e.files){for(const i of Object.keys(e.files))if(e.files[i].name===t)return i}return null},p=(e,t)=>{const i=o(e,t);return i&&e&&e.files?e.files[i].text:null},m=(e,t,i)=>{if(e==null)throw new Error("Content cannot be null or undefined");if(typeof t!="string"||t.length===0)throw new Error("fileName must be a non-empty string");if(typeof i!="string"||i.length===0)throw new Error("fileText must be a non-empty string");const r=e.files||{},s=o(e,t);if(s)return{...e,files:{...r,[s]:{...r[s],text:i}}};{const u=n();return{...e,files:{...r,[u]:{name:t,text:i}}}}},F=e=>{const t={};if(!e)return t;for(const i of Object.values(e))t[i.name]=i.text;return t},b=e=>Object.entries(e).reduce((t,[i,r])=>(t[n()]={name:i,text:r},t),{}),y=e=>Math.floor(e.getTime()/1e3),I=e=>new Date(e*1e3);exports.dateToTimestamp=y;exports.fileCollectionToVizFiles=b;exports.generateRunId=c;exports.generateVizFileId=n;exports.generateVizId=l;exports.getFileId=o;exports.getFileText=p;exports.isImageFileName=a;exports.isVizId=f;exports.setFileText=m;exports.timestampToDate=I;exports.vizFilesToFileCollection=F;
2
2
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/isVizId.ts","../src/isImageFileName.ts","../src/generateVizId.ts","../src/generateVizFileId.ts","../src/generateRunId.ts","../src/getFileId.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","// Check if a file name has an image file extension\nexport const isImageFileName = (fileName: string): boolean => {\n if (!fileName || typeof fileName !== \"string\" || fileName.trim() === \"\") {\n return false;\n }\n\n // Check if it's an image file based on extension\n // Also ensure there's at least one character before the dot\n return /^.+\\.(png|jpg|jpeg|gif|bmp|svg|webp)$/i.test(fileName);\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, VizFileId } from \"@vizhub/viz-types\";\n\n// Gets the file id of a file with the given name.\n// Returns null if not found.\nexport const getFileId = (\n content: VizContent,\n fileName: string,\n): VizFileId | 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 fileId;\n }\n }\n }\n return null;\n};\n","import { VizContent } from \"@vizhub/viz-types\";\nimport { getFileId } from \"./getFileId\";\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 const fileId = getFileId(content, fileName);\n if (fileId && content && content.files) {\n return content.files[fileId].text;\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","isImageFileName","fileName","getCryptoObj","cryptoObj","generateVizId","bytes","b","generateVizFileId","generateRunId","getFileId","content","fileId","getFileText","vizFilesToFileCollection","files","fileCollection","file","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,ICnBHC,EAAmBC,GAC1B,CAACA,GAAY,OAAOA,GAAa,UAAYA,EAAS,KAAA,IAAW,GAC5D,GAKF,yCAAyC,KAAKA,CAAQ,ECHzDC,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,EAAY,CACvBC,EACAT,IACqB,CACrB,GAAIS,GAAWA,EAAQ,OACrB,UAAWC,KAAU,OAAO,KAAKD,EAAQ,KAAK,EAE5C,GADaA,EAAQ,MAAMC,CAAM,EACxB,OAASV,EAChB,OAAOU,EAIb,OAAO,IACT,ECZaC,EAAc,CACzBF,EACAT,IACkB,CAClB,MAAMU,EAASF,EAAUC,EAAST,CAAQ,EAC1C,OAAIU,GAAUD,GAAWA,EAAQ,MACxBA,EAAQ,MAAMC,CAAM,EAAE,KAExB,IACT,ECTaE,EAA4BC,GAAqC,CAC5E,MAAMC,EAAiC,CAAA,EAGvC,GAAI,CAACD,EACH,OAAOC,EAIT,UAAWC,KAAQ,OAAO,OAAOF,CAAK,EACpCC,EAAeC,EAAK,IAAI,EAAIA,EAAK,KAGnC,OAAOD,CACT,EChBaE,EAA4BH,GAChC,OAAO,QAAQA,CAAK,EAAE,OAAO,CAACI,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"}
1
+ {"version":3,"file":"index.cjs","sources":["../src/isVizId.ts","../src/isImageFileName.ts","../src/generateVizId.ts","../src/generateVizFileId.ts","../src/generateRunId.ts","../src/getFileId.ts","../src/getFileText.ts","../src/setFileText.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","// Check if a file name has an image file extension\nexport const isImageFileName = (fileName: string): boolean => {\n if (!fileName || typeof fileName !== \"string\" || fileName.trim() === \"\") {\n return false;\n }\n\n // Check if it's an image file based on extension\n // Also ensure there's at least one character before the dot\n return /^.+\\.(png|jpg|jpeg|gif|bmp|svg|webp)$/i.test(fileName);\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, VizFileId } from \"@vizhub/viz-types\";\n\n// Gets the file id of a file with the given name.\n// Returns null if not found.\nexport const getFileId = (\n content: VizContent,\n fileName: string,\n): VizFileId | 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 fileId;\n }\n }\n }\n return null;\n};\n","import { VizContent } from \"@vizhub/viz-types\";\nimport { getFileId } from \"./getFileId\";\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 const fileId = getFileId(content, fileName);\n if (fileId && content && content.files) {\n return content.files[fileId].text;\n }\n return null;\n};\n","import { VizContent } from \"@vizhub/viz-types\";\nimport { getFileId } from \"./getFileId\";\nimport { generateVizFileId } from \"./generateVizFileId\";\n\n/**\n * Sets the text content of a file using an immutable update pattern.\n * If a file with the given fileName exists, its text is updated while preserving its fileId.\n * If no file with the given fileName exists, a new file is created with a generated fileId.\n *\n * @param content - The VizContent object to update (must not be null or undefined)\n * @param fileName - The name of the file (must be a non-empty string)\n * @param fileText - The new text content for the file (must be a non-empty string)\n * @returns A new VizContent object with the updated or newly created file\n * @throws Error if content is null or undefined\n * @throws Error if fileName is empty or not a string\n * @throws Error if fileText is empty or not a string\n */\nexport const setFileText = (\n content: VizContent,\n fileName: string,\n fileText: string,\n): VizContent => {\n // Validate content\n if (content === null || content === undefined) {\n throw new Error(\"Content cannot be null or undefined\");\n }\n\n // Validate fileName\n if (typeof fileName !== \"string\" || fileName.length === 0) {\n throw new Error(\"fileName must be a non-empty string\");\n }\n\n // Validate fileText\n if (typeof fileText !== \"string\" || fileText.length === 0) {\n throw new Error(\"fileText must be a non-empty string\");\n }\n\n // Initialize files object if it doesn't exist\n const files = content.files || {};\n\n // Try to find existing file with this name\n const existingFileId = getFileId(content, fileName);\n\n if (existingFileId) {\n // Update existing file - shallow copy of files object\n return {\n ...content,\n files: {\n ...files,\n [existingFileId]: {\n ...files[existingFileId],\n text: fileText,\n },\n },\n };\n } else {\n // Create new file with generated ID\n const newFileId = generateVizFileId();\n return {\n ...content,\n files: {\n ...files,\n [newFileId]: {\n name: fileName,\n text: fileText,\n },\n },\n };\n }\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","isImageFileName","fileName","getCryptoObj","cryptoObj","generateVizId","bytes","b","generateVizFileId","generateRunId","getFileId","content","fileId","getFileText","setFileText","fileText","files","existingFileId","newFileId","vizFilesToFileCollection","fileCollection","file","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,ICnBHC,EAAmBC,GAC1B,CAACA,GAAY,OAAOA,GAAa,UAAYA,EAAS,KAAA,IAAW,GAC5D,GAKF,yCAAyC,KAAKA,CAAQ,ECHzDC,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,EAAY,CACvBC,EACAT,IACqB,CACrB,GAAIS,GAAWA,EAAQ,OACrB,UAAWC,KAAU,OAAO,KAAKD,EAAQ,KAAK,EAE5C,GADaA,EAAQ,MAAMC,CAAM,EACxB,OAASV,EAChB,OAAOU,EAIb,OAAO,IACT,ECZaC,EAAc,CACzBF,EACAT,IACkB,CAClB,MAAMU,EAASF,EAAUC,EAAST,CAAQ,EAC1C,OAAIU,GAAUD,GAAWA,EAAQ,MACxBA,EAAQ,MAAMC,CAAM,EAAE,KAExB,IACT,ECGaE,EAAc,CACzBH,EACAT,EACAa,IACe,CAEf,GAAIJ,GAAY,KACd,MAAM,IAAI,MAAM,qCAAqC,EAIvD,GAAI,OAAOT,GAAa,UAAYA,EAAS,SAAW,EACtD,MAAM,IAAI,MAAM,qCAAqC,EAIvD,GAAI,OAAOa,GAAa,UAAYA,EAAS,SAAW,EACtD,MAAM,IAAI,MAAM,qCAAqC,EAIvD,MAAMC,EAAQL,EAAQ,OAAS,CAAA,EAGzBM,EAAiBP,EAAUC,EAAST,CAAQ,EAElD,GAAIe,EAEF,MAAO,CACL,GAAGN,EACH,MAAO,CACL,GAAGK,EACH,CAACC,CAAc,EAAG,CAChB,GAAGD,EAAMC,CAAc,EACvB,KAAMF,CAAA,CACR,CACF,EAEG,CAEL,MAAMG,EAAYV,EAAA,EAClB,MAAO,CACL,GAAGG,EACH,MAAO,CACL,GAAGK,EACH,CAACE,CAAS,EAAG,CACX,KAAMhB,EACN,KAAMa,CAAA,CACR,CACF,CAEJ,CACF,EChEaI,EAA4BH,GAAqC,CAC5E,MAAMI,EAAiC,CAAA,EAGvC,GAAI,CAACJ,EACH,OAAOI,EAIT,UAAWC,KAAQ,OAAO,OAAOL,CAAK,EACpCI,EAAeC,EAAK,IAAI,EAAIA,EAAK,KAGnC,OAAOD,CACT,EChBaE,EAA4BN,GAChC,OAAO,QAAQA,CAAK,EAAE,OAAO,CAACO,EAAK,CAACC,EAAMC,CAAI,KACnDF,EAAIf,EAAA,CAAmB,EAAI,CAAE,KAAAgB,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
@@ -5,6 +5,7 @@ export { generateVizFileId } from "./generateVizFileId";
5
5
  export { generateRunId } from "./generateRunId";
6
6
  export { getFileId } from "./getFileId";
7
7
  export { getFileText } from "./getFileText";
8
+ export { setFileText } from "./setFileText";
8
9
  export { vizFilesToFileCollection } from "./vizFilesToFileCollection";
9
10
  export { fileCollectionToVizFiles } from "./fileCollectionToVizFiles";
10
11
  export { dateToTimestamp } from "./dateToTimestamp";
@@ -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,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,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,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,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"}
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,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,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,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,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,37 +1,70 @@
1
- const u = (e) => e.length !== 32 || !/^[0-9a-f]{32}$/i.test(e) ? !1 : e[12] === "4", a = (e) => !e || typeof e != "string" || e.trim() === "" ? !1 : /^.+\.(png|jpg|jpeg|gif|bmp|svg|webp)$/i.test(e), o = () => typeof globalThis.crypto < "u" ? globalThis.crypto : require("node:crypto").webcrypto, s = o(), l = () => {
1
+ const a = (e) => e.length !== 32 || !/^[0-9a-f]{32}$/i.test(e) ? !1 : e[12] === "4", c = (e) => !e || typeof e != "string" || e.trim() === "" ? !1 : /^.+\.(png|jpg|jpeg|gif|bmp|svg|webp)$/i.test(e), f = () => typeof globalThis.crypto < "u" ? globalThis.crypto : require("node:crypto").webcrypto, u = f(), g = () => {
2
2
  const e = new Uint8Array(16);
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(
3
+ 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(
4
4
  ""
5
5
  );
6
- }, n = () => l().substring(0, 8), c = n, f = (e, t) => {
6
+ }, s = () => g().substring(0, 8), d = s, o = (e, t) => {
7
7
  if (e && e.files) {
8
8
  for (const r of Object.keys(e.files))
9
9
  if (e.files[r].name === t)
10
10
  return r;
11
11
  }
12
12
  return null;
13
- }, g = (e, t) => {
14
- const r = f(e, t);
13
+ }, p = (e, t) => {
14
+ const r = o(e, t);
15
15
  return r && e && e.files ? e.files[r].text : null;
16
- }, p = (e) => {
16
+ }, b = (e, t, r) => {
17
+ if (e == null)
18
+ throw new Error("Content cannot be null or undefined");
19
+ if (typeof t != "string" || t.length === 0)
20
+ throw new Error("fileName must be a non-empty string");
21
+ if (typeof r != "string" || r.length === 0)
22
+ throw new Error("fileText must be a non-empty string");
23
+ const i = e.files || {}, n = o(e, t);
24
+ if (n)
25
+ return {
26
+ ...e,
27
+ files: {
28
+ ...i,
29
+ [n]: {
30
+ ...i[n],
31
+ text: r
32
+ }
33
+ }
34
+ };
35
+ {
36
+ const l = s();
37
+ return {
38
+ ...e,
39
+ files: {
40
+ ...i,
41
+ [l]: {
42
+ name: t,
43
+ text: r
44
+ }
45
+ }
46
+ };
47
+ }
48
+ }, y = (e) => {
17
49
  const t = {};
18
50
  if (!e)
19
51
  return t;
20
52
  for (const r of Object.values(e))
21
53
  t[r.name] = r.text;
22
54
  return t;
23
- }, d = (e) => Object.entries(e).reduce((t, [r, i]) => (t[n()] = { name: r, text: i }, t), {}), b = (e) => Math.floor(e.getTime() / 1e3), y = (e) => new Date(e * 1e3);
55
+ }, m = (e) => Object.entries(e).reduce((t, [r, i]) => (t[s()] = { name: r, text: i }, t), {}), h = (e) => Math.floor(e.getTime() / 1e3), w = (e) => new Date(e * 1e3);
24
56
  export {
25
- b as dateToTimestamp,
26
- d as fileCollectionToVizFiles,
27
- c as generateRunId,
28
- n as generateVizFileId,
29
- l as generateVizId,
30
- f as getFileId,
31
- g as getFileText,
32
- a as isImageFileName,
33
- u as isVizId,
34
- y as timestampToDate,
35
- p as vizFilesToFileCollection
57
+ h as dateToTimestamp,
58
+ m as fileCollectionToVizFiles,
59
+ d as generateRunId,
60
+ s as generateVizFileId,
61
+ g as generateVizId,
62
+ o as getFileId,
63
+ p as getFileText,
64
+ c as isImageFileName,
65
+ a as isVizId,
66
+ b as setFileText,
67
+ w as timestampToDate,
68
+ y as vizFilesToFileCollection
36
69
  };
37
70
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/isVizId.ts","../src/isImageFileName.ts","../src/generateVizId.ts","../src/generateVizFileId.ts","../src/generateRunId.ts","../src/getFileId.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","// Check if a file name has an image file extension\nexport const isImageFileName = (fileName: string): boolean => {\n if (!fileName || typeof fileName !== \"string\" || fileName.trim() === \"\") {\n return false;\n }\n\n // Check if it's an image file based on extension\n // Also ensure there's at least one character before the dot\n return /^.+\\.(png|jpg|jpeg|gif|bmp|svg|webp)$/i.test(fileName);\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, VizFileId } from \"@vizhub/viz-types\";\n\n// Gets the file id of a file with the given name.\n// Returns null if not found.\nexport const getFileId = (\n content: VizContent,\n fileName: string,\n): VizFileId | 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 fileId;\n }\n }\n }\n return null;\n};\n","import { VizContent } from \"@vizhub/viz-types\";\nimport { getFileId } from \"./getFileId\";\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 const fileId = getFileId(content, fileName);\n if (fileId && content && content.files) {\n return content.files[fileId].text;\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","isImageFileName","fileName","getCryptoObj","cryptoObj","generateVizId","bytes","b","generateVizFileId","generateRunId","getFileId","content","fileId","getFileText","vizFilesToFileCollection","files","fileCollection","file","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,KCnBHC,IAAkB,CAACC,MAC1B,CAACA,KAAY,OAAOA,KAAa,YAAYA,EAAS,KAAA,MAAW,KAC5D,KAKF,yCAAyC,KAAKA,CAAQ,GCHzDC,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,IAAY,CACvBC,GACAT,MACqB;AACrB,MAAIS,KAAWA,EAAQ;AACrB,eAAWC,KAAU,OAAO,KAAKD,EAAQ,KAAK;AAE5C,UADaA,EAAQ,MAAMC,CAAM,EACxB,SAASV;AAChB,eAAOU;AAAA;AAIb,SAAO;AACT,GCZaC,IAAc,CACzBF,GACAT,MACkB;AAClB,QAAMU,IAASF,EAAUC,GAAST,CAAQ;AAC1C,SAAIU,KAAUD,KAAWA,EAAQ,QACxBA,EAAQ,MAAMC,CAAM,EAAE,OAExB;AACT,GCTaE,IAA2B,CAACC,MAAqC;AAC5E,QAAMC,IAAiC,CAAA;AAGvC,MAAI,CAACD;AACH,WAAOC;AAIT,aAAWC,KAAQ,OAAO,OAAOF,CAAK;AACpC,IAAAC,EAAeC,EAAK,IAAI,IAAIA,EAAK;AAGnC,SAAOD;AACT,GChBaE,IAA2B,CAACH,MAChC,OAAO,QAAQA,CAAK,EAAE,OAAO,CAACI,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;"}
1
+ {"version":3,"file":"index.js","sources":["../src/isVizId.ts","../src/isImageFileName.ts","../src/generateVizId.ts","../src/generateVizFileId.ts","../src/generateRunId.ts","../src/getFileId.ts","../src/getFileText.ts","../src/setFileText.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","// Check if a file name has an image file extension\nexport const isImageFileName = (fileName: string): boolean => {\n if (!fileName || typeof fileName !== \"string\" || fileName.trim() === \"\") {\n return false;\n }\n\n // Check if it's an image file based on extension\n // Also ensure there's at least one character before the dot\n return /^.+\\.(png|jpg|jpeg|gif|bmp|svg|webp)$/i.test(fileName);\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, VizFileId } from \"@vizhub/viz-types\";\n\n// Gets the file id of a file with the given name.\n// Returns null if not found.\nexport const getFileId = (\n content: VizContent,\n fileName: string,\n): VizFileId | 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 fileId;\n }\n }\n }\n return null;\n};\n","import { VizContent } from \"@vizhub/viz-types\";\nimport { getFileId } from \"./getFileId\";\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 const fileId = getFileId(content, fileName);\n if (fileId && content && content.files) {\n return content.files[fileId].text;\n }\n return null;\n};\n","import { VizContent } from \"@vizhub/viz-types\";\nimport { getFileId } from \"./getFileId\";\nimport { generateVizFileId } from \"./generateVizFileId\";\n\n/**\n * Sets the text content of a file using an immutable update pattern.\n * If a file with the given fileName exists, its text is updated while preserving its fileId.\n * If no file with the given fileName exists, a new file is created with a generated fileId.\n *\n * @param content - The VizContent object to update (must not be null or undefined)\n * @param fileName - The name of the file (must be a non-empty string)\n * @param fileText - The new text content for the file (must be a non-empty string)\n * @returns A new VizContent object with the updated or newly created file\n * @throws Error if content is null or undefined\n * @throws Error if fileName is empty or not a string\n * @throws Error if fileText is empty or not a string\n */\nexport const setFileText = (\n content: VizContent,\n fileName: string,\n fileText: string,\n): VizContent => {\n // Validate content\n if (content === null || content === undefined) {\n throw new Error(\"Content cannot be null or undefined\");\n }\n\n // Validate fileName\n if (typeof fileName !== \"string\" || fileName.length === 0) {\n throw new Error(\"fileName must be a non-empty string\");\n }\n\n // Validate fileText\n if (typeof fileText !== \"string\" || fileText.length === 0) {\n throw new Error(\"fileText must be a non-empty string\");\n }\n\n // Initialize files object if it doesn't exist\n const files = content.files || {};\n\n // Try to find existing file with this name\n const existingFileId = getFileId(content, fileName);\n\n if (existingFileId) {\n // Update existing file - shallow copy of files object\n return {\n ...content,\n files: {\n ...files,\n [existingFileId]: {\n ...files[existingFileId],\n text: fileText,\n },\n },\n };\n } else {\n // Create new file with generated ID\n const newFileId = generateVizFileId();\n return {\n ...content,\n files: {\n ...files,\n [newFileId]: {\n name: fileName,\n text: fileText,\n },\n },\n };\n }\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","isImageFileName","fileName","getCryptoObj","cryptoObj","generateVizId","bytes","b","generateVizFileId","generateRunId","getFileId","content","fileId","getFileText","setFileText","fileText","files","existingFileId","newFileId","vizFilesToFileCollection","fileCollection","file","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,KCnBHC,IAAkB,CAACC,MAC1B,CAACA,KAAY,OAAOA,KAAa,YAAYA,EAAS,KAAA,MAAW,KAC5D,KAKF,yCAAyC,KAAKA,CAAQ,GCHzDC,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,IAAY,CACvBC,GACAT,MACqB;AACrB,MAAIS,KAAWA,EAAQ;AACrB,eAAWC,KAAU,OAAO,KAAKD,EAAQ,KAAK;AAE5C,UADaA,EAAQ,MAAMC,CAAM,EACxB,SAASV;AAChB,eAAOU;AAAA;AAIb,SAAO;AACT,GCZaC,IAAc,CACzBF,GACAT,MACkB;AAClB,QAAMU,IAASF,EAAUC,GAAST,CAAQ;AAC1C,SAAIU,KAAUD,KAAWA,EAAQ,QACxBA,EAAQ,MAAMC,CAAM,EAAE,OAExB;AACT,GCGaE,IAAc,CACzBH,GACAT,GACAa,MACe;AAEf,MAAIJ,KAAY;AACd,UAAM,IAAI,MAAM,qCAAqC;AAIvD,MAAI,OAAOT,KAAa,YAAYA,EAAS,WAAW;AACtD,UAAM,IAAI,MAAM,qCAAqC;AAIvD,MAAI,OAAOa,KAAa,YAAYA,EAAS,WAAW;AACtD,UAAM,IAAI,MAAM,qCAAqC;AAIvD,QAAMC,IAAQL,EAAQ,SAAS,CAAA,GAGzBM,IAAiBP,EAAUC,GAAST,CAAQ;AAElD,MAAIe;AAEF,WAAO;AAAA,MACL,GAAGN;AAAA,MACH,OAAO;AAAA,QACL,GAAGK;AAAA,QACH,CAACC,CAAc,GAAG;AAAA,UAChB,GAAGD,EAAMC,CAAc;AAAA,UACvB,MAAMF;AAAA,QAAA;AAAA,MACR;AAAA,IACF;AAEG;AAEL,UAAMG,IAAYV,EAAA;AAClB,WAAO;AAAA,MACL,GAAGG;AAAA,MACH,OAAO;AAAA,QACL,GAAGK;AAAA,QACH,CAACE,CAAS,GAAG;AAAA,UACX,MAAMhB;AAAA,UACN,MAAMa;AAAA,QAAA;AAAA,MACR;AAAA,IACF;AAAA,EAEJ;AACF,GChEaI,IAA2B,CAACH,MAAqC;AAC5E,QAAMI,IAAiC,CAAA;AAGvC,MAAI,CAACJ;AACH,WAAOI;AAIT,aAAWC,KAAQ,OAAO,OAAOL,CAAK;AACpC,IAAAI,EAAeC,EAAK,IAAI,IAAIA,EAAK;AAGnC,SAAOD;AACT,GChBaE,IAA2B,CAACN,MAChC,OAAO,QAAQA,CAAK,EAAE,OAAO,CAACO,GAAK,CAACC,GAAMC,CAAI,OACnDF,EAAIf,EAAA,CAAmB,IAAI,EAAE,MAAAgB,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,16 @@
1
+ import { VizContent } from "@vizhub/viz-types";
2
+ /**
3
+ * Sets the text content of a file using an immutable update pattern.
4
+ * If a file with the given fileName exists, its text is updated while preserving its fileId.
5
+ * If no file with the given fileName exists, a new file is created with a generated fileId.
6
+ *
7
+ * @param content - The VizContent object to update (must not be null or undefined)
8
+ * @param fileName - The name of the file (must be a non-empty string)
9
+ * @param fileText - The new text content for the file (must be a non-empty string)
10
+ * @returns A new VizContent object with the updated or newly created file
11
+ * @throws Error if content is null or undefined
12
+ * @throws Error if fileName is empty or not a string
13
+ * @throws Error if fileText is empty or not a string
14
+ */
15
+ export declare const setFileText: (content: VizContent, fileName: string, fileText: string) => VizContent;
16
+ //# sourceMappingURL=setFileText.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setFileText.d.ts","sourceRoot":"","sources":["../src/setFileText.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAI/C;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,WAAW,GACtB,SAAS,UAAU,EACnB,UAAU,MAAM,EAChB,UAAU,MAAM,KACf,UAgDF,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=setFileText.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setFileText.test.d.ts","sourceRoot":"","sources":["../src/setFileText.test.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vizhub/viz-utils",
3
- "version": "1.5.0",
3
+ "version": "1.6.0",
4
4
  "description": "Utility functions for use across VizHub packages.",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -36,15 +36,15 @@
36
36
  },
37
37
  "homepage": "https://github.com/vizhub-core/viz-utils#readme",
38
38
  "dependencies": {
39
- "@vizhub/viz-types": "^0.4.0"
39
+ "@vizhub/viz-types": "^0.5.0"
40
40
  },
41
41
  "devDependencies": {
42
- "@types/node": "^24.3.0",
43
- "npm-check-updates": "^18.0.2",
44
- "prettier": "^3.6.2",
45
- "typescript": "^5.9.2",
46
- "vite": "^7.1.2",
47
- "vitest": "^3.2.4"
42
+ "@types/node": "^25.0.3",
43
+ "npm-check-updates": "^19.2.1",
44
+ "prettier": "^3.7.4",
45
+ "typescript": "^5.9.3",
46
+ "vite": "^7.3.0",
47
+ "vitest": "^4.0.16"
48
48
  },
49
49
  "publishConfig": {
50
50
  "access": "public"