json-renderers 1.13.7 → 1.16.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.
@@ -0,0 +1,14 @@
1
+ import meta from "./meta.js";
2
+
3
+ export const registerGlobal = (inFuncDefinition) => {
4
+ if (typeof globalThis === "undefined" || !inFuncDefinition) return;
5
+
6
+ globalThis.ks ??= {};
7
+
8
+ globalThis.ks.jsonRenderers ??= {
9
+ meta,
10
+ renderToDom: inFuncDefinition
11
+ };
12
+ };
13
+
14
+ export default registerGlobal;
@@ -0,0 +1,40 @@
1
+ import jsonRenderBuild from "json-renderers-build";
2
+ import jsonToTag from "@keshavsoft/json-to-tag";
3
+
4
+ // --- Story of Table Render ---
5
+ const startFunc = ({
6
+ inTargetHtmlId,
7
+ inColumns,
8
+ inType = "table",
9
+ inData,
10
+ inShowLog = false
11
+ } = {}) => {
12
+ const localTargetHtmlId = inTargetHtmlId;
13
+ const localData = inData ?? [];
14
+ const localColumns = inColumns;
15
+ const localType = inType;
16
+ const localShowLog = inShowLog;
17
+
18
+ if (localShowLog) console.log("buildSpec 1 :", localTargetHtmlId, localData, localColumns);
19
+
20
+ let specAsJsonToDom = jsonRenderBuild({
21
+ type: localType,
22
+ targetHtmlId: localTargetHtmlId,
23
+ data: localData,
24
+ columns: localColumns
25
+ });
26
+
27
+ let jsonToSend = specAsJsonToDom;
28
+
29
+ const content = jsonToTag(jsonToSend);
30
+
31
+ if (localShowLog) {
32
+ console.log("buildSpec 2 :", jsonToSend, content);
33
+ console.log("buildSpec 3 :", content);
34
+ }
35
+
36
+ return content;
37
+ };
38
+
39
+ export { startFunc };
40
+ export default startFunc;
@@ -0,0 +1,20 @@
1
+ const deriveColumnsFromData = ({ inData = [] } = {}) => {
2
+ const localData = inData;
3
+
4
+ if (!Array.isArray(localData) || localData.length === 0) {
5
+ return [];
6
+ }
7
+
8
+ const firstRow = localData[0];
9
+ if (!firstRow || typeof firstRow !== "object") {
10
+ return [];
11
+ }
12
+
13
+ return Object.keys(firstRow).map(key => ({
14
+ key,
15
+ label: key
16
+ }));
17
+ };
18
+
19
+ export { deriveColumnsFromData };
20
+ export default deriveColumnsFromData;
@@ -0,0 +1,38 @@
1
+ const guardFunc = ({ inConfig } = {}) => {
2
+ const localConfig = inConfig;
3
+
4
+ if (!localConfig || typeof localConfig !== "object") {
5
+ console.error("Config must be an object");
6
+ throw new TypeError("Config must be an object");
7
+ }
8
+
9
+ if (localConfig.type !== "table") {
10
+ console.error("type must be 'table'");
11
+ throw new Error("type must be 'table'");
12
+ }
13
+
14
+ if (!Array.isArray(localConfig.data)) {
15
+ console.error("data must be an Array");
16
+ throw new TypeError("data must be an Array");
17
+ }
18
+
19
+ if (typeof localConfig.targetHtmlId !== "string" || !localConfig.targetHtmlId.trim()) {
20
+ console.error("targetHtmlId must be a non-empty string");
21
+ throw new TypeError("targetHtmlId must be a non-empty string");
22
+ }
23
+
24
+ if (!Array.isArray(localConfig.columns)) {
25
+ console.error("columns must be an array");
26
+ throw new TypeError("columns must be an array");
27
+ }
28
+
29
+ if (localConfig.showLog !== undefined && typeof localConfig.showLog !== "boolean") {
30
+ console.error("showLog must be a boolean");
31
+ throw new TypeError("showLog must be a boolean");
32
+ }
33
+
34
+ return true;
35
+ };
36
+
37
+ export { guardFunc };
38
+ export default guardFunc;
@@ -0,0 +1,110 @@
1
+ import registerGlobal from "./registerGlobal.js";
2
+ import guardFunc from "./guard.js";
3
+ import buildSpec from "./buildSpec/index.js";
4
+
5
+ // Pure function: builds and validates the JSON configuration without touching DOM
6
+ const getSpec = ({
7
+ type = "table",
8
+ targetHtmlId,
9
+ data,
10
+ columns,
11
+ classToApply,
12
+ appendPosition,
13
+ showLog = false
14
+ } = {}) => {
15
+ const localType = type;
16
+ const localTargetHtmlId = targetHtmlId;
17
+ const localData = data;
18
+ const localColumns = columns;
19
+ const localClassToApply = classToApply;
20
+ const localAppendPosition = appendPosition;
21
+ const localShowLog = showLog;
22
+
23
+ const specJson = {
24
+ type: localType,
25
+ targetHtmlId: localTargetHtmlId,
26
+ data: localData,
27
+ columns: localColumns,
28
+ classToApply: localClassToApply,
29
+ appendPosition: localAppendPosition,
30
+ showLog: localShowLog
31
+ };
32
+
33
+ // Internal call using the in-convention
34
+ guardFunc({ inConfig: specJson });
35
+
36
+ return specJson;
37
+ };
38
+
39
+ // Render function: Public API accepting clean single options object
40
+ const render = ({
41
+ type = "table",
42
+ targetHtmlId,
43
+ data,
44
+ columns,
45
+ classToApply,
46
+ appendPosition,
47
+ showLog = false
48
+ } = {}) => {
49
+ // 1. Get and validate the clean JSON specification
50
+ const specJson = getSpec({
51
+ type,
52
+ targetHtmlId,
53
+ data,
54
+ columns,
55
+ classToApply,
56
+ appendPosition,
57
+ showLog
58
+ });
59
+
60
+ if (specJson.showLog) console.log("showLog 1 (spec):", specJson);
61
+
62
+ // 2. Build DOM content via buildSpec
63
+ const content = buildSpec({
64
+ inTargetHtmlId: specJson.targetHtmlId,
65
+ inColumns: specJson.columns,
66
+ inData: specJson.data,
67
+ inType: specJson.type,
68
+ inShowLog: specJson.showLog
69
+ });
70
+
71
+ if (specJson.showLog) console.log("showLog 2 (content):", content);
72
+
73
+ const container = document.getElementById(specJson.targetHtmlId);
74
+ if (specJson.showLog) console.log("showLog 3 (container):", container);
75
+
76
+ if (!container) return null;
77
+
78
+ const isCollection = Array.isArray(content) ||
79
+ content instanceof NodeList ||
80
+ content instanceof HTMLCollection;
81
+
82
+ if (isCollection) {
83
+ if (specJson.appendPosition === "prepend") {
84
+ container.prepend(...content);
85
+ } else {
86
+ container.append(...content);
87
+ }
88
+ } else {
89
+ if (specJson.appendPosition === "prepend") {
90
+ container.prepend(content);
91
+ } else {
92
+ container.innerHTML = "";
93
+ container.append(content);
94
+ }
95
+ }
96
+
97
+ if (specJson.showLog) console.log("showLog 4 (mounted):", container);
98
+
99
+ return container;
100
+ };
101
+
102
+ // Register on global window.ks.jsonRenderers
103
+ registerGlobal({ render, getSpec });
104
+
105
+ // Export two ways:
106
+ // 1. As an object (named exports)
107
+ export { render, getSpec, guardFunc, buildSpec };
108
+
109
+ // 2. As a single function (default export)
110
+ export default render;
@@ -0,0 +1,6 @@
1
+ export const meta = {
2
+ version: "v16.0.0",
3
+ description: "build table from store data and render to DOM uses json-to-spec, json-to-dom under the hood"
4
+ };
5
+
6
+ export default meta;
@@ -0,0 +1,27 @@
1
+ import meta from "./meta.js";
2
+
3
+ export const registerGlobal = (inDefinition) => {
4
+ const localDefinition = inDefinition;
5
+
6
+ if (typeof globalThis === "undefined" || !localDefinition) return;
7
+
8
+ globalThis.ks ??= {};
9
+
10
+ const renderFunc = typeof localDefinition === "function"
11
+ ? localDefinition
12
+ : localDefinition.render;
13
+
14
+ const getSpecFunc = typeof localDefinition === "object"
15
+ ? localDefinition.getSpec
16
+ : undefined;
17
+
18
+ globalThis.ks.jsonRenderers = {
19
+ meta,
20
+ renderToDom: renderFunc,
21
+ render: renderFunc,
22
+ ...(getSpecFunc ? { getSpec: getSpecFunc } : {}),
23
+ ...(typeof localDefinition === "object" ? localDefinition : {})
24
+ };
25
+ };
26
+
27
+ export default registerGlobal;