json-renderers-run-instructions 1.9.4 → 1.9.6

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/src/v9/index.js CHANGED
@@ -1,197 +1,195 @@
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
-
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 baseKey = definitions[instructionMainKey][instructionSubKey].baseFromSpec;
168
+ const instructionsToRun = definitions[instructionMainKey][instructionSubKey].instructions;
169
+ const optionDefs = definitions[instructionMainKey][instructionSubKey].options;
170
+
171
+ const base = structuredClone(spec[baseKey]);
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
+ runOptions({ base: context?.base, options, optionDefs, showLog });
191
+
192
+ return context?.base;
193
+ };
194
+
197
195
  export default startFunc;
@@ -1,6 +1,7 @@
1
- import { instructions as defaultInstructions } from "spec-repo";
1
+ import { instructions as defaultInstructions, spec } from "spec-repo";
2
2
  import runSingleInstructions from "json-instruction-single";
3
3
  import instructionsWithTargets from "./instructionsWithTargets/index.js";
4
+ import insertSearch from "./insertSearch/index.js";
4
5
 
5
6
  const getInstructionList = (optDef, instructions = defaultInstructions) => {
6
7
  if (!optDef) return [];
@@ -19,12 +20,16 @@ export const runOptions = ({
19
20
  if (!base) return base;
20
21
  const context = { base };
21
22
 
22
- if (options.showSearch && optionDefs.search) {
23
- const list = getInstructionList(optionDefs.search, instructions);
24
- const targets = instructionsWithTargets(list, base);
25
- if (showLog) console.log("[runOptions:search]", targets);
26
- runSingleInstructions(targets, context, showLog);
27
- }
23
+ if (options.showSearch) {
24
+ // const list = getInstructionList(optionDefs.search, instructions);
25
+ // const targets = instructionsWithTargets(list, base);
26
+ // if (showLog) console.log("[runOptions:search]", targets);
27
+ // runSingleInstructions(targets, context, showLog);
28
+
29
+ insertSearch({ inSpec: spec, inBase: base })
30
+ };
31
+
32
+
28
33
  if ((options.showSerial || options.serial) && optionDefs.serial) {
29
34
  const list = getInstructionList(optionDefs.serial, instructions);
30
35
  const targets = instructionsWithTargets(list, base);
@@ -0,0 +1,28 @@
1
+ import { paths } from "jsonpathly";
2
+
3
+ const startFunc = ({ inSpec, inBase }) => {
4
+ const spec = inSpec;
5
+ const newElement = spec?.tableSearchBar;
6
+
7
+ const matchedPaths = paths(inBase, '$..[?@.tagName == "table"]');
8
+
9
+ if (matchedPaths.length > 0) {
10
+ // 2. Parse the bracket segments (e.g., ["children", "0"])
11
+ const segments = [...matchedPaths[0].matchAll(/\[(?:'([^']*)'|(\d+))\]/g)].map(
12
+ (m) => (m[1] !== undefined ? m[1] : Number(m[2]))
13
+ );
14
+
15
+ // 3. Separate the target index from the parent array path
16
+ const tableIndex = segments.pop();
17
+ const parentArray = segments.reduce((obj, key) => obj[key], inBase);
18
+
19
+ // 4. Insert the new element right before the table
20
+ if (Array.isArray(parentArray) && typeof tableIndex === "number") {
21
+ parentArray.splice(tableIndex, 0, newElement);
22
+ };
23
+ };
24
+
25
+ return inBase;
26
+ };
27
+
28
+ export default startFunc;