json-renderers-run-instructions 1.8.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.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-renderers-run-instructions",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.1",
|
|
4
4
|
"description": "Reusable instruction execution and JSON-to-DOM mounting runtime for JSON renderers",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
|
@@ -16,14 +16,15 @@
|
|
|
16
16
|
"build": "npx @keshavsoft/build-cli"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
+
"json-renderers-run-options": "^1.2.1",
|
|
19
20
|
"json-transformer-funcs-one-dimension": "^1.9.9",
|
|
20
21
|
"json-transformer-funcs-primitive": "^1.9.3",
|
|
21
22
|
"json-transformer-funcs-row-on-column": "^1.2.1",
|
|
22
23
|
"json-write-path": "^1.6.1",
|
|
23
24
|
"jsonpathly": "^4.0.0",
|
|
24
|
-
"spec-repo": "^1.
|
|
25
|
+
"spec-repo": "^1.17.1"
|
|
25
26
|
},
|
|
26
27
|
"devDependencies": {
|
|
27
|
-
"spec-repo": "^1.
|
|
28
|
+
"spec-repo": "^1.17.1"
|
|
28
29
|
}
|
|
29
|
-
}
|
|
30
|
+
}
|
package/src/v9/index.js
ADDED
|
@@ -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;
|