@vizhub/viz-utils 0.1.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/README.md +65 -12
  2. package/dist/fileCollectionToVizFiles.d.ts +3 -0
  3. package/dist/fileCollectionToVizFiles.d.ts.map +1 -0
  4. package/dist/fileCollectionToVizFiles.test.d.ts +2 -0
  5. package/dist/fileCollectionToVizFiles.test.d.ts.map +1 -0
  6. package/dist/generateVizFileId.d.ts +1 -0
  7. package/dist/generateVizFileId.d.ts.map +1 -0
  8. package/dist/generateVizFileId.test.d.ts +1 -0
  9. package/dist/generateVizFileId.test.d.ts.map +1 -0
  10. package/dist/generateVizId.d.ts +5 -0
  11. package/dist/generateVizId.d.ts.map +1 -0
  12. package/dist/generateVizId.test.d.ts +1 -0
  13. package/dist/generateVizId.test.d.ts.map +1 -0
  14. package/dist/getFileText.d.ts +1 -0
  15. package/dist/getFileText.d.ts.map +1 -0
  16. package/dist/getFileText.test.d.ts +1 -0
  17. package/dist/getFileText.test.d.ts.map +1 -0
  18. package/dist/index.cjs +2 -0
  19. package/dist/index.cjs.map +1 -0
  20. package/dist/index.d.ts +3 -0
  21. package/dist/index.d.ts.map +1 -0
  22. package/dist/index.js +30 -12
  23. package/dist/index.js.map +1 -0
  24. package/dist/isVizId.d.ts +1 -0
  25. package/dist/isVizId.d.ts.map +1 -0
  26. package/dist/isVizId.test.d.ts +1 -0
  27. package/dist/isVizId.test.d.ts.map +1 -0
  28. package/dist/vizFilesToFileCollection.d.ts +6 -0
  29. package/dist/vizFilesToFileCollection.d.ts.map +1 -0
  30. package/dist/vizFilesToFileCollection.test.d.ts +2 -0
  31. package/dist/vizFilesToFileCollection.test.d.ts.map +1 -0
  32. package/package.json +20 -7
  33. package/dist/generateVizFileId.js +0 -9
  34. package/dist/generateVizFileId.test.js +0 -21
  35. package/dist/generateVizId.js +0 -14
  36. package/dist/generateVizId.test.js +0 -30
  37. package/dist/getFileText.js +0 -18
  38. package/dist/getFileText.test.js +0 -74
  39. package/dist/isVizId.js +0 -23
  40. package/dist/isVizId.test.js +0 -25
package/README.md CHANGED
@@ -16,34 +16,38 @@ This package depends on `@vizhub/viz-types`, which defines the TypeScript types
16
16
 
17
17
  ### ID Generation
18
18
 
19
- #### `generateVizId()`
19
+ #### `generateVizId(): string`
20
20
 
21
- Generates a unique VizId (a UUID v4 string without dashes) for a visualization.
21
+ Generates a unique VizId (a UUID v4 string without dashes) for a visualization. The generated ID is a 32-character hexadecimal string with the 13th character always being "4" (following UUID v4 format).
22
22
 
23
23
  ```typescript
24
- import { generateVizId } from '@vizhub/viz-utils';
24
+ import { generateVizId } from "@vizhub/viz-utils";
25
25
 
26
- const newVizId = generateVizId(); // e.g. "12345678901234567890123456789012"
26
+ const newVizId = generateVizId(); // e.g. "550e8400e29b41d4a716446655440000"
27
27
  ```
28
28
 
29
- #### `generateVizFileId()`
29
+ #### `generateVizFileId(): string`
30
30
 
31
- Generates a unique VizFileId (an 8-character substring of a VizId) for a file within a visualization.
31
+ Generates a unique VizFileId (an 8-character hexadecimal string) for a file within a visualization.
32
32
 
33
33
  ```typescript
34
- import { generateVizFileId } from '@vizhub/viz-utils';
34
+ import { generateVizFileId } from "@vizhub/viz-utils";
35
35
 
36
- const newFileId = generateVizFileId(); // e.g. "12345678"
36
+ const newFileId = generateVizFileId(); // e.g. "550e8400"
37
37
  ```
38
38
 
39
39
  ### Validation
40
40
 
