json-renderers-run-instructions 1.7.1 → 1.9.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,197 @@
1
+ import rowOnColumnFunc from "json-transformer-funcs-row-on-column";
2
+ import fromCollection from "json-transformer-funcs-one-dimension";
3
+ import applyTemplate from "json-transformer-funcs-primitive";
4
+ import writePath from "json-write-path";
5
+ import { query } from "jsonpathly";
6
+
7
+ import { spec, instructions, definitions } from "spec-repo";
8
+
9
+ import runOptions from "./runOptions/index.js";
10
+
11
+ const transformerFunctions = {
12
+ fromCollection,
13
+ applyTemplate
14
+ };
15
+
16
+ const resolveSource = (sourcePath, context) => {
17
+ if (!sourcePath) return null;
18
+
19
+ const root = context[sourcePath.fileName];
20
+ if (root === undefined) {
21
+ throw new Error(`Context entry not found: ${sourcePath.fileName}`);
22
+ }
23
+
24
+ if (sourcePath.jsonPath) {
25
+ const matches = query(root, sourcePath.jsonPath, { returnArray: true });
26
+ if (matches.length === 0) {
27
+ return [];
28
+ }
29
+ return matches.length === 1 ? matches[0] : matches;
30
+ }
31
+
32
+ if (Array.isArray(sourcePath.filePath) && sourcePath.filePath.length > 0) {
33
+ return sourcePath.filePath.reduce((curr, key) => (curr ? curr[key] : undefined), root);
34
+ }
35
+
36
+ return root;
37
+ };
38
+
39
+ const checkDestinationExists = (destinationPath, context) => {
40
+ if (!destinationPath) return true;
41
+ const root = context[destinationPath.fileName];
42
+ if (root === undefined) return false;
43
+
44
+ if (destinationPath.jsonPath) {
45
+ const matches = query(root, destinationPath.jsonPath, { returnArray: true });
46
+ return matches.length > 0;
47
+ }
48
+
49
+ return true;
50
+ };
51
+
52
+ const executeInstruction = ({ instruction, context, spec, showLog = false }) => {
53
+ if (!instruction) return null;
54
+
55
+ // Handle array of sub-instructions (e.g. serial, options, search)
56
+ if (Array.isArray(instruction)) {
57
+ return instruction.map(step => executeInstruction({ instruction: step, context, spec, showLog }));
58
+ }
59
+
60
+ // Auto-normalize legacy dataRole to jsonPath
61
+ if (instruction.destinationPath?.dataRole && !instruction.destinationPath?.jsonPath) {
62
+ instruction.destinationPath.jsonPath = `$..*[?@.attributes['data-role'] == '${instruction.destinationPath.dataRole}']`;
63
+ }
64
+
65
+ // If destination node does not exist in current base (e.g. footer row on a table without footer), safely skip
66
+ if (!checkDestinationExists(instruction.destinationPath, context)) {
67
+ if (showLog) {
68
+ console.warn(`Destination node not found for ${instruction.destinationPath?.jsonPath}, skipping instruction.`);
69
+ }
70
+ return null;
71
+ }
72
+
73
+ const type = instruction.type ?? (instruction.templateKey?.columnKey ? "rowOnColumn" : "single");
74
+
75
+ // 1. rowOnColumn instruction (e.g. buildBody for table)
76
+ if (type === "rowOnColumn") {
77
+ const columnKey = instruction?.templateKey?.columnKey;
78
+ const rowKey = instruction?.templateKey?.rowKey;
79
+ const columnSpec = spec[columnKey];
80
+ const rowSpec = spec[rowKey];
81
+
82
+ const columns = context.data?.columns ?? context.columns;
83
+ const rows = context.data?.data ?? (Array.isArray(context.data) ? context.data : context.rows);
84
+
85
+ const result = rowOnColumnFunc({
86
+ columns,
87
+ data: rows,
88
+ columnSpec,
89
+ rowSpec
90
+ });
91
+
92
+ writePath(context, instruction.destinationPath, result, showLog);
93
+ return result;
94
+ }
95
+
96
+ // 2. columnOnRow instruction (e.g. buildSelectWithOptions, buildDataListWithOptions)
97
+ if (type === "columnOnRow") {
98
+ const columnKey = instruction?.templateKey?.columnKey;
99
+ const rowKey = instruction?.templateKey?.rowKey;
100
+ const columnSpec = spec[columnKey];
101
+ const rowSpec = spec[rowKey];
102
+
103
+ const columns = context.data?.columns ?? context.columns ?? [];
104
+ const rows = context.data?.data ?? (Array.isArray(context.data) ? context.data : context.rows ?? []);
105
+
106
+ const result = columns.map(column => {
107
+ const container = applyTemplate(column, columnSpec);
108
+
109
+ let optionsList = [];
110
+ if (Array.isArray(column.options) && column.options.length > 0) {
111
+ optionsList = column.options.map(opt =>
112
+ typeof opt === "object" ? opt : { rowValue: opt }
113
+ );
114
+ } else if (Array.isArray(rows) && rows.length > 0) {
115
+ optionsList = rows.map(r => ({
116
+ ...r,
117
+ rowValue: r[column.columnName] ?? r.value ?? r
118
+ }));
119
+ }
120
+
121
+ container.children = optionsList.map(item => applyTemplate(item, rowSpec));
122
+ return container;
123
+ });
124
+
125
+ if (!instruction.destinationPath?.filePath || instruction.destinationPath.filePath.length === 0) {
126
+ context[instruction.destinationPath.fileName] = result;
127
+ } else {
128
+ writePath(context, instruction.destinationPath, result, showLog);
129
+ }
130
+ return result;
131
+ }
132
+
133
+ // 3. single instruction (e.g. buildColgroup, buildHeader, buildFooter, buildFormField, buildOptionsArray)
134
+ const template = instruction.templateKey ? spec[instruction.templateKey] : null;
135
+ const source = resolveSource(instruction.sourcePath, context);
136
+
137
+ let result;
138
+ if (instruction.func === null || instruction.func === undefined) {
139
+ result = template;
140
+ } else if (transformerFunctions[instruction.func]) {
141
+ result = transformerFunctions[instruction.func](source, template);
142
+ } else {
143
+ throw new Error(`Unknown function: ${instruction.func}`);
144
+ }
145
+
146
+ // If destination filePath is empty (e.g. writing directly into an array base like selectOptionsOnlyBase)
147
+ if (!instruction.destinationPath?.filePath || instruction.destinationPath.filePath.length === 0) {
148
+ if (Array.isArray(context[instruction.destinationPath.fileName])) {
149
+ context[instruction.destinationPath.fileName].push(...(Array.isArray(result) ? result : [result]));
150
+ } else {
151
+ context[instruction.destinationPath.fileName] = result;
152
+ }
153
+ } else {
154
+ writePath(context, instruction.destinationPath, result, showLog);
155
+ }
156
+
157
+ return result;
158
+ };
159
+
160
+ const startFunc = ({
161
+ instructionMainKey,
162
+ instructionSubKey,
163
+ data,
164
+ options,
165
+ showLog = false
166
+ } = {}) => {
167
+ const formBaseKey = definitions[instructionMainKey][instructionSubKey].baseFromSpec;
168
+ const instructionsToRun = definitions[instructionMainKey][instructionSubKey].instructions;
169
+ const optionDefs = definitions[instructionMainKey][instructionSubKey].options;
170
+
171
+ const base = structuredClone(spec[formBaseKey]);
172
+
173
+ const context = { base, data };
174
+
175
+ instructionsToRun.forEach(instructionKeyOrObj => {
176
+ if (showLog) console.log("instruction : ", instructionKeyOrObj);
177
+
178
+ const instruction = typeof instructionKeyOrObj === "string"
179
+ ? instructions[instructionKeyOrObj]
180
+ : instructionKeyOrObj;
181
+
182
+ if (!instruction) {
183
+ if (showLog) console.warn(`Instruction not found: ${instructionKeyOrObj}`);
184
+ return;
185
+ }
186
+
187
+ executeInstruction({ instruction, context, spec, showLog });
188
+ });
189
+
190
+ console.log("optionDefs: ", optionDefs);
191
+
192
+ runOptions({ base: context?.base, options, optionDefs, showLog });
193
+
194
+ return context?.base;
195
+ };
196
+
197
+ export default startFunc;
@@ -0,0 +1,26 @@
1
+ import runSingleInstructions from "json-instruction-single";
2
+ import instructionsWithTargets from "./instructionsWithTargets/index.js";
3
+
4
+ export const runOptions = ({ base, options = {}, optionDefs = {}, showLog = false } = {}) => {
5
+ if (!base) return base;
6
+ const context = { base };
7
+
8
+ if (options.showSearch && optionDefs.search) {
9
+ const targets = instructionsWithTargets(optionDefs.search, base);
10
+ if (showLog) console.log("[runOptions:search]", targets);
11
+ runSingleInstructions(targets, context, showLog);
12
+ }
13
+ if ((options.showSerial || options.serial) && optionDefs.serial) {
14
+ const targets = instructionsWithTargets(optionDefs.serial, base);
15
+ if (showLog) console.log("[runOptions:serial]", targets);
16
+ runSingleInstructions(targets, context, showLog);
17
+ }
18
+ if ((options.showOptions || options.options) && optionDefs.options) {
19
+ const targets = instructionsWithTargets(optionDefs.options, base);
20
+ if (showLog) console.log("[runOptions:options]", targets);
21
+ runSingleInstructions(targets, context, showLog);
22
+ }
23
+ return base;
24
+ };
25
+
26
+ export default runOptions;
@@ -0,0 +1,11 @@
1
+ import { query } from "jsonpathly";
2
+
3
+ const instructionsWithTargets = (instructions = [], base) => {
4
+ return instructions.filter(({ destinationPath }) => {
5
+ const jsonPath = destinationPath?.jsonPath;
6
+ if (typeof jsonPath !== "string" || !jsonPath.trim()) return false;
7
+ return query(base, jsonPath, { returnArray: true }).length > 0;
8
+ });
9
+ };
10
+
11
+ export default instructionsWithTargets;
package/src/v2/index.js DELETED
@@ -1,35 +0,0 @@
1
- import runSingleInstructions from "json-instruction-single";
2
- import runDoubleInstructions from "json-instruction-double";
3
-
4
- export const runInstructions = ({
5
- single = [],
6
- double = [],
7
- registry = {},
8
- showLog = false
9
- } = {}) => {
10
- if (!Array.isArray(single)) {
11
- throw new TypeError("single instructions must be an array");
12
- }
13
- if (!Array.isArray(double)) {
14
- throw new TypeError("double instructions must be an array");
15
- }
16
- if (!registry || typeof registry !== "object" || Array.isArray(registry)) {
17
- throw new TypeError("registry must be an object");
18
- };
19
- console.log("aaaaaaaaa : ", single, double);
20
-
21
-
22
- if (single.length > 0) {
23
- runSingleInstructions(single, registry, showLog);
24
- }
25
- if (double.length > 0) {
26
- runDoubleInstructions({
27
- instructions: double,
28
- registry
29
- });
30
- };
31
-
32
- return registry;
33
- };
34
-
35
- export default runInstructions;
package/src/v3/index.js DELETED
@@ -1,35 +0,0 @@
1
- import runSingleInstructions from "json-instruction-single";
2
- import runDoubleInstructions from "json-instruction-double";
3
-
4
- export const runInstructions = ({
5
- single = [],
6
- double = [],
7
- registry = {},
8
- showLog = false
9
- } = {}) => {
10
- if (showLog) console.log("runInstructions : ", single, double);
11
-
12
- if (!Array.isArray(single)) {
13
- throw new TypeError("single instructions must be an array");
14
- }
15
- if (!Array.isArray(double)) {
16
- throw new TypeError("double instructions must be an array");
17
- }
18
- if (!registry || typeof registry !== "object" || Array.isArray(registry)) {
19
- throw new TypeError("registry must be an object");
20
- };
21
-
22
- if (single.length > 0) {
23
- runSingleInstructions(single, registry, showLog);
24
- }
25
- if (double.length > 0) {
26
- runDoubleInstructions({
27
- instructions: double,
28
- registry
29
- });
30
- };
31
-
32
- return registry;
33
- };
34
-
35
- export default runInstructions;
package/src/v4/index.js DELETED
@@ -1,99 +0,0 @@
1
- import runSingleInstructions from "json-instruction-single";
2
- import runDoubleInstructions from "json-instruction-double";
3
- import rowOnColumnFunc from "json-transformer-funcs-row-on-column";
4
- import columnOnRowFunc from "json-transformer-funcs-column-on-row";
5
- import writePath from "json-write-path";
6
-
7
- const runRowOnColumnInstructions = ({ instructions = [], registry = {}, showLog = false } = {}) => {
8
- const localSpec = registry?.spec;
9
- const columns = registry?.data?.columns;
10
- const data = registry?.data?.data;
11
-
12
- instructions.forEach(instruction => {
13
- const columnSpec = localSpec ? localSpec[instruction?.templateKey?.columnKey] : undefined;
14
- const rowSpec = localSpec ? localSpec[instruction?.templateKey?.rowKey] : undefined;
15
-
16
- const result = rowOnColumnFunc({
17
- columns,
18
- data,
19
- columnSpec,
20
- rowSpec
21
- });
22
-
23
- if (showLog) console.log("[runInstructions:rowOnColumn]", instruction.narration, result);
24
- writePath(registry, instruction.destinationPath, result);
25
- });
26
- };
27
-
28
- const runColumnOnRowInstructions = ({ instructions = [], registry = {}, showLog = false } = {}) => {
29
- const localSpec = registry?.spec;
30
- const columns = registry?.data?.columns;
31
- const data = registry?.data?.data;
32
-
33
- instructions.forEach(instruction => {
34
- const columnSpec = localSpec ? localSpec[instruction?.templateKey?.columnKey] : undefined;
35
- const rowSpec = localSpec ? localSpec[instruction?.templateKey?.rowKey] : undefined;
36
-
37
- const result = columnOnRowFunc({
38
- columns,
39
- data,
40
- columnSpec,
41
- rowSpec
42
- });
43
-
44
- if (showLog) console.log("[runInstructions:columnOnRow]", instruction.narration, result);
45
- writePath(registry, instruction.destinationPath, result);
46
- });
47
- };
48
-
49
- export const runInstructions = ({
50
- single = [],
51
- double = [],
52
- rowOnColumn = [],
53
- columnOnRow = [],
54
- registry = {},
55
- showLog = false
56
- } = {}) => {
57
- if (showLog) console.log("runInstructions input:", { single, double, rowOnColumn, columnOnRow });
58
-
59
- if (!Array.isArray(single)) throw new TypeError("single instructions must be an array");
60
- if (!Array.isArray(double)) throw new TypeError("double instructions must be an array");
61
- if (!Array.isArray(rowOnColumn)) throw new TypeError("rowOnColumn instructions must be an array");
62
- if (!Array.isArray(columnOnRow)) throw new TypeError("columnOnRow instructions must be an array");
63
- if (!registry || typeof registry !== "object" || Array.isArray(registry)) {
64
- throw new TypeError("registry must be an object");
65
- }
66
-
67
- if (single.length > 0) {
68
- runSingleInstructions(single, registry, showLog);
69
- }
70
-
71
- if (double.length > 0) {
72
- runDoubleInstructions({
73
- instructions: double,
74
- registry,
75
- showLog
76
- });
77
- }
78
-
79
- if (rowOnColumn.length > 0) {
80
- runRowOnColumnInstructions({
81
- instructions: rowOnColumn,
82
- registry,
83
- showLog
84
- });
85
- }
86
-
87
- if (columnOnRow.length > 0) {
88
- runColumnOnRowInstructions({
89
- instructions: columnOnRow,
90
- registry,
91
- showLog
92
- });
93
- }
94
-
95
- return registry;
96
- };
97
-
98
- export default runInstructions;
99
-
package/src/v5/index.js DELETED
@@ -1,68 +0,0 @@
1
- import runSingleInstructions from "json-instruction-single";
2
- import runDoubleInstructions from "json-instruction-double";
3
- import rowOnColumnFunc from "json-transformer-funcs-row-on-column";
4
- import columnOnRowFunc from "json-transformer-funcs-column-on-row";
5
- import writePath from "json-write-path";
6
- import spec from "spec-repo";
7
-
8
- const runRowOnColumnInstructions = ({ instructions = [], base, data, showLog = false } = {}) => {
9
- instructions.forEach(instruction => {
10
- const columnSpec = spec[instruction?.templateKey?.columnKey];
11
- const rowSpec = spec[instruction?.templateKey?.rowKey];
12
-
13
- const result = rowOnColumnFunc({
14
- columns: data?.columns,
15
- data: data?.data,
16
- columnSpec,
17
- rowSpec
18
- });
19
-
20
- if (showLog) console.log("[runInstructions:rowOnColumn]", instruction.narration, result);
21
- writePath({ base, data }, instruction.destinationPath, result);
22
- });
23
- };
24
-
25
- const runColumnOnRowInstructions = ({ instructions = [], base, data, showLog = false } = {}) => {
26
- instructions.forEach(instruction => {
27
- const columnSpec = spec[instruction?.templateKey?.columnKey];
28
- const rowSpec = spec[instruction?.templateKey?.rowKey];
29
-
30
- const result = columnOnRowFunc({
31
- columns: data?.columns,
32
- data: data?.data,
33
- columnSpec,
34
- rowSpec
35
- });
36
-
37
- if (showLog) console.log("[runInstructions:columnOnRow]", instruction.narration, result);
38
- writePath({ base, data }, instruction.destinationPath, result);
39
- });
40
- };
41
-
42
- export const runInstructions = ({
43
- single = [],
44
- double = [],
45
- rowOnColumn = [],
46
- columnOnRow = [],
47
- base,
48
- data,
49
- showLog = false
50
- } = {}) => {
51
- if (showLog) console.log("runInstructions input:", { single, double, rowOnColumn, columnOnRow, base, data });
52
-
53
- if (!Array.isArray(single)) throw new TypeError("single instructions must be an array");
54
- if (!Array.isArray(double)) throw new TypeError("double instructions must be an array");
55
- if (!Array.isArray(rowOnColumn)) throw new TypeError("rowOnColumn instructions must be an array");
56
- if (!Array.isArray(columnOnRow)) throw new TypeError("columnOnRow instructions must be an array");
57
-
58
- const context = { base, data };
59
-
60
- if (single.length > 0) runSingleInstructions(single, context, showLog);
61
- if (double.length > 0) runDoubleInstructions({ instructions: double, registry: context, showLog });
62
- if (rowOnColumn.length > 0) runRowOnColumnInstructions({ instructions: rowOnColumn, base, data, showLog });
63
- if (columnOnRow.length > 0) runColumnOnRowInstructions({ instructions: columnOnRow, base, data, showLog });
64
-
65
- return context;
66
- };
67
-
68
- export default runInstructions;