@vizhub/viz-utils 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 VizHub
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # viz-utils
2
+ Utility functions for use across VizHub packages
@@ -0,0 +1,2 @@
1
+ import type { VizFileId } from "@vizhub/viz-types";
2
+ export declare const generateVizFileId: () => VizFileId;
@@ -0,0 +1,9 @@
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;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,21 @@
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
+ });
@@ -0,0 +1,2 @@
1
+ import type { VizId } from "@vizhub/viz-types";
2
+ export declare const generateVizId: () => VizId;
@@ -0,0 +1,14 @@
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;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,30 @@
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
+ });
@@ -0,0 +1,3 @@
1
+ export { isVizId } from './isVizId';
2
+ export { generateVizId } from './generateVizId';
3
+ export { generateVizFileId } from './generateVizFileId';
package/dist/index.js ADDED
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ 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; } });
@@ -0,0 +1 @@
1
+ export declare const isVizId: (str: string) => boolean;
@@ -0,0 +1,23 @@
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;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,25 @@
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
+ });
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@vizhub/viz-utils",
3
+ "version": "0.0.1",
4
+ "description": "Utility functions for use across VizHub packages.",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc",
9
+ "prettier": "prettier {*.*,**/*.*} --write",
10
+ "prepublishOnly": "npm run build",
11
+ "typecheck": "tsc --noEmit",
12
+ "test": "vitest run",
13
+ "test:watch": "vitest"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+ssh://git@github.com/vizhub-core/viz-utils.git"
18
+ },
19
+ "keywords": [
20
+ "Viz"
21
+ ],
22
+ "author": "Curran Kelleher",
23
+ "license": "MIT",
24
+ "bugs": {
25
+ "url": "https://github.com/vizhub-core/viz-utils/issues"
26
+ },
27
+ "homepage": "https://github.com/vizhub-core/viz-utils#readme",
28
+ "dependencies": {
29
+ "@vizhub/viz-types": "^0.0.2"
30
+ },
31
+ "devDependencies": {
32
+ "vitest": "^3.0.9"
33
+ },
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
37
+ "files": [
38
+ "dist"
39
+ ]
40
+ }