@vizhub/viz-utils 0.0.1 → 0.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.
- package/README.md +79 -1
- package/dist/getFileText.d.ts +2 -0
- package/dist/getFileText.js +18 -0
- package/dist/getFileText.test.d.ts +1 -0
- package/dist/getFileText.test.js +74 -0
- package/dist/index.d.ts +4 -3
- package/dist/index.js +3 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,2 +1,80 @@
|
|
|
1
1
|
# viz-utils
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
A collection of utility functions for use across VizHub packages. This library provides common functionality for working with VizHub visualizations, including ID generation and file manipulation.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @vizhub/viz-utils
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Dependencies
|
|
12
|
+
|
|
13
|
+
This package depends on `@vizhub/viz-types`, which defines the TypeScript types used throughout the VizHub ecosystem.
|
|
14
|
+
|
|
15
|
+
## API
|
|
16
|
+
|
|
17
|
+
### ID Generation
|
|
18
|
+
|
|
19
|
+
#### `generateVizId()`
|
|
20
|
+
|
|
21
|
+
Generates a unique VizId (a UUID v4 string without dashes) for a visualization.
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
import { generateVizId } from '@vizhub/viz-utils';
|
|
25
|
+
|
|
26
|
+
const newVizId = generateVizId(); // e.g. "12345678901234567890123456789012"
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
#### `generateVizFileId()`
|
|
30
|
+
|
|
31
|
+
Generates a unique VizFileId (an 8-character substring of a VizId) for a file within a visualization.
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
import { generateVizFileId } from '@vizhub/viz-utils';
|
|
35
|
+
|
|
36
|
+
const newFileId = generateVizFileId(); // e.g. "12345678"
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Validation
|
|
40
|
+
|
|
41
|
+
#### `isVizId(str: string): boolean`
|
|
42
|
+
|
|
43
|
+
Checks if a string is a valid VizId.
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import { isVizId } from '@vizhub/viz-utils';
|
|
47
|
+
|
|
48
|
+
isVizId("12345678901234567890123456789012"); // true if valid
|
|
49
|
+
isVizId("invalid-id"); // false
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### File Operations
|
|
53
|
+
|
|
54
|
+
#### `getFileText(content: VizContent, fileName: string): string | null`
|
|
55
|
+
|
|
56
|
+
Gets the text content of a file with the given name from a VizContent object.
|
|
57
|
+
Returns null if the file is not found.
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
import { getFileText } from '@vizhub/viz-utils';
|
|
61
|
+
|
|
62
|
+
const htmlContent = getFileText(vizContent, "index.html");
|
|
63
|
+
if (htmlContent) {
|
|
64
|
+
// Use the file content
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Types
|
|
69
|
+
|
|
70
|
+
This package uses the following types from `@vizhub/viz-types`:
|
|
71
|
+
|
|
72
|
+
- `VizId`: A 32-character hexadecimal string that uniquely identifies a visualization
|
|
73
|
+
- `VizFileId`: An 8-character hexadecimal string that uniquely identifies a file within a visualization
|
|
74
|
+
- `VizContent`: The content of a visualization, including its files
|
|
75
|
+
- `VizFile`: A file with a name and text content
|
|
76
|
+
- `VizFiles`: A collection of files, indexed by their VizFileId
|
|
77
|
+
|
|
78
|
+
## License
|
|
79
|
+
|
|
80
|
+
MIT
|
|
@@ -0,0 +1,18 @@
|
|
|
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;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,74 @@
|
|
|
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/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
export { isVizId } from
|
|
2
|
-
export { generateVizId } from
|
|
3
|
-
export { generateVizFileId } from
|
|
1
|
+
export { isVizId } from "./isVizId";
|
|
2
|
+
export { generateVizId } from "./generateVizId";
|
|
3
|
+
export { generateVizFileId } from "./generateVizFileId";
|
|
4
|
+
export { getFileText } from "./getFileText";
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.generateVizFileId = exports.generateVizId = exports.isVizId = void 0;
|
|
3
|
+
exports.getFileText = exports.generateVizFileId = exports.generateVizId = exports.isVizId = void 0;
|
|
4
4
|
// Re-export all utility functions
|
|
5
5
|
var isVizId_1 = require("./isVizId");
|
|
6
6
|
Object.defineProperty(exports, "isVizId", { enumerable: true, get: function () { return isVizId_1.isVizId; } });
|
|
@@ -8,3 +8,5 @@ var generateVizId_1 = require("./generateVizId");
|
|
|
8
8
|
Object.defineProperty(exports, "generateVizId", { enumerable: true, get: function () { return generateVizId_1.generateVizId; } });
|
|
9
9
|
var generateVizFileId_1 = require("./generateVizFileId");
|
|
10
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; } });
|