json-to-spec 21.1.1 → 23.1.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/docs/dist/min.js +115 -77
- package/docs/dist/v23/min.js +154 -0
- package/package.json +6 -6
- package/src/index.js +2 -2
- package/src/v22/buildSpec/buildChildrenNodes.js +24 -0
- package/src/v22/buildSpec/buildSingleElement.js +18 -0
- package/src/v22/buildSpec/buildSpecArray.js +16 -0
- package/src/v22/buildSpec/forArray/v1/index.js +27 -0
- package/src/v22/buildSpec/forObject/v1/index.js +47 -0
- package/src/v22/buildSpec/forSpec/v3/index.js +84 -0
- package/src/v22/buildSpec/forSpec.js +76 -0
- package/src/v22/buildSpec/guards.js +26 -0
- package/src/v22/buildSpec/index.js +97 -0
- package/src/v22/index.js +21 -0
- package/src/v22/meta.js +6 -0
- package/src/v22/registerGlobal.js +17 -0
- package/src/v23/buildSpec/buildSpecArray.js +16 -0
- package/src/v23/buildSpec/forArray/v1/index.js +27 -0
- package/src/v23/buildSpec/forArrayStrings/v1/index.js +24 -0
- package/src/v23/buildSpec/forObject/v1/index.js +47 -0
- package/src/v23/buildSpec/forSpec/v3/index.js +114 -0
- package/src/v23/buildSpec/guards.js +20 -0
- package/src/v23/buildSpec/index.js +122 -0
- package/src/v23/index.js +21 -0
- package/src/v23/meta.js +6 -0
- package/src/v23/registerGlobal.js +17 -0
package/docs/dist/min.js
CHANGED
|
@@ -1,116 +1,154 @@
|
|
|
1
|
-
const
|
|
2
|
-
version: "
|
|
1
|
+
const f = {
|
|
2
|
+
version: "v23.0",
|
|
3
3
|
description: "Pure spec engine no document at all"
|
|
4
|
-
},
|
|
4
|
+
}, d = (e) => {
|
|
5
5
|
typeof globalThis > "u" || !e || (globalThis.ks ?? (globalThis.ks = {}), globalThis.ks["json-to-spec"] = {
|
|
6
|
-
meta:
|
|
6
|
+
meta: f,
|
|
7
7
|
buildSpecElement: e
|
|
8
8
|
});
|
|
9
|
-
},
|
|
10
|
-
const
|
|
11
|
-
return
|
|
12
|
-
},
|
|
13
|
-
const
|
|
14
|
-
return Array.isArray(
|
|
15
|
-
inSpecJson:
|
|
16
|
-
inShowLog:
|
|
17
|
-
inDataJson:
|
|
9
|
+
}, y = ({ inSpec: e }) => {
|
|
10
|
+
const n = e;
|
|
11
|
+
return n == null;
|
|
12
|
+
}, m = ({ inSpec: e }) => typeof Node < "u" && e instanceof Node, b = ({ inSpecJson: e }) => Array.isArray(e), A = ({ inArray: e = [], inShowLog: n = !1, inDataJson: o }) => {
|
|
13
|
+
const t = e, c = n, r = o;
|
|
14
|
+
return Array.isArray(t) ? t.map((s) => i({
|
|
15
|
+
inSpecJson: s,
|
|
16
|
+
inShowLog: c,
|
|
17
|
+
inDataJson: r
|
|
18
18
|
})).flat().filter(Boolean) : [];
|
|
19
|
-
},
|
|
20
|
-
const
|
|
21
|
-
let
|
|
22
|
-
for (const
|
|
23
|
-
if (
|
|
19
|
+
}, a = (e, n) => typeof e != "string" ? e : e.replace(/\$\{([^}]+)\}/g, (o, t) => {
|
|
20
|
+
const c = t.trim().split(".");
|
|
21
|
+
let r = n;
|
|
22
|
+
for (const s of c) {
|
|
23
|
+
if (r == null)
|
|
24
24
|
return "";
|
|
25
|
-
|
|
25
|
+
r = r[s];
|
|
26
26
|
}
|
|
27
|
-
return
|
|
28
|
-
}),
|
|
29
|
-
e
|
|
30
|
-
t
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
)
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
27
|
+
return r === null || typeof r == "string" || typeof r == "number" || typeof r == "boolean" ? String(r ?? "") : r;
|
|
28
|
+
}), u = ({ inSpecJson: e, inData: n, inShowLog: o }) => {
|
|
29
|
+
const t = structuredClone(e);
|
|
30
|
+
return typeof n == "string" ? ("textContent" in t && (t.textContent === "${}" ? t.textContent = n : typeof t.textContent == "string" && (t.textContent = t.textContent.replaceAll("${}", () => n))), "attributes" in t && (t.attributes = Object.fromEntries(
|
|
31
|
+
Object.entries(t.attributes).map(
|
|
32
|
+
([c, r]) => [
|
|
33
|
+
c,
|
|
34
|
+
r === "${}" ? n : typeof r == "string" ? r.replaceAll("${}", () => n) : r
|
|
35
|
+
]
|
|
36
|
+
)
|
|
37
|
+
))) : "key" in n && "value" in n ? "textContent" in t && (t.textContent = a(
|
|
38
|
+
t.textContent,
|
|
39
|
+
n
|
|
40
|
+
)) : ("textContent" in t && (t.textContent = a(
|
|
41
|
+
t.textContent,
|
|
42
|
+
n
|
|
43
|
+
)), "attributes" in t && (t.attributes = Object.fromEntries(
|
|
44
|
+
Object.entries(t.attributes).map(
|
|
45
|
+
([c, r]) => [
|
|
46
|
+
c,
|
|
47
|
+
a(r, n)
|
|
48
|
+
]
|
|
49
|
+
)
|
|
50
|
+
)), "children" in t && (t.children = t.children.map((c) => i({
|
|
51
|
+
inSpecJson: c,
|
|
52
|
+
inShowLog: o,
|
|
53
|
+
inDataJson: n
|
|
54
|
+
})))), t;
|
|
55
|
+
}, j = ({ inTemplate: e, inDataAsArray: n }) => {
|
|
56
|
+
const o = n, t = e;
|
|
57
|
+
return Array.isArray(o) ? o.map((r) => {
|
|
58
|
+
const s = structuredClone(t);
|
|
59
|
+
return i({
|
|
60
|
+
inSpecJson: s,
|
|
61
|
+
inDataJson: r
|
|
52
62
|
});
|
|
53
|
-
});
|
|
54
|
-
},
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
63
|
+
}) : [];
|
|
64
|
+
}, S = ({ inTemplate: e, inDataAsObject: n }) => {
|
|
65
|
+
const o = n, t = e;
|
|
66
|
+
if (o === null || typeof o != "object")
|
|
67
|
+
return [];
|
|
68
|
+
const c = [];
|
|
69
|
+
for (const [r, s] of Object.entries(o)) {
|
|
70
|
+
const l = structuredClone(t), p = i({
|
|
71
|
+
inSpecJson: l,
|
|
72
|
+
inDataJson: {
|
|
73
|
+
key: r,
|
|
74
|
+
value: s
|
|
75
|
+
}
|
|
61
76
|
});
|
|
62
|
-
|
|
77
|
+
c.push(p);
|
|
63
78
|
}
|
|
64
|
-
return
|
|
65
|
-
},
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
79
|
+
return c;
|
|
80
|
+
}, i = ({
|
|
81
|
+
inSpecJson: e,
|
|
82
|
+
inShowLog: n = !1,
|
|
83
|
+
inDataJson: o
|
|
84
|
+
} = {}) => {
|
|
85
|
+
if (y({ inSpec: e }))
|
|
86
|
+
return null;
|
|
87
|
+
if (m({ inSpec: e }))
|
|
88
|
+
return e;
|
|
89
|
+
if (b({ inSpecJson: e }))
|
|
90
|
+
return A({
|
|
70
91
|
inArray: e,
|
|
71
|
-
inShowLog:
|
|
92
|
+
inShowLog: n,
|
|
72
93
|
inDataJson: o
|
|
73
94
|
});
|
|
74
95
|
if ("jsonToSpec" in e) {
|
|
75
|
-
if ("isSpecBuilt" in e)
|
|
76
|
-
return e;
|
|
77
96
|
if (e.jsonToSpec.operation === "loopArray") {
|
|
78
|
-
const
|
|
97
|
+
const c = j({
|
|
79
98
|
inTemplate: e.jsonToSpec.template,
|
|
80
99
|
inDataAsArray: o[e.jsonToSpec.source]
|
|
100
|
+
}), {
|
|
101
|
+
jsonToSpec: r,
|
|
102
|
+
...s
|
|
103
|
+
} = e, l = {
|
|
104
|
+
...s,
|
|
105
|
+
children: c
|
|
106
|
+
};
|
|
107
|
+
return u({
|
|
108
|
+
inSpecJson: l,
|
|
109
|
+
inShowLog: n,
|
|
110
|
+
inData: o
|
|
81
111
|
});
|
|
82
|
-
e.children = r;
|
|
83
112
|
}
|
|
84
113
|
if (e.jsonToSpec.operation === "loopObject") {
|
|
85
|
-
const
|
|
114
|
+
const c = S({
|
|
86
115
|
inTemplate: e.jsonToSpec.template,
|
|
87
116
|
inDataAsObject: o
|
|
117
|
+
}), {
|
|
118
|
+
jsonToSpec: r,
|
|
119
|
+
...s
|
|
120
|
+
} = e, l = {
|
|
121
|
+
...s,
|
|
122
|
+
children: c
|
|
123
|
+
};
|
|
124
|
+
return u({
|
|
125
|
+
inSpecJson: l,
|
|
126
|
+
inShowLog: n,
|
|
127
|
+
inData: o
|
|
88
128
|
});
|
|
89
|
-
e.children = r;
|
|
90
129
|
}
|
|
91
|
-
e.isSpecBuilt = !0;
|
|
92
130
|
}
|
|
93
|
-
return
|
|
131
|
+
return u({
|
|
94
132
|
inSpecJson: e,
|
|
95
|
-
inShowLog:
|
|
133
|
+
inShowLog: n,
|
|
96
134
|
inData: o
|
|
97
135
|
});
|
|
98
|
-
},
|
|
136
|
+
}, T = ({
|
|
99
137
|
specJson: e,
|
|
100
|
-
showLog:
|
|
138
|
+
showLog: n,
|
|
101
139
|
dataJson: o
|
|
102
140
|
}) => {
|
|
103
141
|
try {
|
|
104
|
-
return
|
|
142
|
+
return i({
|
|
105
143
|
inSpecJson: e,
|
|
106
|
-
inShowLog:
|
|
144
|
+
inShowLog: n,
|
|
107
145
|
inDataJson: o
|
|
108
146
|
});
|
|
109
|
-
} catch (
|
|
110
|
-
console.log("error : ",
|
|
147
|
+
} catch (t) {
|
|
148
|
+
console.log("error : ", t);
|
|
111
149
|
}
|
|
112
150
|
};
|
|
113
|
-
|
|
151
|
+
d(T);
|
|
114
152
|
export {
|
|
115
|
-
|
|
153
|
+
T as default
|
|
116
154
|
};
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
const f = {
|
|
2
|
+
version: "v23.0",
|
|
3
|
+
description: "Pure spec engine no document at all"
|
|
4
|
+
}, d = (e) => {
|
|
5
|
+
typeof globalThis > "u" || !e || (globalThis.ks ?? (globalThis.ks = {}), globalThis.ks["json-to-spec"] = {
|
|
6
|
+
meta: f,
|
|
7
|
+
buildSpecElement: e
|
|
8
|
+
});
|
|
9
|
+
}, y = ({ inSpec: e }) => {
|
|
10
|
+
const n = e;
|
|
11
|
+
return n == null;
|
|
12
|
+
}, m = ({ inSpec: e }) => typeof Node < "u" && e instanceof Node, b = ({ inSpecJson: e }) => Array.isArray(e), A = ({ inArray: e = [], inShowLog: n = !1, inDataJson: o }) => {
|
|
13
|
+
const t = e, c = n, r = o;
|
|
14
|
+
return Array.isArray(t) ? t.map((s) => i({
|
|
15
|
+
inSpecJson: s,
|
|
16
|
+
inShowLog: c,
|
|
17
|
+
inDataJson: r
|
|
18
|
+
})).flat().filter(Boolean) : [];
|
|
19
|
+
}, a = (e, n) => typeof e != "string" ? e : e.replace(/\$\{([^}]+)\}/g, (o, t) => {
|
|
20
|
+
const c = t.trim().split(".");
|
|
21
|
+
let r = n;
|
|
22
|
+
for (const s of c) {
|
|
23
|
+
if (r == null)
|
|
24
|
+
return "";
|
|
25
|
+
r = r[s];
|
|
26
|
+
}
|
|
27
|
+
return r === null || typeof r == "string" || typeof r == "number" || typeof r == "boolean" ? String(r ?? "") : r;
|
|
28
|
+
}), u = ({ inSpecJson: e, inData: n, inShowLog: o }) => {
|
|
29
|
+
const t = structuredClone(e);
|
|
30
|
+
return typeof n == "string" ? ("textContent" in t && (t.textContent === "${}" ? t.textContent = n : typeof t.textContent == "string" && (t.textContent = t.textContent.replaceAll("${}", () => n))), "attributes" in t && (t.attributes = Object.fromEntries(
|
|
31
|
+
Object.entries(t.attributes).map(
|
|
32
|
+
([c, r]) => [
|
|
33
|
+
c,
|
|
34
|
+
r === "${}" ? n : typeof r == "string" ? r.replaceAll("${}", () => n) : r
|
|
35
|
+
]
|
|
36
|
+
)
|
|
37
|
+
))) : "key" in n && "value" in n ? "textContent" in t && (t.textContent = a(
|
|
38
|
+
t.textContent,
|
|
39
|
+
n
|
|
40
|
+
)) : ("textContent" in t && (t.textContent = a(
|
|
41
|
+
t.textContent,
|
|
42
|
+
n
|
|
43
|
+
)), "attributes" in t && (t.attributes = Object.fromEntries(
|
|
44
|
+
Object.entries(t.attributes).map(
|
|
45
|
+
([c, r]) => [
|
|
46
|
+
c,
|
|
47
|
+
a(r, n)
|
|
48
|
+
]
|
|
49
|
+
)
|
|
50
|
+
)), "children" in t && (t.children = t.children.map((c) => i({
|
|
51
|
+
inSpecJson: c,
|
|
52
|
+
inShowLog: o,
|
|
53
|
+
inDataJson: n
|
|
54
|
+
})))), t;
|
|
55
|
+
}, j = ({ inTemplate: e, inDataAsArray: n }) => {
|
|
56
|
+
const o = n, t = e;
|
|
57
|
+
return Array.isArray(o) ? o.map((r) => {
|
|
58
|
+
const s = structuredClone(t);
|
|
59
|
+
return i({
|
|
60
|
+
inSpecJson: s,
|
|
61
|
+
inDataJson: r
|
|
62
|
+
});
|
|
63
|
+
}) : [];
|
|
64
|
+
}, S = ({ inTemplate: e, inDataAsObject: n }) => {
|
|
65
|
+
const o = n, t = e;
|
|
66
|
+
if (o === null || typeof o != "object")
|
|
67
|
+
return [];
|
|
68
|
+
const c = [];
|
|
69
|
+
for (const [r, s] of Object.entries(o)) {
|
|
70
|
+
const l = structuredClone(t), p = i({
|
|
71
|
+
inSpecJson: l,
|
|
72
|
+
inDataJson: {
|
|
73
|
+
key: r,
|
|
74
|
+
value: s
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
c.push(p);
|
|
78
|
+
}
|
|
79
|
+
return c;
|
|
80
|
+
}, i = ({
|
|
81
|
+
inSpecJson: e,
|
|
82
|
+
inShowLog: n = !1,
|
|
83
|
+
inDataJson: o
|
|
84
|
+
} = {}) => {
|
|
85
|
+
if (y({ inSpec: e }))
|
|
86
|
+
return null;
|
|
87
|
+
if (m({ inSpec: e }))
|
|
88
|
+
return e;
|
|
89
|
+
if (b({ inSpecJson: e }))
|
|
90
|
+
return A({
|
|
91
|
+
inArray: e,
|
|
92
|
+
inShowLog: n,
|
|
93
|
+
inDataJson: o
|
|
94
|
+
});
|
|
95
|
+
if ("jsonToSpec" in e) {
|
|
96
|
+
if (e.jsonToSpec.operation === "loopArray") {
|
|
97
|
+
const c = j({
|
|
98
|
+
inTemplate: e.jsonToSpec.template,
|
|
99
|
+
inDataAsArray: o[e.jsonToSpec.source]
|
|
100
|
+
}), {
|
|
101
|
+
jsonToSpec: r,
|
|
102
|
+
...s
|
|
103
|
+
} = e, l = {
|
|
104
|
+
...s,
|
|
105
|
+
children: c
|
|
106
|
+
};
|
|
107
|
+
return u({
|
|
108
|
+
inSpecJson: l,
|
|
109
|
+
inShowLog: n,
|
|
110
|
+
inData: o
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
if (e.jsonToSpec.operation === "loopObject") {
|
|
114
|
+
const c = S({
|
|
115
|
+
inTemplate: e.jsonToSpec.template,
|
|
116
|
+
inDataAsObject: o
|
|
117
|
+
}), {
|
|
118
|
+
jsonToSpec: r,
|
|
119
|
+
...s
|
|
120
|
+
} = e, l = {
|
|
121
|
+
...s,
|
|
122
|
+
children: c
|
|
123
|
+
};
|
|
124
|
+
return u({
|
|
125
|
+
inSpecJson: l,
|
|
126
|
+
inShowLog: n,
|
|
127
|
+
inData: o
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return u({
|
|
132
|
+
inSpecJson: e,
|
|
133
|
+
inShowLog: n,
|
|
134
|
+
inData: o
|
|
135
|
+
});
|
|
136
|
+
}, T = ({
|
|
137
|
+
specJson: e,
|
|
138
|
+
showLog: n,
|
|
139
|
+
dataJson: o
|
|
140
|
+
}) => {
|
|
141
|
+
try {
|
|
142
|
+
return i({
|
|
143
|
+
inSpecJson: e,
|
|
144
|
+
inShowLog: n,
|
|
145
|
+
inDataJson: o
|
|
146
|
+
});
|
|
147
|
+
} catch (t) {
|
|
148
|
+
console.log("error : ", t);
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
d(T);
|
|
152
|
+
export {
|
|
153
|
+
T as default
|
|
154
|
+
};
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-to-spec",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "23.1.1",
|
|
4
4
|
"description": "Pure JSON Specification Compiler with jsonToSpec namespace: (structure, data) -> Spec JSON",
|
|
5
|
-
"main": "src/
|
|
6
|
-
"module": "./src/
|
|
5
|
+
"main": "src/v23/index.js",
|
|
6
|
+
"module": "./src/v23/index.js",
|
|
7
7
|
"files": [
|
|
8
8
|
"index.js",
|
|
9
9
|
"src",
|
|
@@ -35,9 +35,9 @@
|
|
|
35
35
|
"type": "module",
|
|
36
36
|
"exports": {
|
|
37
37
|
".": {
|
|
38
|
-
"import": "./src/
|
|
39
|
-
"require": "./src/
|
|
40
|
-
"default": "./src/
|
|
38
|
+
"import": "./src/v23/index.js",
|
|
39
|
+
"require": "./src/v23/index.js",
|
|
40
|
+
"default": "./src/v23/index.js"
|
|
41
41
|
}
|
|
42
42
|
},
|
|
43
43
|
"bugs": {
|
package/src/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { default } from "./
|
|
2
|
-
export * from "./
|
|
1
|
+
export { default } from "./v23/index.js";
|
|
2
|
+
export * from "./v23/index.js";
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import dispatchSpec from "./index.js";
|
|
2
|
+
|
|
3
|
+
export const buildChildrenNodes = ({ inChildren, inShowLog = false, inOutput }) => {
|
|
4
|
+
const localChildren = inChildren;
|
|
5
|
+
const localShowLog = inShowLog;
|
|
6
|
+
const localOutput = inOutput;
|
|
7
|
+
|
|
8
|
+
if (!Array.isArray(localChildren)) return [];
|
|
9
|
+
|
|
10
|
+
return localChildren.map(child => {
|
|
11
|
+
if (typeof child === "string" || typeof child === "number") {
|
|
12
|
+
return typeof document !== "undefined"
|
|
13
|
+
? document.createTextNode(String(child))
|
|
14
|
+
: String(child);
|
|
15
|
+
};
|
|
16
|
+
return dispatchSpec({
|
|
17
|
+
inSpec: child,
|
|
18
|
+
inShowLog: localShowLog,
|
|
19
|
+
inOutput: localOutput
|
|
20
|
+
});
|
|
21
|
+
}).flat().filter(Boolean);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export default buildChildrenNodes;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import buildChildrenNodes from "./buildChildrenNodes.js";
|
|
2
|
+
import forSpecFunc from "./forSpec/v3/index.js";
|
|
3
|
+
|
|
4
|
+
export const buildSingleElement = ({ inSpecJson, inShowLog = false, inDataJson }) => {
|
|
5
|
+
|
|
6
|
+
// debugger;
|
|
7
|
+
|
|
8
|
+
// const localChildrenNodes = Array.isArray(inSpecJson.children) && inSpecJson.children.length > 0
|
|
9
|
+
// ? buildChildrenNodes({
|
|
10
|
+
// inChildren: inSpecJson.children,
|
|
11
|
+
// inShowLog, inOutput: localOutput
|
|
12
|
+
// })
|
|
13
|
+
// : [];
|
|
14
|
+
|
|
15
|
+
return forSpecFunc({ inSpecJson, inData: inDataJson });
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export default buildSingleElement;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import dispatchSpec from "./index.js";
|
|
2
|
+
|
|
3
|
+
export const buildSpecArray = ({ inArray = [], inShowLog = false, inDataJson }) => {
|
|
4
|
+
const localArray = inArray;
|
|
5
|
+
const localShowLog = inShowLog;
|
|
6
|
+
const localDataJson = inDataJson;
|
|
7
|
+
|
|
8
|
+
if (!Array.isArray(localArray)) return [];
|
|
9
|
+
|
|
10
|
+
return localArray.map(item => dispatchSpec({
|
|
11
|
+
inSpecJson: item,
|
|
12
|
+
inShowLog: localShowLog, inDataJson: localDataJson
|
|
13
|
+
})).flat().filter(Boolean);
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export default buildSpecArray;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import dispatchSpec from "../../index.js";
|
|
2
|
+
|
|
3
|
+
const startFunc = ({ inTemplate, inDataAsArray }) => {
|
|
4
|
+
|
|
5
|
+
const localDataAsArray = inDataAsArray;
|
|
6
|
+
const localTemplate = inTemplate;
|
|
7
|
+
|
|
8
|
+
if (!Array.isArray(localDataAsArray)) {
|
|
9
|
+
return [];
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const childrenArray = localDataAsArray.map(element => {
|
|
13
|
+
|
|
14
|
+
// console.log("OUTER forArray element:", element);
|
|
15
|
+
|
|
16
|
+
const newTemplate = structuredClone(localTemplate);
|
|
17
|
+
|
|
18
|
+
return dispatchSpec({
|
|
19
|
+
inSpecJson: newTemplate,
|
|
20
|
+
inDataJson: element
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
return childrenArray;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export default startFunc;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import dispatchSpec from "../../index.js";
|
|
2
|
+
|
|
3
|
+
const startFunc = ({ inTemplate, inDataAsObject }) => {
|
|
4
|
+
|
|
5
|
+
const localDataAsObject = inDataAsObject;
|
|
6
|
+
const localTemplate = inTemplate;
|
|
7
|
+
|
|
8
|
+
if (
|
|
9
|
+
localDataAsObject === null ||
|
|
10
|
+
typeof localDataAsObject !== "object"
|
|
11
|
+
) {
|
|
12
|
+
return [];
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const childrenArray = [];
|
|
16
|
+
|
|
17
|
+
for (const [key, value] of Object.entries(localDataAsObject)) {
|
|
18
|
+
|
|
19
|
+
// console.log(
|
|
20
|
+
// "INNER:",
|
|
21
|
+
// key,
|
|
22
|
+
// value,
|
|
23
|
+
// typeof value
|
|
24
|
+
// );
|
|
25
|
+
|
|
26
|
+
const newTemplate = structuredClone(localTemplate);
|
|
27
|
+
|
|
28
|
+
const createdElement = dispatchSpec({
|
|
29
|
+
inSpecJson: newTemplate,
|
|
30
|
+
inDataJson: {
|
|
31
|
+
key,
|
|
32
|
+
value
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// console.log(
|
|
37
|
+
// "INNER createdElement:",
|
|
38
|
+
// createdElement
|
|
39
|
+
// );
|
|
40
|
+
|
|
41
|
+
childrenArray.push(createdElement);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
return childrenArray;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export default startFunc;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import buildSpec from "../../index.js";
|
|
2
|
+
|
|
3
|
+
const resolveTemplate = (template, data) => {
|
|
4
|
+
if (typeof template !== "string") {
|
|
5
|
+
return template;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
return template.replace(/\$\{([^}]+)\}/g, (_, path) => {
|
|
9
|
+
|
|
10
|
+
const keys = path.trim().split(".");
|
|
11
|
+
|
|
12
|
+
let value = data;
|
|
13
|
+
|
|
14
|
+
for (const key of keys) {
|
|
15
|
+
if (value === null || value === undefined) {
|
|
16
|
+
return "";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
value = value[key];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (
|
|
23
|
+
value === null ||
|
|
24
|
+
typeof value === "string" ||
|
|
25
|
+
typeof value === "number" ||
|
|
26
|
+
typeof value === "boolean"
|
|
27
|
+
) {
|
|
28
|
+
return String(value ?? "");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return value;
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const startFunc = ({ inSpecJson, inData, inShowLog }) => {
|
|
36
|
+
|
|
37
|
+
// Create a completely new tree.
|
|
38
|
+
const newSpec = structuredClone(inSpecJson);
|
|
39
|
+
|
|
40
|
+
if ("key" in inData && "value" in inData) {
|
|
41
|
+
|
|
42
|
+
if ("textContent" in newSpec) {
|
|
43
|
+
newSpec.textContent = resolveTemplate(
|
|
44
|
+
newSpec.textContent,
|
|
45
|
+
inData
|
|
46
|
+
);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
} else {
|
|
50
|
+
|
|
51
|
+
if ("textContent" in newSpec) {
|
|
52
|
+
newSpec.textContent = resolveTemplate(
|
|
53
|
+
newSpec.textContent,
|
|
54
|
+
inData
|
|
55
|
+
);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
if ("attributes" in newSpec) {
|
|
59
|
+
newSpec.attributes = Object.fromEntries(
|
|
60
|
+
Object.entries(newSpec.attributes).map(
|
|
61
|
+
([attributeName, attributeValue]) => [
|
|
62
|
+
attributeName,
|
|
63
|
+
resolveTemplate(attributeValue, inData)
|
|
64
|
+
]
|
|
65
|
+
)
|
|
66
|
+
);
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
if ("children" in newSpec) {
|
|
70
|
+
newSpec.children = newSpec.children.map((child) => {
|
|
71
|
+
return buildSpec({
|
|
72
|
+
inSpecJson: child,
|
|
73
|
+
inShowLog: inShowLog,
|
|
74
|
+
inDataJson: inData
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
return newSpec;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export default startFunc;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import domElementBuilder from "./elementBuilder/index.js";
|
|
2
|
+
import buildChildrenNodes from "./buildChildrenNodes.js";
|
|
3
|
+
|
|
4
|
+
const resolvePath = (path, data) => {
|
|
5
|
+
const parts = path.split(".");
|
|
6
|
+
|
|
7
|
+
let value = data;
|
|
8
|
+
|
|
9
|
+
for (const part of parts) {
|
|
10
|
+
if (value === null || value === undefined) {
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
value = value[part];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return value;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const replaceValue = (value, data) => {
|
|
21
|
+
|
|
22
|
+
if (typeof value !== "string") {
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const match = value.match(/^\$\{(.+?)\}$/);
|
|
27
|
+
|
|
28
|
+
if (!match) {
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return resolvePath(match[1], data);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export const buildSingleElement = ({ raka, inShowLog = false, poka }) => {
|
|
36
|
+
if (!raka?.tagName) {
|
|
37
|
+
if (localShowLog) {
|
|
38
|
+
console.warn("[json-to-dom v23] Missing tagName on spec:", raka);
|
|
39
|
+
}
|
|
40
|
+
return null;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const localChildrenNodes = Array.isArray(raka.children) && raka.children.length > 0
|
|
44
|
+
? buildChildrenNodes({
|
|
45
|
+
inChildren: raka.children,
|
|
46
|
+
inShowLog: localShowLog, inOutput: localOutput
|
|
47
|
+
})
|
|
48
|
+
: [];
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
if (poka.type === "dom") {
|
|
52
|
+
return domElementBuilder({
|
|
53
|
+
raka: {
|
|
54
|
+
...raka,
|
|
55
|
+
children: localChildrenNodes
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
if (poka.type === "spec") {
|
|
61
|
+
// raka.textContent = poka.data;
|
|
62
|
+
console.log("raka :", raka, poka);
|
|
63
|
+
debugger;
|
|
64
|
+
raka.textContent = replaceValue(raka.textContent, poka.data);
|
|
65
|
+
|
|
66
|
+
return raka;
|
|
67
|
+
// return domElementBuilder({
|
|
68
|
+
// inSpec: {
|
|
69
|
+
// ...localSpec,
|
|
70
|
+
// children: localChildrenNodes
|
|
71
|
+
// }
|
|
72
|
+
// });
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export default buildSingleElement;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export const isNullOrUndefined = ({ inSpec }) => {
|
|
2
|
+
const localSpec = inSpec;
|
|
3
|
+
return localSpec === null || localSpec === undefined;
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
export const isDomNode = ({ inSpec }) => {
|
|
7
|
+
const localSpec = inSpec;
|
|
8
|
+
return typeof Node !== "undefined" && localSpec instanceof Node;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const isSpecArray = ({ inSpecJson }) => {
|
|
12
|
+
const localSpec = inSpecJson;
|
|
13
|
+
return Array.isArray(localSpec);
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const isSpecObject = ({ inSpec }) => {
|
|
17
|
+
const localSpec = inSpec;
|
|
18
|
+
return typeof localSpec === "object" && localSpec !== null && !Array.isArray(localSpec);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export default {
|
|
22
|
+
isNullOrUndefined,
|
|
23
|
+
isDomNode,
|
|
24
|
+
isSpecArray,
|
|
25
|
+
isSpecObject
|
|
26
|
+
};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isNullOrUndefined,
|
|
3
|
+
isDomNode,
|
|
4
|
+
isSpecArray,
|
|
5
|
+
isSpecObject
|
|
6
|
+
} from "./guards.js";
|
|
7
|
+
|
|
8
|
+
import buildSpecArray from "./buildSpecArray.js";
|
|
9
|
+
import buildSingleElement from "./forSpec/v3/index.js";
|
|
10
|
+
import forArray from "./forArray/v1/index.js";
|
|
11
|
+
import forObject from "./forObject/v1/index.js";
|
|
12
|
+
|
|
13
|
+
const dispatchSpec = ({
|
|
14
|
+
inSpecJson,
|
|
15
|
+
inShowLog = false,
|
|
16
|
+
inDataJson
|
|
17
|
+
} = {}) => {
|
|
18
|
+
|
|
19
|
+
if (isNullOrUndefined({ inSpec: inSpecJson })) {
|
|
20
|
+
return null;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
if (isDomNode({ inSpec: inSpecJson })) {
|
|
24
|
+
return inSpecJson;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
if (isSpecArray({ inSpecJson })) {
|
|
28
|
+
return buildSpecArray({
|
|
29
|
+
inArray: inSpecJson,
|
|
30
|
+
inShowLog,
|
|
31
|
+
inDataJson
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
if ("jsonToSpec" in inSpecJson) {
|
|
36
|
+
|
|
37
|
+
if (inSpecJson.jsonToSpec.operation === "loopArray") {
|
|
38
|
+
|
|
39
|
+
const fromArray = forArray({
|
|
40
|
+
inTemplate: inSpecJson.jsonToSpec.template,
|
|
41
|
+
inDataAsArray:
|
|
42
|
+
inDataJson[inSpecJson.jsonToSpec.source]
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const {
|
|
46
|
+
jsonToSpec,
|
|
47
|
+
...specWithoutJsonToSpec
|
|
48
|
+
} = inSpecJson;
|
|
49
|
+
|
|
50
|
+
const newSpec = {
|
|
51
|
+
...specWithoutJsonToSpec,
|
|
52
|
+
children: fromArray
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
return buildSingleElement({
|
|
56
|
+
inSpecJson: newSpec,
|
|
57
|
+
inShowLog,
|
|
58
|
+
inData: inDataJson
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
if (inSpecJson.jsonToSpec.operation === "loopObject") {
|
|
63
|
+
|
|
64
|
+
const fromObject = forObject({
|
|
65
|
+
inTemplate: inSpecJson.jsonToSpec.template,
|
|
66
|
+
inDataAsObject: inDataJson
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const {
|
|
70
|
+
jsonToSpec,
|
|
71
|
+
...specWithoutJsonToSpec
|
|
72
|
+
} = inSpecJson;
|
|
73
|
+
|
|
74
|
+
const newSpec = {
|
|
75
|
+
...specWithoutJsonToSpec,
|
|
76
|
+
children: fromObject
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
return buildSingleElement({
|
|
80
|
+
inSpecJson: newSpec,
|
|
81
|
+
inShowLog,
|
|
82
|
+
inData: inDataJson
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const toReturnObject = buildSingleElement({
|
|
88
|
+
inSpecJson,
|
|
89
|
+
inShowLog,
|
|
90
|
+
inData: inDataJson
|
|
91
|
+
});
|
|
92
|
+
console.log("toReturnObject : ", inSpecJson, toReturnObject);
|
|
93
|
+
|
|
94
|
+
return toReturnObject;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export default dispatchSpec;
|
package/src/v22/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import registerGlobal from "./registerGlobal.js";
|
|
2
|
+
import buildSpec from "./buildSpec/index.js";
|
|
3
|
+
|
|
4
|
+
const buildSpecElement = ({ specJson,
|
|
5
|
+
showLog, dataJson }) => {
|
|
6
|
+
try {
|
|
7
|
+
|
|
8
|
+
const element = buildSpec({
|
|
9
|
+
inSpecJson: specJson,
|
|
10
|
+
inShowLog: showLog, inDataJson: dataJson
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
return element;
|
|
14
|
+
} catch (error) {
|
|
15
|
+
console.log("error : ", error);
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
registerGlobal(buildSpecElement);
|
|
20
|
+
|
|
21
|
+
export default buildSpecElement;
|
package/src/v22/meta.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import meta from "./meta.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 3. registerGlobal - Orchestration Step 3
|
|
5
|
+
* Safely registers the json-to-dom public API onto globalThis.ks in browser / SSR environments.
|
|
6
|
+
*/
|
|
7
|
+
export const registerGlobal = (inFuncDefinition) => {
|
|
8
|
+
if (typeof globalThis === "undefined" || !inFuncDefinition) return;
|
|
9
|
+
|
|
10
|
+
globalThis.ks ??= {};
|
|
11
|
+
globalThis.ks["json-to-spec"] = {
|
|
12
|
+
meta,
|
|
13
|
+
buildSpecElement: inFuncDefinition
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export default registerGlobal;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import dispatchSpec from "./index.js";
|
|
2
|
+
|
|
3
|
+
export const buildSpecArray = ({ inArray = [], inShowLog = false, inDataJson }) => {
|
|
4
|
+
const localArray = inArray;
|
|
5
|
+
const localShowLog = inShowLog;
|
|
6
|
+
const localDataJson = inDataJson;
|
|
7
|
+
|
|
8
|
+
if (!Array.isArray(localArray)) return [];
|
|
9
|
+
|
|
10
|
+
return localArray.map(item => dispatchSpec({
|
|
11
|
+
inSpecJson: item,
|
|
12
|
+
inShowLog: localShowLog, inDataJson: localDataJson
|
|
13
|
+
})).flat().filter(Boolean);
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export default buildSpecArray;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import dispatchSpec from "../../index.js";
|
|
2
|
+
|
|
3
|
+
const startFunc = ({ inTemplate, inDataAsArray }) => {
|
|
4
|
+
|
|
5
|
+
const localDataAsArray = inDataAsArray;
|
|
6
|
+
const localTemplate = inTemplate;
|
|
7
|
+
|
|
8
|
+
if (!Array.isArray(localDataAsArray)) {
|
|
9
|
+
return [];
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const childrenArray = localDataAsArray.map(element => {
|
|
13
|
+
|
|
14
|
+
// console.log("OUTER forArray element:", element);
|
|
15
|
+
|
|
16
|
+
const newTemplate = structuredClone(localTemplate);
|
|
17
|
+
|
|
18
|
+
return dispatchSpec({
|
|
19
|
+
inSpecJson: newTemplate,
|
|
20
|
+
inDataJson: element
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
return childrenArray;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export default startFunc;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import dispatchSpec from "../../index.js";
|
|
2
|
+
|
|
3
|
+
const startFunc = ({ inTemplate, inDataAsArray }) => {
|
|
4
|
+
|
|
5
|
+
const localDataAsArray = inDataAsArray;
|
|
6
|
+
const localTemplate = inTemplate;
|
|
7
|
+
|
|
8
|
+
if (!Array.isArray(localDataAsArray)) {
|
|
9
|
+
return [];
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const childrenArray = localDataAsArray.map(element => {
|
|
13
|
+
const newTemplate = structuredClone(localTemplate);
|
|
14
|
+
|
|
15
|
+
return dispatchSpec({
|
|
16
|
+
inSpecJson: newTemplate,
|
|
17
|
+
inDataJson: element
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
return childrenArray;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export default startFunc;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import dispatchSpec from "../../index.js";
|
|
2
|
+
|
|
3
|
+
const startFunc = ({ inTemplate, inDataAsObject }) => {
|
|
4
|
+
|
|
5
|
+
const localDataAsObject = inDataAsObject;
|
|
6
|
+
const localTemplate = inTemplate;
|
|
7
|
+
|
|
8
|
+
if (
|
|
9
|
+
localDataAsObject === null ||
|
|
10
|
+
typeof localDataAsObject !== "object"
|
|
11
|
+
) {
|
|
12
|
+
return [];
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const childrenArray = [];
|
|
16
|
+
|
|
17
|
+
for (const [key, value] of Object.entries(localDataAsObject)) {
|
|
18
|
+
|
|
19
|
+
// console.log(
|
|
20
|
+
// "INNER:",
|
|
21
|
+
// key,
|
|
22
|
+
// value,
|
|
23
|
+
// typeof value
|
|
24
|
+
// );
|
|
25
|
+
|
|
26
|
+
const newTemplate = structuredClone(localTemplate);
|
|
27
|
+
|
|
28
|
+
const createdElement = dispatchSpec({
|
|
29
|
+
inSpecJson: newTemplate,
|
|
30
|
+
inDataJson: {
|
|
31
|
+
key,
|
|
32
|
+
value
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// console.log(
|
|
37
|
+
// "INNER createdElement:",
|
|
38
|
+
// createdElement
|
|
39
|
+
// );
|
|
40
|
+
|
|
41
|
+
childrenArray.push(createdElement);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
return childrenArray;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export default startFunc;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import buildSpec from "../../index.js";
|
|
2
|
+
|
|
3
|
+
const resolveTemplate = (template, data) => {
|
|
4
|
+
if (typeof template !== "string") {
|
|
5
|
+
return template;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
return template.replace(/\$\{([^}]+)\}/g, (_, path) => {
|
|
9
|
+
|
|
10
|
+
const keys = path.trim().split(".");
|
|
11
|
+
|
|
12
|
+
let value = data;
|
|
13
|
+
|
|
14
|
+
for (const key of keys) {
|
|
15
|
+
if (value === null || value === undefined) {
|
|
16
|
+
return "";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
value = value[key];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (
|
|
23
|
+
value === null ||
|
|
24
|
+
typeof value === "string" ||
|
|
25
|
+
typeof value === "number" ||
|
|
26
|
+
typeof value === "boolean"
|
|
27
|
+
) {
|
|
28
|
+
return String(value ?? "");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return value;
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const startFunc = ({ inSpecJson, inData, inShowLog }) => {
|
|
36
|
+
// console.log("inData : ", inData);
|
|
37
|
+
|
|
38
|
+
// Create a completely new tree.
|
|
39
|
+
const newSpec = structuredClone(inSpecJson);
|
|
40
|
+
|
|
41
|
+
if (typeof inData === 'string') {
|
|
42
|
+
// console.log("It's a string!");
|
|
43
|
+
// console.log("inData : ", inData, newSpec);
|
|
44
|
+
if ("textContent" in newSpec) {
|
|
45
|
+
if (newSpec.textContent === "${}") {
|
|
46
|
+
newSpec.textContent = inData;
|
|
47
|
+
} else if (typeof newSpec.textContent === "string") {
|
|
48
|
+
newSpec.textContent = newSpec.textContent.replaceAll("${}", () => inData);
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
if ("attributes" in newSpec) {
|
|
53
|
+
newSpec.attributes = Object.fromEntries(
|
|
54
|
+
Object.entries(newSpec.attributes).map(
|
|
55
|
+
([attributeName, attributeValue]) => [
|
|
56
|
+
attributeName,
|
|
57
|
+
attributeValue === "${}"
|
|
58
|
+
? inData
|
|
59
|
+
: (typeof attributeValue === "string"
|
|
60
|
+
? attributeValue.replaceAll("${}", () => inData)
|
|
61
|
+
: attributeValue)
|
|
62
|
+
]
|
|
63
|
+
)
|
|
64
|
+
);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
} else {
|
|
68
|
+
if ("key" in inData && "value" in inData) {
|
|
69
|
+
|
|
70
|
+
if ("textContent" in newSpec) {
|
|
71
|
+
newSpec.textContent = resolveTemplate(
|
|
72
|
+
newSpec.textContent,
|
|
73
|
+
inData
|
|
74
|
+
);
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
} else {
|
|
78
|
+
|
|
79
|
+
if ("textContent" in newSpec) {
|
|
80
|
+
newSpec.textContent = resolveTemplate(
|
|
81
|
+
newSpec.textContent,
|
|
82
|
+
inData
|
|
83
|
+
);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
if ("attributes" in newSpec) {
|
|
87
|
+
newSpec.attributes = Object.fromEntries(
|
|
88
|
+
Object.entries(newSpec.attributes).map(
|
|
89
|
+
([attributeName, attributeValue]) => [
|
|
90
|
+
attributeName,
|
|
91
|
+
resolveTemplate(attributeValue, inData)
|
|
92
|
+
]
|
|
93
|
+
)
|
|
94
|
+
);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
if ("children" in newSpec) {
|
|
98
|
+
newSpec.children = newSpec.children.map((child) => {
|
|
99
|
+
return buildSpec({
|
|
100
|
+
inSpecJson: child,
|
|
101
|
+
inShowLog: inShowLog,
|
|
102
|
+
inDataJson: inData
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
return newSpec;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
export default startFunc;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export const isNullOrUndefined = ({ inSpec }) => {
|
|
2
|
+
const localSpec = inSpec;
|
|
3
|
+
return localSpec === null || localSpec === undefined;
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
export const isDomNode = ({ inSpec }) => {
|
|
7
|
+
const localSpec = inSpec;
|
|
8
|
+
return typeof Node !== "undefined" && localSpec instanceof Node;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const isSpecArray = ({ inSpecJson }) => {
|
|
12
|
+
const localSpec = inSpecJson;
|
|
13
|
+
return Array.isArray(localSpec);
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export default {
|
|
17
|
+
isNullOrUndefined,
|
|
18
|
+
isDomNode,
|
|
19
|
+
isSpecArray
|
|
20
|
+
};
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isNullOrUndefined,
|
|
3
|
+
isDomNode,
|
|
4
|
+
isSpecArray
|
|
5
|
+
} from "./guards.js";
|
|
6
|
+
|
|
7
|
+
import buildSpecArray from "./buildSpecArray.js";
|
|
8
|
+
import buildSingleElement from "./forSpec/v3/index.js";
|
|
9
|
+
import forArray from "./forArray/v1/index.js";
|
|
10
|
+
import forObject from "./forObject/v1/index.js";
|
|
11
|
+
import forArrayStrings from "./forArrayStrings/v1/index.js";
|
|
12
|
+
|
|
13
|
+
const dispatchSpec = ({
|
|
14
|
+
inSpecJson,
|
|
15
|
+
inShowLog = false,
|
|
16
|
+
inDataJson
|
|
17
|
+
} = {}) => {
|
|
18
|
+
|
|
19
|
+
if (isNullOrUndefined({ inSpec: inSpecJson })) {
|
|
20
|
+
return null;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
if (isDomNode({ inSpec: inSpecJson })) {
|
|
24
|
+
return inSpecJson;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
if (isSpecArray({ inSpecJson })) {
|
|
28
|
+
return buildSpecArray({
|
|
29
|
+
inArray: inSpecJson,
|
|
30
|
+
inShowLog,
|
|
31
|
+
inDataJson
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
if ("jsonToSpec" in inSpecJson) {
|
|
36
|
+
|
|
37
|
+
if (inSpecJson.jsonToSpec.operation === "loopArray") {
|
|
38
|
+
|
|
39
|
+
const fromArray = forArray({
|
|
40
|
+
inTemplate: inSpecJson.jsonToSpec.template,
|
|
41
|
+
inDataAsArray:
|
|
42
|
+
inDataJson[inSpecJson.jsonToSpec.source]
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const {
|
|
46
|
+
jsonToSpec,
|
|
47
|
+
...specWithoutJsonToSpec
|
|
48
|
+
} = inSpecJson;
|
|
49
|
+
|
|
50
|
+
const newSpec = {
|
|
51
|
+
...specWithoutJsonToSpec,
|
|
52
|
+
children: fromArray
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
return buildSingleElement({
|
|
56
|
+
inSpecJson: newSpec,
|
|
57
|
+
inShowLog,
|
|
58
|
+
inData: inDataJson
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
// may be this can be deleted
|
|
62
|
+
// if (inSpecJson.jsonToSpec.operation === "loopArrayStrings") {
|
|
63
|
+
// debugger
|
|
64
|
+
// const fromArray = forArrayStrings({
|
|
65
|
+
// inTemplate: inSpecJson.jsonToSpec.template,
|
|
66
|
+
// inDataAsArray:
|
|
67
|
+
// inDataJson[inSpecJson.jsonToSpec.source]
|
|
68
|
+
// });
|
|
69
|
+
|
|
70
|
+
// const {
|
|
71
|
+
// jsonToSpec,
|
|
72
|
+
// ...specWithoutJsonToSpec
|
|
73
|
+
// } = inSpecJson;
|
|
74
|
+
|
|
75
|
+
// const newSpec = {
|
|
76
|
+
// ...specWithoutJsonToSpec,
|
|
77
|
+
// children: fromArray
|
|
78
|
+
// };
|
|
79
|
+
|
|
80
|
+
// return buildSingleElement({
|
|
81
|
+
// inSpecJson: newSpec,
|
|
82
|
+
// inShowLog,
|
|
83
|
+
// inData: inDataJson
|
|
84
|
+
// });
|
|
85
|
+
// };
|
|
86
|
+
|
|
87
|
+
if (inSpecJson.jsonToSpec.operation === "loopObject") {
|
|
88
|
+
|
|
89
|
+
const fromObject = forObject({
|
|
90
|
+
inTemplate: inSpecJson.jsonToSpec.template,
|
|
91
|
+
inDataAsObject: inDataJson
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
const {
|
|
95
|
+
jsonToSpec,
|
|
96
|
+
...specWithoutJsonToSpec
|
|
97
|
+
} = inSpecJson;
|
|
98
|
+
|
|
99
|
+
const newSpec = {
|
|
100
|
+
...specWithoutJsonToSpec,
|
|
101
|
+
children: fromObject
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
return buildSingleElement({
|
|
105
|
+
inSpecJson: newSpec,
|
|
106
|
+
inShowLog,
|
|
107
|
+
inData: inDataJson
|
|
108
|
+
});
|
|
109
|
+
};
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const toReturnObject = buildSingleElement({
|
|
113
|
+
inSpecJson,
|
|
114
|
+
inShowLog,
|
|
115
|
+
inData: inDataJson
|
|
116
|
+
});
|
|
117
|
+
// console.log("toReturnObject : ", inSpecJson, toReturnObject);
|
|
118
|
+
|
|
119
|
+
return toReturnObject;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
export default dispatchSpec;
|
package/src/v23/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import registerGlobal from "./registerGlobal.js";
|
|
2
|
+
import buildSpec from "./buildSpec/index.js";
|
|
3
|
+
|
|
4
|
+
const buildSpecElement = ({ specJson,
|
|
5
|
+
showLog, dataJson }) => {
|
|
6
|
+
try {
|
|
7
|
+
|
|
8
|
+
const element = buildSpec({
|
|
9
|
+
inSpecJson: specJson,
|
|
10
|
+
inShowLog: showLog, inDataJson: dataJson
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
return element;
|
|
14
|
+
} catch (error) {
|
|
15
|
+
console.log("error : ", error);
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
registerGlobal(buildSpecElement);
|
|
20
|
+
|
|
21
|
+
export default buildSpecElement;
|
package/src/v23/meta.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import meta from "./meta.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 3. registerGlobal - Orchestration Step 3
|
|
5
|
+
* Safely registers the json-to-dom public API onto globalThis.ks in browser / SSR environments.
|
|
6
|
+
*/
|
|
7
|
+
export const registerGlobal = (inFuncDefinition) => {
|
|
8
|
+
if (typeof globalThis === "undefined" || !inFuncDefinition) return;
|
|
9
|
+
|
|
10
|
+
globalThis.ks ??= {};
|
|
11
|
+
globalThis.ks["json-to-spec"] = {
|
|
12
|
+
meta,
|
|
13
|
+
buildSpecElement: inFuncDefinition
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export default registerGlobal;
|