json-renderers-run-instructions 1.11.2 → 1.15.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 (48) hide show
  1. package/package.json +2 -5
  2. package/src/index.js +3 -3
  3. package/src/{v11 → v12}/index.js +178 -219
  4. package/src/{v10 → v12}/registerGlobal/index.js +1 -1
  5. package/src/v12/registerGlobal/meta.js +6 -0
  6. package/src/{v10 → v12}/runOptions/index.js +66 -70
  7. package/src/v12/transformers/columnOnRow.js +33 -0
  8. package/src/v12/transformers/index.js +21 -0
  9. package/src/v13/index.js +49 -0
  10. package/src/{v11 → v13}/registerGlobal/index.js +1 -1
  11. package/src/v13/registerGlobal/meta.js +6 -0
  12. package/src/{v11 → v13}/runOptions/index.js +66 -70
  13. package/src/v13/transformers/columnOnRow.js +33 -0
  14. package/src/v13/transformers/index.js +21 -0
  15. package/src/v13/types/columnOnRow.js +34 -0
  16. package/src/v13/types/helpers/guards.js +38 -0
  17. package/src/v13/types/helpers/resolveSource.js +27 -0
  18. package/src/v13/types/helpers/writeResult.js +19 -0
  19. package/src/v13/types/index.js +33 -0
  20. package/src/v13/types/rowOnColumn.js +22 -0
  21. package/src/v13/types/single.js +28 -0
  22. package/src/v14/index.js +48 -0
  23. package/src/v14/registerGlobal/index.js +16 -0
  24. package/src/{v10 → v14}/registerGlobal/meta.js +1 -1
  25. package/src/v14/runOptions/index.js +77 -0
  26. package/src/v15/index.js +48 -0
  27. package/src/v15/registerGlobal/index.js +16 -0
  28. package/src/{v11 → v15}/registerGlobal/meta.js +1 -1
  29. package/src/v15/runOptions/index.js +77 -0
  30. package/src/v10/index.js +0 -218
  31. package/src/v10/runOptions/instructionsWithTargets/index.js +0 -31
  32. package/src/v10-2/index.js +0 -202
  33. package/src/v10-2/runOptions/index.js +0 -52
  34. package/src/v10-2/runOptions/instructionsWithTargets/index.js +0 -31
  35. package/src/v10-old/index.js +0 -195
  36. package/src/v10-old/runOptions/index.js +0 -117
  37. package/src/v10-old/runOptions/instructionsWithTargets/index.js +0 -31
  38. package/src/v11/runOptions/instructionsWithTargets/index.js +0 -31
  39. package/src/v6/index.js +0 -68
  40. package/src/v7/index.js +0 -57
  41. package/src/v8/definitions.json +0 -165
  42. package/src/v8/index.js +0 -193
  43. package/src/v8/instructions.json +0 -275
  44. package/src/v8/spec.json +0 -900
  45. package/src/v9/index.js +0 -195
  46. package/src/v9/runOptions/index.js +0 -48
  47. package/src/v9/runOptions/insertSearch/index.js +0 -28
  48. package/src/v9/runOptions/instructionsWithTargets/index.js +0 -12
@@ -0,0 +1,16 @@
1
+ import meta from "./meta.js";
2
+
3
+ const registerGlobal = (renderFunc) => {
4
+ if (typeof globalThis === "undefined" || !renderFunc) return;
5
+
6
+ globalThis.ks ??= {};
7
+ globalThis.ks.jsonRenderers ??= {};
8
+ globalThis.ks.jsonRenderers.runInstructions ??= {};
9
+
10
+ globalThis.ks.jsonRenderers.runInstructions.render = {
11
+ meta,
12
+ renderFunc
13
+ };
14
+ };
15
+
16
+ export default registerGlobal;
@@ -1,6 +1,6 @@
1
1
  export const meta = {
2
2
  name: "json-renderers-run-instructions",
3
- version: "1.10.1"
3
+ version: "1.15.1"
4
4
  };
5
5
 
6
6
  export default meta;
