json-to-spec 20.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.
Files changed (50) hide show
  1. package/docs/dist/min.js +137 -309
  2. package/docs/dist/v17/min.js +355 -355
  3. package/docs/dist/v18/min.js +160 -160
  4. package/docs/dist/v19/min.js +164 -164
  5. package/docs/dist/v20/min.js +326 -326
  6. package/docs/dist/v21/min.js +116 -0
  7. package/docs/dist/v23/min.js +154 -0
  8. package/package.json +6 -6
  9. package/samples/form/input/data.json +8 -8
  10. package/samples/form/input/structure.json +191 -191
  11. package/samples/form1/input/data.json +37 -37
  12. package/samples/table/input/structure.json +13 -13
  13. package/samples/table1/input/structure.json +10 -10
  14. package/samples/table2/input/structure.json +18 -18
  15. package/samples/table3/input/structure.json +44 -44
  16. package/src/index.js +2 -2
  17. package/src/v21/buildSpec/buildChildrenNodes.js +24 -0
  18. package/src/v21/buildSpec/buildSingleElement.js +18 -0
  19. package/src/v21/buildSpec/buildSpecArray.js +16 -0
  20. package/src/v21/buildSpec/forArray/v1/index.js +22 -0
  21. package/src/v21/buildSpec/forObject/v1/index.js +22 -0
  22. package/src/v21/buildSpec/forSpec/v3/index.js +91 -0
  23. package/src/v21/buildSpec/forSpec.js +76 -0
  24. package/src/v21/buildSpec/guards.js +26 -0
  25. package/src/v21/buildSpec/index.js +53 -0
  26. package/src/v21/index.js +21 -0
  27. package/src/v21/meta.js +6 -0
  28. package/src/v21/registerGlobal.js +17 -0
  29. package/src/v22/buildSpec/buildChildrenNodes.js +24 -0
  30. package/src/v22/buildSpec/buildSingleElement.js +18 -0
  31. package/src/v22/buildSpec/buildSpecArray.js +16 -0
  32. package/src/v22/buildSpec/forArray/v1/index.js +27 -0
  33. package/src/v22/buildSpec/forObject/v1/index.js +47 -0
  34. package/src/v22/buildSpec/forSpec/v3/index.js +84 -0
  35. package/src/v22/buildSpec/forSpec.js +76 -0
  36. package/src/v22/buildSpec/guards.js +26 -0
  37. package/src/v22/buildSpec/index.js +97 -0
  38. package/src/v22/index.js +21 -0
  39. package/src/v22/meta.js +6 -0
  40. package/src/v22/registerGlobal.js +17 -0
  41. package/src/v23/buildSpec/buildSpecArray.js +16 -0
  42. package/src/v23/buildSpec/forArray/v1/index.js +27 -0
  43. package/src/v23/buildSpec/forArrayStrings/v1/index.js +24 -0
  44. package/src/v23/buildSpec/forObject/v1/index.js +47 -0
  45. package/src/v23/buildSpec/forSpec/v3/index.js +114 -0
  46. package/src/v23/buildSpec/guards.js +20 -0
  47. package/src/v23/buildSpec/index.js +122 -0
  48. package/src/v23/index.js +21 -0
  49. package/src/v23/meta.js +6 -0
  50. package/src/v23/registerGlobal.js +17 -0
