json-renderers-run-instructions 1.9.4 → 1.11.2
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 +3 -3
- package/src/index.js +3 -3
- package/src/v10/index.js +218 -0
- package/src/v10/registerGlobal/index.js +16 -0
- package/src/v10/registerGlobal/meta.js +6 -0
- package/src/v10/runOptions/index.js +70 -0
- package/src/v10/runOptions/instructionsWithTargets/index.js +31 -0
- package/src/v10-2/index.js +202 -0
- package/src/v10-2/runOptions/index.js +52 -0
- package/src/v10-2/runOptions/instructionsWithTargets/index.js +31 -0
- package/src/v10-old/index.js +195 -0
- package/src/v10-old/runOptions/index.js +117 -0
- package/src/v10-old/runOptions/instructionsWithTargets/index.js +31 -0
- package/src/v11/index.js +219 -0
- package/src/v11/registerGlobal/index.js +16 -0
- package/src/v11/registerGlobal/meta.js +6 -0
- package/src/v11/runOptions/index.js +70 -0
- package/src/v11/runOptions/instructionsWithTargets/index.js +31 -0
- package/src/v8/index.js +192 -192
- package/src/v8/instructions.json +275 -275
- package/src/v9/index.js +194 -196
- package/src/v9/runOptions/index.js +12 -7
- package/src/v9/runOptions/insertSearch/index.js +28 -0
package/src/v8/index.js
CHANGED
|
@@ -1,193 +1,193 @@
|
|
|
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 defaultSpec from "./spec.json" with { type: "json" };
|
|
8
|
-
import defaultInstructionsJson from "./instructions.json" with { type: "json" };
|
|
9
|
-
import definitions from "./definitions.json" with { type: "json" };
|
|
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 = defaultSpec, 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
|
-
showLog = false
|
|
165
|
-
} = {}) => {
|
|
166
|
-
const spec = defaultSpec;
|
|
167
|
-
const formBaseKey = definitions[instructionMainKey][instructionSubKey].baseFromSpec;
|
|
168
|
-
const instructions = definitions[instructionMainKey][instructionSubKey].instructions;
|
|
169
|
-
|
|
170
|
-
const base = structuredClone(spec[formBaseKey]);
|
|
171
|
-
|
|
172
|
-
const context = { base, data };
|
|
173
|
-
const instructionsJson = defaultInstructionsJson;
|
|
174
|
-
|
|
175
|
-
instructions.forEach(instructionKeyOrObj => {
|
|
176
|
-
if (showLog) console.log("instruction : ", instructionKeyOrObj);
|
|
177
|
-
|
|
178
|
-
const instruction = typeof instructionKeyOrObj === "string"
|
|
179
|
-
? instructionsJson[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
|
-
return context?.base;
|
|
191
|
-
};
|
|
192
|
-
|
|
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 defaultSpec from "./spec.json" with { type: "json" };
|
|
8
|
+
import defaultInstructionsJson from "./instructions.json" with { type: "json" };
|
|
9
|
+
import definitions from "./definitions.json" with { type: "json" };
|
|
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 = defaultSpec, 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
|
+
showLog = false
|
|
165
|
+
} = {}) => {
|
|
166
|
+
const spec = defaultSpec;
|
|
167
|
+
const formBaseKey = definitions[instructionMainKey][instructionSubKey].baseFromSpec;
|
|
168
|
+
const instructions = definitions[instructionMainKey][instructionSubKey].instructions;
|
|
169
|
+
|
|
170
|
+
const base = structuredClone(spec[formBaseKey]);
|
|
171
|
+
|
|
172
|
+
const context = { base, data };
|
|
173
|
+
const instructionsJson = defaultInstructionsJson;
|
|
174
|
+
|
|
175
|
+
instructions.forEach(instructionKeyOrObj => {
|
|
176
|
+
if (showLog) console.log("instruction : ", instructionKeyOrObj);
|
|
177
|
+
|
|
178
|
+
const instruction = typeof instructionKeyOrObj === "string"
|
|
179
|
+
? instructionsJson[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
|
+
return context?.base;
|
|
191
|
+
};
|
|
192
|
+
|
|
193
193
|
export default startFunc;
|