@@ -0,0 +1,77 @@
1
+ import { spec, instructions } from "spec-repo";
2
+ import writePath from "json-write-path";
3
+ import { query } from "jsonpathly";
4
+
5
+ const isObject = value => value !== null && typeof value === "object" && !Array.isArray(value);
6
+
7
+ const getFeatureOptions = (options, featureKey) => {
8
+ if (!isObject(options)) return null;
9
+ if (options[featureKey] !== undefined) return options[featureKey];
10
+ const prefixed = `show${featureKey.charAt(0).toUpperCase()}${featureKey.slice(1)}`;
11
+ if (options[prefixed] !== undefined) return options[prefixed];
12
+ return null;
13
+ };
14
+
15
+ const applyOption = (instructionKey, base) => {
16
+ const list = instructions[instructionKey];
17
+ if (!list) return;
18
+
19
+ for (const step of list) {
20
+ if (step.skipIf && query(base, step.skipIf, { returnArray: true }).length > 0) continue;
21
+ const template = spec[step.templateKey];
22
+ try {
23
+ writePath({ base }, step.destinationPath, template);
24
+ } catch {
25
+ // Optional target node not in base (e.g. footer on table without footer)
26
+ }
27
+ }
28
+ };
29
+
30
+ /**
31
+ * Task 2: Applies key-value feature options (search, serial, actions) to base.
32
+ */
33
+ const runOptions = ({
34
+ options = {},
35
+ optionDefs = {},
36
+ base,
37
+ showLog = false
38
+ } = {}) => {
39
+ if (!base || !isObject(options) || !isObject(optionDefs)) {
40
+ return base;
41
+ }
42
+
43
+ for (const [featureKey, featureSwitches] of Object.entries(optionDefs)) {
44
+ if (!isObject(featureSwitches)) continue;
45
+
46
+ const userFeature = getFeatureOptions(options, featureKey);
47
+ if (!userFeature) continue;
48
+
49
+ const isBool = typeof userFeature === "boolean" && userFeature === true;
50
+ const isObj = isObject(userFeature);
51
+ if (!isBool && !isObj) continue;
52
+
53
+ const hasActiveSwitch = Object.entries(featureSwitches).some(
54
+ ([switchKey]) => switchKey !== "initialize" && (isBool || userFeature[switchKey] === true)
55
+ );
56
+ if (!hasActiveSwitch) continue;
57
+
58
+ // 1. Run initialize option if present (e.g. "searchFrame")
59
+ if (featureSwitches.initialize) {
60
+ applyOption(featureSwitches.initialize, base);
61
+ }
62
+
63
+ // 2. Run enabled switch options (e.g. "searchLabel", "searchInput")
64
+ for (const [switchKey, instructionKey] of Object.entries(featureSwitches)) {
65
+ if (switchKey === "initialize") continue;
66
+
67
+ const isEnabled = isBool || userFeature[switchKey] === true;
68
+ if (isEnabled) {
69
+ applyOption(instructionKey, base);
70
+ }
71
+ }
72
+ }
73
+
74
+ return base;
75
+ };
76
+
77
+ export default runOptions;
package/src/v10/index.js DELETED
@@ -1,218 +0,0 @@
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
- import registerGlobal from "./registerGlobal/index.js";
7
-
8
- import { spec, instructions, definitions } from "spec-repo";
9
-
10
- import runOptions from "./runOptions/index.js";
11
-
12
- const transformerFunctions = {
13
- fromCollection,
14
- applyTemplate
15
- };
16
-
17
- const resolveSource = (sourcePath, context) => {
18
- if (!sourcePath) return null;
19
-
20
- const root = context[sourcePath.fileName];
21
- if (root === undefined) {
22
- throw new Error(`Context entry not found: ${sourcePath.fileName}`);
23
- }
24
-
25
- if (sourcePath.jsonPath) {
26
- const matches = query(root, sourcePath.jsonPath, { returnArray: true });
27
- if (matches.length === 0) {
28
- return [];
29
- }
30
- return matches.length === 1 ? matches[0] : matches;
31
- }
32
-
33
- if (Array.isArray(sourcePath.filePath) && sourcePath.filePath.length > 0) {
34
- return sourcePath.filePath.reduce((curr, key) => (curr ? curr[key] : undefined), root);
35
- }
36
-
37
- return root;
38
- };
39
-
40
- const checkDestinationExists = (destinationPath, context) => {
41
- if (!destinationPath) return true;
42
- const root = context[destinationPath.fileName];
43
- if (root === undefined) return false;
44
-
45
- if (destinationPath.jsonPath) {
46
- const matches = query(root, destinationPath.jsonPath, { returnArray: true });
47
- return matches.length > 0;
48
- }
49
-
50
- return true;
51
- };
52
-
53
- const executeInstruction = ({ instruction, context, spec, showLog = false }) => {
54
- if (!instruction) return null;
55
-
56
- // Handle array of sub-instructions (e.g. serial, options, search)
57
- if (Array.isArray(instruction)) {
58
- return instruction.map(step => executeInstruction({ instruction: step, context, spec, showLog }));
59
- }
60
-
61
- // Auto-normalize legacy dataRole to jsonPath
62
- if (instruction.destinationPath?.dataRole && !instruction.destinationPath?.jsonPath) {
63
- instruction.destinationPath.jsonPath = `$..*[?@.attributes['data-role'] == '${instruction.destinationPath.dataRole}']`;
64
- }
65
-
66
- // Skip if instruction has skipIf condition and target already exists
67
- if (instruction.skipIf && query(context.base, instruction.skipIf, { returnArray: true }).length > 0) {
68
- return null;
69
- }
70
-
71
- // If destination node does not exist in current base (e.g. footer row on a table without footer), safely skip
72
- if (!checkDestinationExists(instruction.destinationPath, context)) {
73
- if (showLog) {
74
- console.warn(`Destination node not found for ${instruction.destinationPath?.jsonPath}, skipping instruction.`);
75
- }
76
- return null;
77
- }
78
-
79
- const type = instruction.type ?? (instruction.templateKey?.columnKey ? "rowOnColumn" : "single");
80
-
81
- // 1. rowOnColumn instruction (e.g. buildBody for table)
82
- if (type === "rowOnColumn") {
83
- const columnKey = instruction?.templateKey?.columnKey;
84
- const rowKey = instruction?.templateKey?.rowKey;
85
- const columnSpec = spec[columnKey];
86
- const rowSpec = spec[rowKey];
87
-
88
- const columns = context.data?.columns ?? context.columns;
89
- const rows = context.data?.data ?? (Array.isArray(context.data) ? context.data : context.rows);
90
-
91
- const result = rowOnColumnFunc({
92
- columns,
93
- data: rows,
94
- columnSpec,
95
- rowSpec
96
- });
97
-
98
- writePath(context, instruction.destinationPath, result, showLog);
99
- return result;
100
- }
101
-
102
- // 2. columnOnRow instruction (e.g. buildSelectWithOptions, buildDataListWithOptions)
103
- if (type === "columnOnRow") {
104
- const columnKey = instruction?.templateKey?.columnKey;
105
- const rowKey = instruction?.templateKey?.rowKey;
106
- const columnSpec = spec[columnKey];
107
- const rowSpec = spec[rowKey];
108
-
109
- const columns = context.data?.columns ?? context.columns ?? [];
110
- const rows = context.data?.data ?? (Array.isArray(context.data) ? context.data : context.rows ?? []);
111
-
112
- const result = columns.map(column => {
113
- const container = applyTemplate(column, columnSpec);
114
-
115
- let optionsList = [];
116
- if (Array.isArray(column.options) && column.options.length > 0) {
117
- optionsList = column.options.map(opt =>
118
- typeof opt === "object" ? opt : { rowValue: opt }
119
- );
120
- } else if (Array.isArray(rows) && rows.length > 0) {
121
- optionsList = rows.map(r => ({
122
- ...r,
123
- rowValue: r[column.columnName] ?? r.value ?? r
124
- }));
125
- }
126
-
127
- container.children = optionsList.map(item => applyTemplate(item, rowSpec));
128
- return container;
129
- });
130
-
131
- if (!instruction.destinationPath?.filePath || instruction.destinationPath.filePath.length === 0) {
132
- context[instruction.destinationPath.fileName] = result;
133
- } else {
134
- writePath(context, instruction.destinationPath, result, showLog);
135
- }
136
- return result;
137
- }
138
-
139
- // 3. single instruction (e.g. buildColgroup, buildHeader, buildFooter, buildFormField, buildOptionsArray)
140
- const template = instruction.templateKey ? spec[instruction.templateKey] : null;
141
- const source = resolveSource(instruction.sourcePath, context);
142
-
143
- let result;
144
- if (instruction.func === null || instruction.func === undefined) {
145
- result = structuredClone(template);
146
- } else if (transformerFunctions[instruction.func]) {
147
- result = transformerFunctions[instruction.func](source, template);
148
- } else {
149
- throw new Error(`Unknown function: ${instruction.func}`);
150
- }
151
-
152
- // If destination filePath is empty (e.g. writing directly into an array base like selectOptionsOnlyBase)
153
- if (!instruction.destinationPath?.filePath || instruction.destinationPath.filePath.length === 0) {
154
- if (Array.isArray(context[instruction.destinationPath.fileName])) {
155
- context[instruction.destinationPath.fileName].push(...(Array.isArray(result) ? result : [result]));
156
- } else {
157
- context[instruction.destinationPath.fileName] = result;
158
- }
159
- } else {
160
- writePath(context, instruction.destinationPath, result, showLog);
161
- }
162
-
163
- return result;
164
- };
165
-
166
- const startFunc = ({
167
- instructionMainKey,
168
- instructionSubKey,
169
- data,
170
- options,
171
- showLog = false
172
- } = {}) => {
173
- const definition = definitions[instructionMainKey]?.[instructionSubKey];
174
- if (!definition) {
175
- throw new Error(`Definition not found: ${instructionMainKey}.${instructionSubKey}`);
176
- }
177
-
178
- const baseKey = definition.baseFromSpec;
179
- const instructionsToRun = definition.instructions ?? [];
180
- const optionDefs = (definition.options && typeof definition.options === "object")
181
- ? definition.options
182
- : (definitions[instructionMainKey]?.options ?? instructions.options?.[definition.options] ?? {});
183
-
184
- const base = structuredClone(spec[baseKey]);
185
-
186
- const context = { base, data };
187
-
188
- instructionsToRun.forEach(instructionKeyOrObj => {
189
- if (showLog) console.log("instruction : ", instructionKeyOrObj);
190
-
191
- const instruction = typeof instructionKeyOrObj === "string"
192
- ? instructions[instructionKeyOrObj]
193
- : instructionKeyOrObj;
194
-
195
- if (!instruction) {
196
- if (showLog) console.warn(`Instruction not found: ${instructionKeyOrObj}`);
197
- return;
198
- }
199
-
200
- executeInstruction({ instruction, context, spec, showLog });
201
- });
202
-
203
- runOptions({
204
- base: context?.base,
205
- options,
206
- optionDefs,
207
- instructions,
208
- spec,
209
- executeInstruction: ({ instruction }) => executeInstruction({ instruction, context, spec, showLog }),
210
- showLog
211
- });
212
-
213
- return context?.base;
214
- };
215
-
216
- export default startFunc;
217
-
218
- registerGlobal(startFunc);
@@ -1,31 +0,0 @@
1
- import { query } from "jsonpathly";
2
-
3
- const hasMatch = (base, expression) => {
4
- if (typeof expression !== "string" || !expression.trim()) return false;
5
- return query(base, expression, { returnArray: true }).length > 0;
6
- };
7
-
8
- const matchesSkipCondition = (base, skipIf) => {
9
- if (!skipIf) return false;
10
- const expressions = Array.isArray(skipIf) ? skipIf : [skipIf];
11
- return expressions.some(expression => hasMatch(base, expression));
12
- };
13
-
14
- const instructionsWithTargets = (instructions = [], base) => {
15
- const list = Array.isArray(instructions) ? instructions : [];
16
-
17
- return list.filter(instruction => {
18
- const destinationPath = instruction?.destinationPath;
19
- const root = destinationPath?.fileName
20
- ? (destinationPath.fileName === "base" ? base : undefined)
21
- : base;
22
-
23
- if (!destinationPath?.jsonPath) return false;
24
- if (!hasMatch(root, destinationPath.jsonPath)) return false;
25
- if (matchesSkipCondition(base, instruction.skipIf)) return false;
26
-
27
- return true;
28
- });
29
- };
30
-
31
- export default instructionsWithTargets;
@@ -1,202 +0,0 @@
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 definition = definitions[instructionMainKey]?.[instructionSubKey];
168
- if (!definition) {
169
- throw new Error(`Definition not found: ${instructionMainKey}.${instructionSubKey}`);
170
- }
171
-
172
- const baseKey = definition.baseFromSpec;
173
- const instructionsToRun = definition.instructions ?? [];
174
- const optionDefs = definition.options
175
- ?? definitions._optionSets?.[definition.optionsRef]
176
- ?? {};
177
-
178
- const base = structuredClone(spec[baseKey]);
179
-
180
- const context = { base, data };
181
-
182
- instructionsToRun.forEach(instructionKeyOrObj => {
183
- if (showLog) console.log("instruction : ", instructionKeyOrObj);
184
-
185
- const instruction = typeof instructionKeyOrObj === "string"
186
- ? instructions[instructionKeyOrObj]
187
- : instructionKeyOrObj;
188
-
189
- if (!instruction) {
190
- if (showLog) console.warn(`Instruction not found: ${instructionKeyOrObj}`);
191
- return;
192
- }
193
-
194
- executeInstruction({ instruction, context, spec, showLog });
195
- });
196
-
197
- runOptions({ base: context?.base, options, optionDefs, showLog });
198
-
199
- return context?.base;
200
- };
201
-
202
- export default startFunc;
@@ -1,52 +0,0 @@
1
- import { instructions as defaultInstructions, spec as defaultSpec } from "spec-repo";
2
- import runSingleInstructions from "json-instruction-single";
3
- import instructionsWithTargets from "./instructionsWithTargets/index.js";
4
-
5
- const isObject = value => value !== null && typeof value === "object" && !Array.isArray(value);
6
-
7
- const resolve = (reference, library) => {
8
- if (Array.isArray(reference)) return reference.flatMap(item => resolve(item, library));
9
- if (typeof reference === "string") return resolve(library[reference], library);
10
- return [];
11
- };
12
-
13
- const execute = (reference, context, library, showLog) => {
14
- const resolved = resolve(reference, library);
15
- const ready = instructionsWithTargets(resolved, context.base);
16
-
17
- if (showLog) console.log("[runOptions]", { resolved: resolved.length, ready: ready.length });
18
- if (ready.length) runSingleInstructions(ready, context, showLog);
19
- };
20
-
21
- /** Apply the enabled option switches defined by spec-repo. */
22
- const runOptions = ({
23
- base,
24
- options = {},
25
- optionDefs = {},
26
- instructions = defaultInstructions,
27
- showLog = false
28
- } = {}) => {
29
- if (!base || !isObject(options)) return base;
30
-
31
- const context = { base, spec: structuredClone(defaultSpec) };
32
-
33
- for (const [feature, switches] of Object.entries(optionDefs)) {
34
- const selected = options[feature];
35
- if (!isObject(selected) || !isObject(switches)) continue;
36
-
37
- const enabled = Object.entries(selected)
38
- .filter(([name, value]) => value === true && name !== "initialize" && switches[name]);
39
-
40
- if (!enabled.length) continue;
41
-
42
- if (switches.initialize) execute(switches.initialize, context, instructions, showLog);
43
- for (const [name] of enabled) {
44
- execute(switches[name], context, instructions, showLog);
45
- }
46
- }
47
-
48
- return base;
49
- };
50
-
51
- export { runOptions };
52
- export default runOptions;
@@ -1,31 +0,0 @@
1
- import { query } from "jsonpathly";
2
-
3
- const hasMatch = (base, expression) => {
4
- if (typeof expression !== "string" || !expression.trim()) return false;
5
- return query(base, expression, { returnArray: true }).length > 0;
6
- };
7
-
8
- const matchesSkipCondition = (base, skipIf) => {
9
- if (!skipIf) return false;
10
- const expressions = Array.isArray(skipIf) ? skipIf : [skipIf];
11
- return expressions.some(expression => hasMatch(base, expression));
12
- };
13
-
14
- const instructionsWithTargets = (instructions = [], base) => {
15
- const list = Array.isArray(instructions) ? instructions : [];
16
-
17
- return list.filter(instruction => {
18
- const destinationPath = instruction?.destinationPath;
19
- const root = destinationPath?.fileName
20
- ? (destinationPath.fileName === "base" ? base : undefined)
21
- : base;
22
-
23
- if (!destinationPath?.jsonPath) return false;
24
- if (!hasMatch(root, destinationPath.jsonPath)) return false;
25
- if (matchesSkipCondition(base, instruction.skipIf)) return false;
26
-
27
- return true;
28
- });
29
- };
30
-
31
- export default instructionsWithTargets;