package/docs/dist/min.js CHANGED
@@ -1,326 +1,154 @@
1
1
  const f = {
2
- version: "19.0.0",
3
- name: "json-to-spec/v19",
4
- description: "Minimalist 2-layer compiler: pure value replace + jsonToSpec iterate"
5
- }, b = (n) => {
6
- typeof globalThis > "u" || !n || (globalThis.ks ?? (globalThis.ks = {}), globalThis.ks["json-to-spec"] = {
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"] = {
7
6
  meta: f,
8
- compile: n
7
+ buildSpecElement: e
9
8
  });
10
- }, d = ({ inData: n, inDataKey: r }) => {
11
- const t = r;
12
- if (t === "") return n;
13
- let e = n[t];
14
- return t.includes(".") && (e = t.split(".").reduce(
15
- (c, a) => c == null ? void 0 : c[a],
16
- n
17
- )), e;
18
- }, O = ({
19
- inNode: n,
20
- inData: r
21
- } = {}) => {
22
- const t = n, e = r;
23
- if (!(t != null && t.textContent)) return "no-textContent";
24
- if (typeof t.textContent != "string") return "not-a-string";
25
- if (!t.textContent.includes("${")) return "no-template-token";
26
- const o = t.textContent.replace(/^\$\{/, "").replace(/\}$/, "");
27
- t.textContent = d({
28
- inData: e,
29
- inDataKey: o
30
- });
31
- }, C = ({
32
- inNode: n,
33
- inData: r
34
- } = {}) => {
35
- const t = n, e = r;
36
- if ("attributes" in t) {
37
- const o = Object.fromEntries(
38
- Object.entries(t.attributes || {}).map(([s, c]) => [
39
- s,
40
- typeof c == "string" && c.includes("${") ? d({
41
- inData: e,
42
- inDataKey: c.replace(/^\$\{/, "").replace(/\}$/, "")
43
- }) : c
44
- ])
45
- );
46
- t.attributes = { ...o };
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];
47
26
  }