41
41
  #### `isVizId(str: string): boolean`
42
42
 
43
- Checks if a string is a valid VizId.
43
+ Checks if a string is a valid VizId. A valid VizId must:
44
+
45
+ - Be exactly 32 characters long
46
+ - Contain only hexadecimal characters (0-9, a-f)
47
+ - Have "4" as the 13th character (following UUID v4 format)
44
48
 
45
49
  ```typescript
46
- import { isVizId } from '@vizhub/viz-utils';
50
+ import { isVizId } from "@vizhub/viz-utils";
47
51
 
48
52
  isVizId("12345678901234567890123456789012"); // true if valid
49
53
  isVizId("invalid-id"); // false
@@ -54,10 +58,16 @@ isVizId("invalid-id"); // false
54
58
  #### `getFileText(content: VizContent, fileName: string): string | null`
55
59
 
56
60
  Gets the text content of a file with the given name from a VizContent object.
57
- Returns null if the file is not found.
61
+ Returns null if:
62
+
63
+ - The file is not found
64
+ - The content is undefined
65
+ - The content has no files property
66
+
67
+ If multiple files with the same name exist, returns the content of the first matching file.
58
68
 
59
69
  ```typescript
60
- import { getFileText } from '@vizhub/viz-utils';
70
+ import { getFileText } from "@vizhub/viz-utils";
61
71
 
62
72
  const htmlContent = getFileText(vizContent, "index.html");