48
- }, $ = ({
49
- inNode: n,
50
- inData: r
51
- } = {}) => {
52
- const t = n, e = r;
53
- O({ inNode: t, inData: e }), C({ inNode: t, inData: e });
54
- }, T = ({
55
- inTemplate: n,
56
- inSourceValues: r
57
- } = {}) => {
58
- const t = r;
59
- return t === void 0 ? void 0 : t.map((o) => {
60
- const s = structuredClone(n);
61
- return l({
62
- inNode: s,
63
- inData: o,
64
- inOperation: "replace"
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
65
62
  });
66
- });
67
- }, k = ({
68
- inNode: n,
69
- inData: r,
70
- inShowLog: t
71
- } = {}) => {
72
- const e = n, o = r;
73
- if (!("jsonToSpec" in e) || !("operation" in e.jsonToSpec) || e.jsonToSpec.operation !== "iterate" || !("source" in e.jsonToSpec)) return;
74
- const s = o[e.jsonToSpec.source], c = T({
75
- inTemplate: e.jsonToSpec.template,
76
- inSourceValues: s
77
- });
78
- e.children = c;
79
- }, x = ({
80
- inNode: n,
81
- inData: r
82
- } = {}) => {
83
- const t = n;
84
- if (!(t != null && t.textContent)) return "no-textContent";
85
- if (typeof t.textContent != "string") return "not-a-string";
86
- if (!t.textContent.includes("${")) return "no-template-token";
87
- t.textContent === "${key}" && (t.textContent = r.key), t.textContent === "${value}" && (t.textContent = r.value);
88
- }, A = ({
89
- inNode: n,
90
- inData: r
91
- } = {}) => {
92
- const t = n;
93
- if ("attributes" in t) {
94
- const e = Object.fromEntries(
95
- Object.entries(t.attributes).map(([o, s]) => {
96
- let c = s;
97
- return s === "${key}" ? c = r.key : s === "${value}" && (c = r.value), [o, c];
98
- })
99
- );
100
- t.attributes = { ...e };
101
- }
102
- }, j = ({
103
- inNode: n,
104
- inData: r
105
- } = {}) => {
106
- const t = n, e = r;
107
- x({ inNode: t, inData: e }), A({ inNode: t, inData: e });
108
- };
109
- function m(n) {
110
- return n !== null && typeof n == "object" && Object.getPrototypeOf(n) === Object.prototype;
111
- }
112
- const h = ({
113
- inTemplate: n,
114
- inSourceValues: r,
115
- inShowLog: t
116
- } = {}) => {
117
- const e = r;
118
- if (e === void 0 || !m(e)) return;
119
- t && console.log("loopObject:createChildren:1 ", n, r);
120
- let o = [];
121
- for (const [s, c] of Object.entries(e)) {
122
- const a = structuredClone(n);
123
- j({ inNode: a, inData: { key: s, value: c } }), o.push(a);
124
- }
125
- return o;
126
- }, D = ({
127
- inNode: n,
128
- inData: r,
129
- inShowLog: t
130
- } = {}) => {
131
- const e = n, o = r;
132
- if (t && console.log("loopObject:1 ", n, r), !("jsonToSpec" in e) || !("operation" in e.jsonToSpec) || e.jsonToSpec.operation !== "loopObject" || !("source" in e.jsonToSpec)) return;
133
- t && console.log("loopObject:2 ", n, r);
134
- const s = o[e.jsonToSpec.source], c = h({
135
- inTemplate: e.jsonToSpec.template,
136
- inSourceValues: s,
137
- inShowLog: t
138
- });
139
- e.children = c;
140
- };
141
- function S(n) {
142
- return n !== null && typeof n == "object" && Object.getPrototypeOf(n) === Object.prototype;
143
- }
144
- const N = ({
145
- inNode: n,
146
- inData: r
147
- } = {}) => {
148
- if (Object.keys(r).map((o) => `\${${o}}`).includes(n.textContent)) {
149
- const o = n.textContent.slice(2, -1);
150
- n.textContent = r[o];
151
- }
152
- }, F = ({
153
- inNode: n,
154
- inData: r
155
- } = {}) => {
156
- const t = n, e = r;
157
- if (!(t != null && t.textContent)) return "no-textContent";
158
- if (typeof t.textContent != "string") return "not-a-string";
159
- if (!t.textContent.includes("${")) return "no-template-token";
160
- S(e) ? N({ inNode: n, inData: r }) : t.textContent === "${}" && (t.textContent = e);
161
- }, g = ({
162
- inNode: n,
163
- inData: r
164
- } = {}) => {
165
- const t = n;
166
- if ("attributes" in t) {
167
- const e = Object.fromEntries(
168
- Object.entries(t.attributes).map(([o, s]) => {
169
- let c = s;
170
- return s === "${}" && (c = r.key), [o, c];
171
- })
172
- );
173
- t.attributes = { ...e };
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);
174
78
  }
175
- }, V = ({
176
- inNode: n,
177
- inData: r
178
- } = {}) => {
179
- const t = n, e = r;
180
- F({ inNode: t, inData: e }), g({ inNode: t, inData: e });
181
- }, w = ({
182
- inTemplate: n,
183
- inSourceValuesAsArray: r,
184
- inShowLog: t
185
- } = {}) => {
186
- const e = r;
187
- if (e === void 0 || !Array.isArray(e)) return;
188
- t && console.log("loopObject:createChildren:1 ", n, inSourceValues);
189
- let o = [];
190
- return e.forEach((s) => {
191
- var a, i;
192
- const c = structuredClone(n);
193
- if ("jsonToSpec" in c) {
194
- const y = (a = c == null ? void 0 : c.jsonToSpec) == null ? void 0 : a.source;
195
- let u = {};
196
- u[y] = s, l({
197
- inNode: c,
198
- inData: u,
199
- inOperation: (i = c == null ? void 0 : c.jsonToSpec) == null ? void 0 : i.operation,
200
- inShowLog: t
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
201
111
  });
202
112
  }
203
- l({
204
- inNode: c,
205
- inData: s,
206
- inOperation: "replace",
207
- inShowLog: t
208
- }), V({ inNode: c, inData: s }), o.push(c);
209
- }), o;
210
- }, K = ({
211
- inNode: n,
212
- inData: r,
213
- inShowLog: t
214
- } = {}) => {
215
- const e = n, o = r;
216
- if (t && console.log("loopObject:1 ", n, r), !("jsonToSpec" in e) || !("operation" in e.jsonToSpec) || e.jsonToSpec.operation !== "loopArray" || !("source" in e.jsonToSpec)) return;
217
- const s = o[e.jsonToSpec.source];
218
- if (!Array.isArray(s)) return;
219
- t && console.log("loopObject:2 ", n, r);
220
- const c = w({
221
- inTemplate: e.jsonToSpec.template,
222
- inSourceValuesAsArray: s,
223
- inShowLog: t
224
- });
225
- e.children = c;
226
- }, p = ({ inNode: n, inData: r, inOperation: t }) => {
227
- const e = n, o = r, s = t;
228
- if (Array.isArray(e)) {
229
- const c = [];
230
- for (const a of e) {
231
- const i = l({
232
- inNode: a,
233
- inData: o,
234
- inOperation: s
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
235
128
  });
236
- Array.isArray(i) ? c.push(...i) : i != null && c.push(i);
237
129
  }
238
- return c;
239
- }
240
- }, l = ({
241
- inNode: n,
242
- inData: r = {},
243
- inOperation: t,
244
- inShowLog: e = !1
245
- } = {}) => {
246
- const o = n, s = r, c = t;
247
- if (e && console.log("walk ", n, r, t), o == null)
248
- return o;
249
- if (Array.isArray(o))
250
- return p({
251
- inNode: o,
252
- inData: r,
253
- inOperation: c
254
- });
255
- if ("children" in o && Array.isArray(o == null ? void 0 : o.children) && (o.children = p({
256
- inNode: o == null ? void 0 : o.children,
257
- inData: r,
258
- inOperation: c
259
- })), typeof o != "object")
260
- return o;
261
- switch (e && console.log("localOperation ", c), c) {
262
- case "replace":
263
- typeof o == "object" && $({
264
- inNode: o,
265
- inData: r
266
- });
267
- break;
268
- case "replaceObject":
269
- typeof o == "object" && j({
270
- inNode: o,
271
- inData: r
272
- });
273
- break;
274
- case "iterateDo":
275
- k({
276
- inNode: o,
277
- inData: s,
278
- inShowLog: e
279
- });
280
- break;
281
- case "loopObject":
282
- D({
283
- inNode: o,
284
- inData: s,
285
- inShowLog: e
286
- });
287
- break;
288
- case "loopArray":
289
- K({
290
- inNode: o,
291
- inData: s,
292
- inShowLog: e
293
- });
294
- break;
295
130
  }
296
- return o;
297
- }, E = ({ inStructureAsJson: n, inDataAsJson: r, inOperation: t, inShowLog: e }) => {
298
- try {
299
- return l({
300
- inNode: n,
301
- inData: r,
302
- inOperation: t,
303
- inShowLog: e
304
- });
305
- } catch (o) {
306
- throw console.error("[json-to-spec/v17] replace error:", o), o;
307
- }
308
- }, P = (n, r = {}, t = !1) => {
309
- let e = n, o = r;
310
- t && console.log(f.name, e, o);
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
+ }) => {
311
141
  try {
312
- return E({
313
- inStructureAsJson: e,
314
- inDataAsJson: o,
315
- inOperation: "loopArray",
316
- inShowLog: t
142
+ return i({
143
+ inSpecJson: e,
144
+ inShowLog: n,
145
+ inDataJson: o
317
146
  });
318
- } catch (s) {
319
- throw console.error("[json-to-spec/v19] compile error:", s), s;
147
+ } catch (t) {
148
+ console.log("error : ", t);
320
149
  }
321
150
  };
322
- b(P);
151
+ d(T);
323
152
  export {
324
- P as compile,
325
- P as default
153
+ T as default
326
154
  };