63
73
  if (htmlContent) {
@@ -65,6 +75,49 @@ if (htmlContent) {
65
75
  }
66
76
  ```
67
77
 
78
+ #### `vizFilesToFileCollection(files?: VizFiles): Record<string, string>`
79
+
80
+ Converts VizFiles (keyed by file ID) to a simpler file collection format (keyed by filename).
81
+ Returns an empty object if:
82
+
83
+ - No files are provided
84
+ - The files object is empty
85
+
86
+ ```typescript
87
+ import { vizFilesToFileCollection } from "@vizhub/viz-utils";
88
+
89
+ const vizFiles = {
90
+ "550e8400": { name: "index.html", text: "<html>Test</html>" },
91
+ e29b41d4: { name: "script.js", text: 'console.log("Hello");' },
92
+ };
93
+
94
+ const fileCollection = vizFilesToFileCollection(vizFiles);
95
+ // Result: { "index.html": "<html>Test</html>", "script.js": 'console.log("Hello");' }
96
+ ```
97
+
98
+ #### `fileCollectionToVizFiles(files: FileCollection): VizFiles`
99
+
100
+ Converts a simple file collection (keyed by filename) to VizFiles format (keyed by generated file ID).
101
+ Returns an empty object if:
102
+
103
+ - No files are provided
104
+ - The files object is empty
105
+
106
+ ```typescript
107
+ import { fileCollectionToVizFiles } from "@vizhub/viz-utils";
108
+
109
+ const fileCollection = {
110
+ "index.html": "<html>Test</html>",
111
+ "script.js": 'console.log("Hello");',
112
+ };
113
+
114
+ const vizFiles = fileCollectionToVizFiles(fileCollection);
115
+ // Result: {
116
+ // "550e8400": { name: "index.html", text: "<html>Test</html>" },
117
+ // "e29b41d4": { name: "script.js", text: 'console.log("Hello");' }
118
+ // }
119
+ ```
120
+
68
121
  ## Types
69
122
 
70
123
  This package uses the following types from `@vizhub/viz-types`:
@@ -0,0 +1,3 @@
1
+ import { FileCollection, VizFiles } from "@vizhub/viz-types";
2
+ export declare const fileCollectionToVizFiles: (files: FileCollection) => VizFiles;
3
+ //# sourceMappingURL=fileCollectionToVizFiles.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fileCollectionToVizFiles.d.ts","sourceRoot":"","sources":["../src/fileCollectionToVizFiles.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAG7D,eAAO,MAAM,wBAAwB,GAAI,OAAO,cAAc,KAAG,QAKhE,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=fileCollectionToVizFiles.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fileCollectionToVizFiles.test.d.ts","sourceRoot":"","sources":["../src/fileCollectionToVizFiles.test.ts"],"names":[],"mappings":""}
@@ -1,2 +1,3 @@
1
1
  import type { VizFileId } from "@vizhub/viz-types";
2
2
  export declare const generateVizFileId: () => VizFileId;
3
+ //# sourceMappingURL=generateVizFileId.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generateVizFileId.d.ts","sourceRoot":"","sources":["../src/generateVizFileId.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAInD,eAAO,MAAM,iBAAiB,QAAO,SACJ,CAAC"}
@@ -1 +1,2 @@
1
1
  export {};
2
+ //# sourceMappingURL=generateVizFileId.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generateVizFileId.test.d.ts","sourceRoot":"","sources":["../src/generateVizFileId.test.ts"],"names":[],"mappings":""}
@@ -1,2 +1,7 @@
1
1
  import type { VizId } from "@vizhub/viz-types";
2
+ /**
3
+ * Generates a dash‑free RFC 4122‑compliant UUID v4 (32 hex chars).
4
+ * Works in all evergreen browsers and Node 19 + without extra deps.
5
+ */
2
6
  export declare const generateVizId: () => VizId;
7
+ //# sourceMappingURL=generateVizId.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generateVizId.d.ts","sourceRoot":"","sources":["../src/generateVizId.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAgB/C;;;GAGG;AACH,eAAO,MAAM,aAAa,QAAO,KAWhC,CAAC"}
@@ -1 +1,2 @@
1
1
  export {};
2
+ //# sourceMappingURL=generateVizId.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generateVizId.test.d.ts","sourceRoot":"","sources":["../src/generateVizId.test.ts"],"names":[],"mappings":""}
@@ -1,2 +1,3 @@
1
1
  import { VizContent } from "@vizhub/viz-types";
2
2
  export declare const getFileText: (content: VizContent, fileName: string) => string | null;
3
+ //# sourceMappingURL=getFileText.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getFileText.d.ts","sourceRoot":"","sources":["../src/getFileText.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAI/C,eAAO,MAAM,WAAW,GACtB,SAAS,UAAU,EACnB,UAAU,MAAM,KACf,MAAM,GAAG,IAUX,CAAC"}
@@ -1 +1,2 @@
1
1
  export {};
2
+ //# sourceMappingURL=getFileText.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getFileText.test.d.ts","sourceRoot":"","sources":["../src/getFileText.test.ts"],"names":[],"mappings":""}
package/dist/index.cjs ADDED
@@ -0,0 +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;
2
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +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"}
package/dist/index.d.ts CHANGED
@@ -2,3 +2,6 @@ export { isVizId } from "./isVizId";
2
2
  export { generateVizId } from "./generateVizId";
3
3
  export { generateVizFileId } from "./generateVizFileId";
4
4
  export { getFileText } from "./getFileText";
5
+ export { vizFilesToFileCollection } from "./vizFilesToFileCollection";
6
+ export { fileCollectionToVizFiles } from "./fileCollectionToVizFiles";
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +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"}
package/dist/index.js CHANGED
@@ -1,12 +1,30 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getFileText = exports.generateVizFileId = exports.generateVizId = exports.isVizId = void 0;
4
- // Re-export all utility functions
5
- var isVizId_1 = require("./isVizId");
6
- Object.defineProperty(exports, "isVizId", { enumerable: true, get: function () { return isVizId_1.isVizId; } });
7
- var generateVizId_1 = require("./generateVizId");
8
- Object.defineProperty(exports, "generateVizId", { enumerable: true, get: function () { return generateVizId_1.generateVizId; } });
9
- var generateVizFileId_1 = require("./generateVizFileId");
10
- Object.defineProperty(exports, "generateVizFileId", { enumerable: true, get: function () { return generateVizFileId_1.generateVizFileId; } });
11
- var getFileText_1 = require("./getFileText");
12
- Object.defineProperty(exports, "getFileText", { enumerable: true, get: function () { return getFileText_1.getFileText; } });
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 = () => {
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(
4
+ ""
5
+ );
6
+ }, s = () => l().substring(0, 8), u = (e, t) => {
7
+ if (e && e.files)
8
+ for (const r of Object.keys(e.files)) {
9
+ const i = e.files[r];
10
+ if (i.name === t)
11
+ return i.text;
12
+ }
13
+ return null;
14
+ }, c = (e) => {
15
+ const t = {};
16
+ if (!e)
17
+ return t;
18
+ for (const r of Object.values(e))
19
+ t[r.name] = r.text;
20
+ return t;
21
+ }, a = (e) => Object.entries(e).reduce((t, [r, i]) => (t[s()] = { name: r, text: i }, t), {});
22
+ export {
23
+ a as fileCollectionToVizFiles,
24
+ s as generateVizFileId,
25
+ l as generateVizId,
26
+ u as getFileText,
27
+ f as isVizId,
28
+ c as vizFilesToFileCollection
29
+ };
30
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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;"}
package/dist/isVizId.d.ts CHANGED
@@ -1 +1,2 @@
1
1
  export declare const isVizId: (str: string) => boolean;
2
+ //# sourceMappingURL=isVizId.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"isVizId.d.ts","sourceRoot":"","sources":["../src/isVizId.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,OAAO,GAAI,KAAK,MAAM,KAAG,OAqBrC,CAAC"}
@@ -1 +1,2 @@
1
1
  export {};
2
+ //# sourceMappingURL=isVizId.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"isVizId.test.d.ts","sourceRoot":"","sources":["../src/isVizId.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,6 @@
1
+ import type { VizFiles, FileCollection } from "@vizhub/viz-types";
2
+ /**
3
+ * Converts VizContent to FileCollection format.
4
+ */
5
+ export declare const vizFilesToFileCollection: (files?: VizFiles) => FileCollection;
6
+ //# sourceMappingURL=vizFilesToFileCollection.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vizFilesToFileCollection.d.ts","sourceRoot":"","sources":["../src/vizFilesToFileCollection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAElE;;GAEG;AACH,eAAO,MAAM,wBAAwB,GAAI,QAAQ,QAAQ,KAAG,cAc3D,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=vizFilesToFileCollection.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vizFilesToFileCollection.test.d.ts","sourceRoot":"","sources":["../src/vizFilesToFileCollection.test.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,16 +1,26 @@
1
1
  {
2
2
  "name": "@vizhub/viz-utils",
3
- "version": "0.1.0",
3
+ "version": "1.1.0",
4
4
  "description": "Utility functions for use across VizHub packages.",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
5
+ "main": "./dist/index.cjs",
6
+ "module": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "type": "module",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs"
14
+ }
15
+ },
7
16
  "scripts": {
8
- "build": "tsc",
17
+ "build": "vite build && tsc --emitDeclarationOnly",
9
18
  "prettier": "prettier {*.*,**/*.*} --write",
10
19
  "prepublishOnly": "npm run build",
11
20
  "typecheck": "tsc --noEmit",
12
21
  "test": "vitest run",
13
- "test:watch": "vitest"
22
+ "test:watch": "vitest",
23
+ "upgrade": "ncu -u"
14
24
  },
15
25
  "repository": {
16
26
  "type": "git",
@@ -26,10 +36,13 @@
26
36
  },
27
37
  "homepage": "https://github.com/vizhub-core/viz-utils#readme",
28
38
  "dependencies": {
29
- "@vizhub/viz-types": "^0.0.2"
39
+ "@vizhub/viz-types": "^0.1.0"
30
40
  },
31
41
  "devDependencies": {
32
- "vitest": "^3.0.9"
42
+ "npm-check-updates": "^17.1.16",
43
+ "prettier": "^3.5.3",
44
+ "vite": "^6.2.5",
45
+ "vitest": "^3.1.1"
33
46
  },
34
47
  "publishConfig": {
35
48
  "access": "public"
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.generateVizFileId = void 0;
4
- var generateVizId_1 = require("./generateVizId");
5
- // Generates a file id
6
- var generateVizFileId = function () {
7
- return (0, generateVizId_1.generateVizId)().substring(0, 8);
8
- };
9
- exports.generateVizFileId = generateVizFileId;
@@ -1,21 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var vitest_1 = require("vitest");
4
- var generateVizFileId_1 = require("./generateVizFileId");
5
- (0, vitest_1.describe)('generateVizFileId', function () {
6
- (0, vitest_1.it)('should generate a string of length 8', function () {
7
- var fileId = (0, generateVizFileId_1.generateVizFileId)();
8
- (0, vitest_1.expect)(fileId.length).toBe(8);
9
- });
10
- (0, vitest_1.it)('should generate a string with only hexadecimal characters', function () {
11
- var fileId = (0, generateVizFileId_1.generateVizFileId)();
12
- (0, vitest_1.expect)(/^[0-9a-f]{8}$/i.test(fileId)).toBe(true);
13
- });
14
- (0, vitest_1.it)('should generate unique file IDs', function () {
15
- var fileIds = new Set();
16
- for (var i = 0; i < 100; i++) {
17
- fileIds.add((0, generateVizFileId_1.generateVizFileId)());
18
- }
19
- (0, vitest_1.expect)(fileIds.size).toBe(100);
20
- });
21
- });
@@ -1,14 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.generateVizId = void 0;
4
- // Generates a UUID v4 string without dashes,
5
- // without any dependencies on Node packages.
6
- // For use in the browser for generating entity ids.
7
- // See also interactors/generateId for the server-side version.
8
- // Code by ChatGPT, Feb 2024
9
- var generateVizId = function () {
10
- return "xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx".replace(/[xy]/g, function (c) {
11
- return ((c === "x" ? Math.random() * 16 : (Math.random() * 4) | 8) | 0).toString(16);
12
- });
13
- };
14
- exports.generateVizId = generateVizId;
@@ -1,30 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var vitest_1 = require("vitest");
4
- var generateVizId_1 = require("./generateVizId");
5
- var isVizId_1 = require("./isVizId");
6
- (0, vitest_1.describe)('generateVizId', function () {
7
- (0, vitest_1.it)('should generate a valid VizId', function () {
8
- var id = (0, generateVizId_1.generateVizId)();
9
- (0, vitest_1.expect)((0, isVizId_1.isVizId)(id)).toBe(true);
10
- });
11
- (0, vitest_1.it)('should generate a string of length 32', function () {
12
- var id = (0, generateVizId_1.generateVizId)();
13
- (0, vitest_1.expect)(id.length).toBe(32);
14
- });
15
- (0, vitest_1.it)('should generate a string with only hexadecimal characters', function () {
16
- var id = (0, generateVizId_1.generateVizId)();
17
- (0, vitest_1.expect)(/^[0-9a-f]{32}$/i.test(id)).toBe(true);
18
- });
19
- (0, vitest_1.it)('should generate a string with "4" as the 13th character', function () {
20
- var id = (0, generateVizId_1.generateVizId)();
21
- (0, vitest_1.expect)(id[12]).toBe('4');
22
- });
23
- (0, vitest_1.it)('should generate unique IDs', function () {
24
- var ids = new Set();
25
- for (var i = 0; i < 100; i++) {
26
- ids.add((0, generateVizId_1.generateVizId)());
27
- }
28
- (0, vitest_1.expect)(ids.size).toBe(100);
29
- });
30
- });
@@ -1,18 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getFileText = void 0;
4
- // Gets the text content of a file with the given name.
5
- // Returns null if not found.
6
- var getFileText = function (content, fileName) {
7
- if (content && content.files) {
8
- for (var _i = 0, _a = Object.keys(content.files); _i < _a.length; _i++) {
9
- var fileId = _a[_i];
10
- var file = content.files[fileId];
11
- if (file.name === fileName) {
12
- return file.text;
13
- }
14
- }
15
- }
16
- return null;
17
- };
18
- exports.getFileText = getFileText;
@@ -1,74 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var vitest_1 = require("vitest");
4
- var getFileText_1 = require("./getFileText");
5
- (0, vitest_1.describe)("getFileText", function () {
6
- (0, vitest_1.it)("should return the text content of a file with matching name", function () {
7
- var mockVizContent = {
8
- id: "test-viz-123",
9
- files: {
10
- file1: {
11
- name: "index.html",
12
- text: "<html>Test</html>",
13
- },
14
- file2: {
15
- name: "script.js",
16
- text: 'console.log("Hello");',
17
- },
18
- },
19
- };
20
- var result = (0, getFileText_1.getFileText)(mockVizContent, "index.html");
21
- (0, vitest_1.expect)(result).toBe("<html>Test</html>");
22
- });
23
- (0, vitest_1.it)("should return null when no file with the given name exists", function () {
24
- var mockVizContent = {
25
- id: "test-viz-123",
26
- files: {
27
- file1: {
28
- name: "index.html",
29
- text: "<html>Test</html>",
30
- },
31
- file2: {
32
- name: "script.js",
33
- text: 'console.log("Hello");',
34
- },
35
- },
36
- };
37
- var result = (0, getFileText_1.getFileText)(mockVizContent, "style.css");
38
- (0, vitest_1.expect)(result).toBeNull();
39
- });
40
- (0, vitest_1.it)("should return null when content has no files property", function () {
41
- var mockVizContent = {
42
- id: "test-viz-123",
43
- };
44
- var result = (0, getFileText_1.getFileText)(mockVizContent, "index.html");
45
- (0, vitest_1.expect)(result).toBeNull();
46
- });
47
- (0, vitest_1.it)("should return null when content is undefined", function () {
48
- var result = (0, getFileText_1.getFileText)(undefined, "index.html");
49
- (0, vitest_1.expect)(result).toBeNull();
50
- });
51
- (0, vitest_1.it)("should find the correct file when multiple files exist", function () {
52
- var mockVizContent = {
53
- id: "test-viz-123",
54
- files: {
55
- file1: { name: "data.csv", text: "a,b,c" },
56
- file2: {
57
- name: "index.html",
58
- text: "<html>First</html>",
59
- },
60
- file3: {
61
- name: "script.js",
62
- text: 'console.log("Hello");',
63
- },
64
- file4: {
65
- name: "index.html",
66
- text: "<html>Second</html>",
67
- },
68
- },
69
- };
70
- // Should return the first matching file's content
71
- var result = (0, getFileText_1.getFileText)(mockVizContent, "index.html");
72
- (0, vitest_1.expect)(result).toBe("<html>First</html>");
73
- });
74
- });
package/dist/isVizId.js DELETED
@@ -1,23 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isVizId = void 0;
4
- // Checks if a string was generated by `generateId` from interactors.
5
- var isVizId = function (str) {
6
- // First check if the length is exactly 32 characters
7
- if (str.length !== 32) {
8
- return false;
9
- }
10
- // Regular expression for a 32-character hexadecimal string
11
- var uuidV4NoDashRegex = /^[0-9a-f]{32}$/i;
12
- // Check if the string matches the regular expression
13
- if (!uuidV4NoDashRegex.test(str)) {
14
- return false;
15
- }
16
- // Check if the 13th character is '4' (indicating UUID v4)
17
- // and the 17th character is one of '8', '9', 'a', 'b' (indicating the variant)
18
- return (str[12] === "4"
19
- // &&
20
- // ['8', '9', 'a', 'b'].includes(str[16].toLowerCase())
21
- );
22
- };
23
- exports.isVizId = isVizId;
@@ -1,25 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var vitest_1 = require("vitest");
4
- var isVizId_1 = require("./isVizId");
5
- (0, vitest_1.describe)('isVizId', function () {
6
- (0, vitest_1.it)('should return true for valid VizId', function () {
7
- // Valid VizId with '4' as the 13th character
8
- var validId = '12345678901234567890123456789012';
9
- var validIdWith4 = validId.substring(0, 12) + '4' + validId.substring(13);
10
- (0, vitest_1.expect)((0, isVizId_1.isVizId)(validIdWith4)).toBe(true);
11
- });
12
- (0, vitest_1.it)('should return false for strings that are not 32 characters', function () {
13
- (0, vitest_1.expect)((0, isVizId_1.isVizId)('123')).toBe(false);
14
- (0, vitest_1.expect)((0, isVizId_1.isVizId)('12345678901234567890123456789012345')).toBe(false);
15
- });
16
- (0, vitest_1.it)('should return false for strings with non-hex characters', function () {
17
- (0, vitest_1.expect)((0, isVizId_1.isVizId)('1234567890123g5678901234567890123')).toBe(false);
18
- (0, vitest_1.expect)((0, isVizId_1.isVizId)('12345678901234567890123456789012!')).toBe(false);
19
- });
20
- (0, vitest_1.it)('should return false if the 13th character is not "4"', function () {
21
- var validId = '12345678901234567890123456789012';
22
- var invalidIdWith5 = validId.substring(0, 12) + '5' + validId.substring(13);
23
- (0, vitest_1.expect)((0, isVizId_1.isVizId)(invalidIdWith5)).toBe(false);
24
- });
25
- });