json-renderers-run-instructions 1.11.2 → 1.14.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.11.2",
3
+ "version": "1.14.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",
@@ -12,10 +12,11 @@
12
12
  "README.md"
13
13
  ],
14
14
  "scripts": {
15
- "test": "node --test",
15
+ "test": "node --test test/*.js test/*.mjs",
16
16
  "build": "npx @keshavsoft/build-cli"
17
17
  },
18
18
  "dependencies": {
19
+ "json-renderers-run-instructions-steps": "^1.3.1",
19
20
  "json-renderers-run-options": "^1.2.1",
20
21
  "json-transformer-funcs-one-dimension": "^1.9.9",
21
22
  "json-transformer-funcs-primitive": "^1.9.3",
package/src/index.js CHANGED
@@ -1,3 +1,3 @@
1
- export * from "./v11/index.js";
1
+ export * from "./v14/index.js";
2
2
 
3
- export { default } from "./v11/index.js";
3
+ export { default } from "./v14/index.js";
@@ -0,0 +1,178 @@
1
+ import writePath from "json-write-path";
2
+ import { query } from "jsonpathly";
3
+ import { spec, instructions, definitions } from "spec-repo";
4
+
5
+ import { matrixTransformers, singleTransformers } from "./transformers/index.js";
6
+ import runOptions from "./runOptions/index.js";
7
+ import registerGlobal from "./registerGlobal/index.js";
8
+
9
+ const resolveSource = (sourcePath, context) => {
10
+ if (!sourcePath) return null;
11
+
12
+ const root = context[sourcePath.fileName];
13
+ if (root === undefined) {
14
+ throw new Error(`Context entry not found: ${sourcePath.fileName}`);
15
+ }
16
+
17
+ if (sourcePath.jsonPath) {
18
+ const matches = query(root, sourcePath.jsonPath, { returnArray: true });
19
+ if (matches.length === 0) {
20
+ if (Array.isArray(root)) return root;
21
+ return [];
22
+ }
23
+ return matches.length === 1 ? matches[0] : matches;
24
+ }
25
+
26
+ if (Array.isArray(sourcePath.filePath) && sourcePath.filePath.length > 0) {
27
+ return sourcePath.filePath.reduce((curr, key) => (curr ? curr[key] : undefined), root);
28
+ }
29
+
30
+ return root;
31
+ };
32
+
33
+ const checkDestinationExists = (destinationPath, context) => {
34
+ if (!destinationPath) return true;
35
+ const root = context[destinationPath.fileName];
36
+ if (root === undefined) return false;
37
+
38
+ if (destinationPath.jsonPath) {
39
+ const matches = query(root, destinationPath.jsonPath, { returnArray: true });
40
+ return matches.length > 0;
41
+ }
42
+
43
+ return true;
44
+ };
45
+
46
+ const writeResult = (context, destinationPath, result, showLog = false) => {
47
+ if (!destinationPath) return;
48
+
49
+ if (!destinationPath.filePath || destinationPath.filePath.length === 0) {
50
+ const target = context[destinationPath.fileName];
51
+ if (Array.isArray(target)) {
52
+ target.push(...(Array.isArray(result) ? result : [result]));
53
+ } else {
54
+ context[destinationPath.fileName] = result;
55
+ }
56
+ return;
57
+ }
58
+
59
+ writePath(context, destinationPath, result, showLog);
60
+ };
61
+
62
+ const transformMatrix = ({ instruction, context, spec }) => {
63
+ const fn = matrixTransformers[instruction.type] ?? matrixTransformers.rowOnColumn;
64
+ const columnSpec = spec[instruction?.templateKey?.columnKey];
65
+ const rowSpec = spec[instruction?.templateKey?.rowKey];
66
+
67
+ const columns = context.data?.columns ?? context.columns ?? [];
68
+ const rows = context.data?.data ?? (Array.isArray(context.data) ? context.data : context.rows ?? []);
69
+
70
+ return fn({ columns, data: rows, rows, columnSpec, rowSpec });
71
+ };
72
+
73
+ const transformSingle = ({ instruction, context, spec }) => {
74
+ const template = instruction.templateKey ? spec[instruction.templateKey] : null;
75
+ const source = resolveSource(instruction.sourcePath, context);
76
+
77
+ if (instruction.func === null || instruction.func === undefined) {
78
+ return structuredClone(template);
79
+ }
80
+
81
+ const fn = singleTransformers[instruction.func];
82
+ if (!fn) {
83
+ throw new Error(`Unknown function: ${instruction.func}`);
84
+ }
85
+
86
+ return fn(source, template);
87
+ };
88
+
89
+ const executeInstruction = ({ instruction, context, spec, showLog = false }) => {
90
+ if (!instruction) return null;
91
+
92
+ if (Array.isArray(instruction)) {
93
+ return instruction.map(step => executeInstruction({ instruction: step, context, spec, showLog }));
94
+ }
95
+
96
+ // Auto-normalize legacy dataRole to jsonPath
97
+ if (instruction.destinationPath?.dataRole && !instruction.destinationPath?.jsonPath) {
98
+ instruction.destinationPath.jsonPath = `$..*[?@.attributes['data-role'] == '${instruction.destinationPath.dataRole}']`;
99
+ }
100
+
101
+ // Skip if instruction has skipIf condition and target already exists
102
+ if (instruction.skipIf && query(context.base, instruction.skipIf, { returnArray: true }).length > 0) {
103
+ return null;
104
+ }
105
+
106
+ // If destination node does not exist in current base, safely skip
107
+ if (!checkDestinationExists(instruction.destinationPath, context)) {
108
+ if (showLog) {
109
+ console.warn(`Destination node not found for ${instruction.destinationPath?.jsonPath}, skipping instruction.`);
110
+ }
111
+ return null;
112
+ }
113
+
114
+ const isMatrix = instruction.type === "rowOnColumn" ||
115
+ instruction.type === "columnOnRow" ||
116
+ Boolean(instruction.templateKey?.columnKey);
117
+
118
+ const result = isMatrix
119
+ ? transformMatrix({ instruction, context, spec })
120
+ : transformSingle({ instruction, context, spec });
121
+
122
+ writeResult(context, instruction.destinationPath, result, showLog);
123
+
124
+ return result;
125
+ };
126
+
127
+ const startFunc = ({
128
+ instructionMainKey,
129
+ instructionSubKey,
130
+ data,
131
+ options,
132
+ showLog = false
133
+ } = {}) => {
134
+ const definition = definitions[instructionMainKey]?.[instructionSubKey];
135
+ if (!definition) {
136
+ throw new Error(`Definition not found: ${instructionMainKey}.${instructionSubKey}`);
137
+ }
138
+
139
+ const baseKey = definition.baseFromSpec;
140
+ const instructionsToRun = definition.instructions ?? [];
141
+ const optionDefs = (definition.options && typeof definition.options === "object")
142
+ ? definition.options
143
+ : (definitions[instructionMainKey]?.options ?? instructions.options?.[definition.options] ?? {});
144
+
145
+ const base = structuredClone(spec[baseKey]);
146
+ const context = { base, data };
147
+
148
+ instructionsToRun.forEach(instructionKeyOrObj => {
149
+ if (showLog) console.log("instruction : ", instructionKeyOrObj);
150
+
151
+ const instruction = typeof instructionKeyOrObj === "string"
152
+ ? instructions[instructionKeyOrObj]
153
+ : instructionKeyOrObj;
154
+
155
+ if (!instruction) {
156
+ if (showLog) console.warn(`Instruction not found: ${instructionKeyOrObj}`);
157
+ return;
158
+ }
159
+
160
+ executeInstruction({ instruction, context, spec, showLog });
161
+ });
162
+
163
+ runOptions({
164
+ base: context?.base,
165
+ options,
166
+ optionDefs,
167
+ instructions,
168
+ spec,
169
+ executeInstruction: ({ instruction }) => executeInstruction({ instruction, context, spec, showLog }),
170
+ showLog
171
+ });
172
+
173
+ return context?.base;
174
+ };
175
+
176
+ export default startFunc;
177
+
178
+ registerGlobal(startFunc);
@@ -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;
@@ -0,0 +1,6 @@
1
+ export const meta = {
2
+ name: "json-renderers-run-instructions",
3
+ version: "1.12.0"
4
+ };
5
+
6
+ export default meta;
@@ -0,0 +1,66 @@
1
+ const isObject = value => value !== null && typeof value === "object" && !Array.isArray(value);
2
+
3
+ const getFeatureOptions = (options, featureKey) => {
4
+ if (!isObject(options)) return null;
5
+ if (options[featureKey] !== undefined) return options[featureKey];
6
+ const prefixed = `show${featureKey.charAt(0).toUpperCase()}${featureKey.slice(1)}`;
7
+ if (options[prefixed] !== undefined) return options[prefixed];
8
+ return null;
9
+ };
10
+
11
+ /**
12
+ * 2-level loop for applying option switches from definitions.json:
13
+ * Level 1: Loop over feature groups (search, serial, options)
14
+ * Level 2: Loop over switches inside the feature (showLabel, showHelper, showInput...)
15
+ */
16
+ const runOptions = ({
17
+ base,
18
+ options = {},
19
+ optionDefs = {},
20
+ instructions = {},
21
+ spec = {},
22
+ executeInstruction,
23
+ showLog = false
24
+ } = {}) => {
25
+ if (!base || !isObject(options) || !isObject(optionDefs) || typeof executeInstruction !== "function") {
26
+ return base;
27
+ }
28
+
29
+ for (const [featureKey, featureSwitches] of Object.entries(optionDefs)) {
30
+ if (!isObject(featureSwitches)) continue;
31
+
32
+ const userFeature = getFeatureOptions(options, featureKey);
33
+ if (!userFeature) continue;
34
+
35
+ const isBool = typeof userFeature === "boolean" && userFeature === true;
36
+ const isObj = isObject(userFeature);
37
+ if (!isBool && !isObj) continue;
38
+
39
+ const hasActiveSwitch = Object.entries(featureSwitches).some(
40
+ ([switchKey]) => switchKey !== "initialize" && (isBool || userFeature[switchKey] === true)
41
+ );
42
+ if (!hasActiveSwitch) continue;
43
+
44
+ if (featureSwitches.initialize && instructions[featureSwitches.initialize]) {
45
+ executeInstruction({
46
+ instruction: instructions[featureSwitches.initialize]
47
+ });
48
+ }
49
+
50
+ for (const [switchKey, instructionKey] of Object.entries(featureSwitches)) {
51
+ if (switchKey === "initialize") continue;
52
+
53
+ const isEnabled = isBool || userFeature[switchKey] === true;
54
+ if (isEnabled && instructions[instructionKey]) {
55
+ executeInstruction({
56
+ instruction: instructions[instructionKey]
57
+ });
58
+ }
59
+ }
60
+ }
61
+
62
+ return base;
63
+ };
64
+
65
+ export { runOptions };
66
+ export default runOptions;
@@ -0,0 +1,33 @@
1
+ import applyTemplate from "json-transformer-funcs-primitive";
2
+
3
+ /**
4
+ * Pure transformer: maps columns onto rows (e.g. for selects with options or datalists).
5
+ * Symmetrical counterpart to rowOnColumn.
6
+ */
7
+ const columnOnRow = ({
8
+ columns = [],
9
+ rows = [],
10
+ columnSpec,
11
+ rowSpec
12
+ } = {}) => {
13
+ return columns.map(column => {
14
+ const container = applyTemplate(column, columnSpec);
15
+
16
+ let optionsList = [];
17
+ if (Array.isArray(column.options) && column.options.length > 0) {
18
+ optionsList = column.options.map(opt =>
19
+ typeof opt === "object" ? opt : { rowValue: opt }
20
+ );
21
+ } else if (Array.isArray(rows) && rows.length > 0) {
22
+ optionsList = rows.map(r => ({
23
+ ...r,
24
+ rowValue: r[column.columnName] ?? r.value ?? r
25
+ }));
26
+ }
27
+
28
+ container.children = optionsList.map(item => applyTemplate(item, rowSpec));
29
+ return container;
30
+ });
31
+ };
32
+
33
+ export default columnOnRow;
@@ -0,0 +1,21 @@
1
+ import fromCollection from "json-transformer-funcs-one-dimension";
2
+ import applyTemplate from "json-transformer-funcs-primitive";
3
+ import rowOnColumn from "json-transformer-funcs-row-on-column";
4
+ import columnOnRow from "./columnOnRow.js";
5
+
6
+ export const matrixTransformers = {
7
+ rowOnColumn,
8
+ columnOnRow
9
+ };
10
+
11
+ export const singleTransformers = {
12
+ fromCollection,
13
+ applyTemplate
14
+ };
15
+
16
+ export {
17
+ fromCollection,
18
+ applyTemplate,
19
+ rowOnColumn,
20
+ columnOnRow
21
+ };
@@ -0,0 +1,49 @@
1
+ import { spec, instructions, definitions } from "spec-repo";
2
+
3
+ import executeInstruction from "./types/index.js";
4
+ import runOptions from "./runOptions/index.js";
5
+ import registerGlobal from "./registerGlobal/index.js";
6
+
7
+ const startFunc = ({
8
+ instructionMainKey,
9
+ instructionSubKey,
10
+ data,
11
+ options,
12
+ showLog = false
13
+ } = {}) => {
14
+ const definition = definitions[instructionMainKey]?.[instructionSubKey];
15
+ if (!definition) {
16
+ throw new Error(`Definition not found: ${instructionMainKey}.${instructionSubKey}`);
17
+ }
18
+
19
+ const base = structuredClone(spec[definition.baseFromSpec]);
20
+ const context = { base, data };
21
+
22
+ const instructionsToRun = definition.instructions ?? [];
23
+ instructionsToRun.forEach(keyOrObj => {
24
+ const instruction = typeof keyOrObj === "string" ? instructions[keyOrObj] : keyOrObj;
25
+ if (instruction) {
26
+ executeInstruction({ instruction, context, spec, showLog });
27
+ }
28
+ });
29
+
30
+ const optionDefs = (definition.options && typeof definition.options === "object")
31
+ ? definition.options
32
+ : (definitions[instructionMainKey]?.options ?? instructions.options?.[definition.options] ?? {});
33
+
34
+ runOptions({
35
+ base: context.base,
36
+ options,
37
+ optionDefs,
38
+ instructions,
39
+ spec,
40
+ executeInstruction: ({ instruction }) => executeInstruction({ instruction, context, spec, showLog }),
41
+ showLog
42
+ });
43
+
44
+ return context.base;
45
+ };
46
+
47
+ export default startFunc;
48
+
49
+ registerGlobal(startFunc);
@@ -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;
@@ -0,0 +1,6 @@
1
+ export const meta = {
2
+ name: "json-renderers-run-instructions",
3
+ version: "1.13.1"
4
+ };
5
+
6
+ export default meta;
@@ -0,0 +1,66 @@
1
+ const isObject = value => value !== null && typeof value === "object" && !Array.isArray(value);
2
+
3
+ const getFeatureOptions = (options, featureKey) => {
4
+ if (!isObject(options)) return null;
5
+ if (options[featureKey] !== undefined) return options[featureKey];
6
+ const prefixed = `show${featureKey.charAt(0).toUpperCase()}${featureKey.slice(1)}`;
7
+ if (options[prefixed] !== undefined) return options[prefixed];
8
+ return null;
9
+ };
10
+
11
+ /**
12
+ * 2-level loop for applying option switches from definitions.json:
13
+ * Level 1: Loop over feature groups (search, serial, options)
14
+ * Level 2: Loop over switches inside the feature (showLabel, showHelper, showInput...)
15
+ */
16
+ const runOptions = ({
17
+ base,
18
+ options = {},
19
+ optionDefs = {},
20
+ instructions = {},
21
+ spec = {},
22
+ executeInstruction,
23
+ showLog = false
24
+ } = {}) => {
25
+ if (!base || !isObject(options) || !isObject(optionDefs) || typeof executeInstruction !== "function") {
26
+ return base;
27
+ }
28
+
29
+ for (const [featureKey, featureSwitches] of Object.entries(optionDefs)) {
30
+ if (!isObject(featureSwitches)) continue;
31
+
32
+ const userFeature = getFeatureOptions(options, featureKey);
33
+ if (!userFeature) continue;
34
+
35
+ const isBool = typeof userFeature === "boolean" && userFeature === true;
36
+ const isObj = isObject(userFeature);
37
+ if (!isBool && !isObj) continue;
38
+
39
+ const hasActiveSwitch = Object.entries(featureSwitches).some(
40
+ ([switchKey]) => switchKey !== "initialize" && (isBool || userFeature[switchKey] === true)
41
+ );
42
+ if (!hasActiveSwitch) continue;
43
+
44
+ if (featureSwitches.initialize && instructions[featureSwitches.initialize]) {
45
+ executeInstruction({
46
+ instruction: instructions[featureSwitches.initialize]
47
+ });
48
+ }
49
+
50
+ for (const [switchKey, instructionKey] of Object.entries(featureSwitches)) {
51
+ if (switchKey === "initialize") continue;
52
+
53
+ const isEnabled = isBool || userFeature[switchKey] === true;
54
+ if (isEnabled && instructions[instructionKey]) {
55
+ executeInstruction({
56
+ instruction: instructions[instructionKey]
57
+ });
58
+ }
59
+ }
60
+ }
61
+
62
+ return base;
63
+ };
64
+
65
+ export { runOptions };
66
+ export default runOptions;
@@ -0,0 +1,33 @@
1
+ import applyTemplate from "json-transformer-funcs-primitive";
2
+
3
+ /**
4
+ * Pure transformer: maps columns onto rows (e.g. for selects with options or datalists).
5
+ * Symmetrical counterpart to rowOnColumn.
6
+ */
7
+ const columnOnRow = ({
8
+ columns = [],
9
+ rows = [],
10
+ columnSpec,
11
+ rowSpec
12
+ } = {}) => {
13
+ return columns.map(column => {
14
+ const container = applyTemplate(column, columnSpec);
15
+
16
+ let optionsList = [];
17
+ if (Array.isArray(column.options) && column.options.length > 0) {
18
+ optionsList = column.options.map(opt =>
19
+ typeof opt === "object" ? opt : { rowValue: opt }
20
+ );
21
+ } else if (Array.isArray(rows) && rows.length > 0) {
22
+ optionsList = rows.map(r => ({
23
+ ...r,
24
+ rowValue: r[column.columnName] ?? r.value ?? r
25
+ }));
26
+ }
27
+
28
+ container.children = optionsList.map(item => applyTemplate(item, rowSpec));
29
+ return container;
30
+ });
31
+ };
32
+
33
+ export default columnOnRow;
@@ -0,0 +1,21 @@
1
+ import fromCollection from "json-transformer-funcs-one-dimension";
2
+ import applyTemplate from "json-transformer-funcs-primitive";
3
+ import rowOnColumn from "json-transformer-funcs-row-on-column";
4
+ import columnOnRow from "./columnOnRow.js";
5
+
6
+ export const matrixTransformers = {
7
+ rowOnColumn,
8
+ columnOnRow
9
+ };
10
+
11
+ export const singleTransformers = {
12
+ fromCollection,
13
+ applyTemplate
14
+ };
15
+
16
+ export {
17
+ fromCollection,
18
+ applyTemplate,
19
+ rowOnColumn,
20
+ columnOnRow
21
+ };
@@ -0,0 +1,34 @@
1
+ import applyTemplate from "json-transformer-funcs-primitive";
2
+ import writeResult from "./helpers/writeResult.js";
3
+
4
+ const runColumnOnRow = ({ instruction, context, spec, showLog = false }) => {
5
+ const columnSpec = spec[instruction?.templateKey?.columnKey];
6
+ const rowSpec = spec[instruction?.templateKey?.rowKey];
7
+
8
+ const columns = context.data?.columns ?? context.columns ?? [];
9
+ const rows = context.data?.data ?? (Array.isArray(context.data) ? context.data : context.rows ?? []);
10
+
11
+ const result = columns.map(column => {
12
+ const container = applyTemplate(column, columnSpec);
13
+
14
+ let optionsList = [];
15
+ if (Array.isArray(column.options) && column.options.length > 0) {
16
+ optionsList = column.options.map(opt =>
17
+ typeof opt === "object" ? opt : { rowValue: opt }
18
+ );
19
+ } else if (Array.isArray(rows) && rows.length > 0) {
20
+ optionsList = rows.map(r => ({
21
+ ...r,
22
+ rowValue: r[column.columnName] ?? r.value ?? r
23
+ }));
24
+ }
25
+
26
+ container.children = optionsList.map(item => applyTemplate(item, rowSpec));
27
+ return container;
28
+ });
29
+
30
+ writeResult(context, instruction.destinationPath, result, showLog);
31
+ return result;
32
+ };
33
+
34
+ export default runColumnOnRow;
@@ -0,0 +1,38 @@
1
+ import { query } from "jsonpathly";
2
+
3
+ const checkDestinationExists = (destinationPath, context) => {
4
+ if (!destinationPath) return true;
5
+ const root = context[destinationPath.fileName];
6
+ if (root === undefined) return false;
7
+
8
+ if (destinationPath.jsonPath) {
9
+ const matches = query(root, destinationPath.jsonPath, { returnArray: true });
10
+ return matches.length > 0;
11
+ }
12
+
13
+ return true;
14
+ };
15
+
16
+ export const shouldSkip = (instruction, context, showLog = false) => {
17
+ // Auto-normalize legacy dataRole to jsonPath
18
+ if (instruction.destinationPath?.dataRole && !instruction.destinationPath?.jsonPath) {
19
+ instruction.destinationPath.jsonPath = `$..*[?@.attributes['data-role'] == '${instruction.destinationPath.dataRole}']`;
20
+ }
21
+
22
+ // Skip if instruction has skipIf condition and target already exists
23
+ if (instruction.skipIf && query(context.base, instruction.skipIf, { returnArray: true }).length > 0) {
24
+ return true;
25
+ }
26
+
27
+ // If destination node does not exist in current base, safely skip
28
+ if (!checkDestinationExists(instruction.destinationPath, context)) {
29
+ if (showLog) {
30
+ console.warn(`Destination node not found for ${instruction.destinationPath?.jsonPath}, skipping instruction.`);
31
+ }
32
+ return true;
33
+ }
34
+
35
+ return false;
36
+ };
37
+
38
+ export default shouldSkip;
@@ -0,0 +1,27 @@
1
+ import { query } from "jsonpathly";
2
+
3
+ const resolveSource = (sourcePath, context) => {
4
+ if (!sourcePath) return null;
5
+
6
+ const root = context[sourcePath.fileName];
7
+ if (root === undefined) {
8
+ throw new Error(`Context entry not found: ${sourcePath.fileName}`);
9
+ }
10
+
11
+ if (sourcePath.jsonPath) {
12
+ const matches = query(root, sourcePath.jsonPath, { returnArray: true });
13
+ if (matches.length === 0) {
14
+ if (Array.isArray(root)) return root;
15
+ return [];
16
+ }
17
+ return matches.length === 1 ? matches[0] : matches;
18
+ }
19
+
20
+ if (Array.isArray(sourcePath.filePath) && sourcePath.filePath.length > 0) {
21
+ return sourcePath.filePath.reduce((curr, key) => (curr ? curr[key] : undefined), root);
22
+ }
23
+
24
+ return root;
25
+ };
26
+
27
+ export default resolveSource;
@@ -0,0 +1,19 @@
1
+ import writePath from "json-write-path";
2
+
3
+ const writeResult = (context, destinationPath, result, showLog = false) => {
4
+ if (!destinationPath) return;
5
+
6
+ if (!destinationPath.filePath || destinationPath.filePath.length === 0) {
7
+ const target = context[destinationPath.fileName];
8
+ if (Array.isArray(target)) {
9
+ target.push(...(Array.isArray(result) ? result : [result]));
10
+ } else {
11
+ context[destinationPath.fileName] = result;
12
+ }
13
+ return;
14
+ }
15
+
16
+ writePath(context, destinationPath, result, showLog);
17
+ };
18
+
19
+ export default writeResult;
@@ -0,0 +1,33 @@
1
+ import single from "./single.js";
2
+ import rowOnColumn from "./rowOnColumn.js";
3
+ import columnOnRow from "./columnOnRow.js";
4
+ import { shouldSkip } from "./helpers/guards.js";
5
+
6
+ const typeMap = {
7
+ single,
8
+ rowOnColumn,
9
+ columnOnRow
10
+ };
11
+
12
+ const executeInstruction = ({ instruction, context, spec, showLog = false }) => {
13
+ if (!instruction) return null;
14
+
15
+ if (Array.isArray(instruction)) {
16
+ return instruction.map(step => executeInstruction({ instruction: step, context, spec, showLog }));
17
+ }
18
+
19
+ if (shouldSkip(instruction, context, showLog)) {
20
+ return null;
21
+ }
22
+
23
+ const type = instruction.type ?? (instruction.templateKey?.columnKey ? "rowOnColumn" : "single");
24
+ const executor = typeMap[type];
25
+
26
+ if (!executor) {
27
+ throw new Error(`Unknown instruction type: ${type}`);
28
+ }
29
+
30
+ return executor({ instruction, context, spec, showLog });
31
+ };
32
+
33
+ export default executeInstruction;
@@ -0,0 +1,22 @@
1
+ import rowOnColumnFunc from "json-transformer-funcs-row-on-column";
2
+ import writeResult from "./helpers/writeResult.js";
3
+
4
+ const runRowOnColumn = ({ instruction, context, spec, showLog = false }) => {
5
+ const columnSpec = spec[instruction?.templateKey?.columnKey];
6
+ const rowSpec = spec[instruction?.templateKey?.rowKey];
7
+
8
+ const columns = context.data?.columns ?? context.columns ?? [];
9
+ const rows = context.data?.data ?? (Array.isArray(context.data) ? context.data : context.rows ?? []);
10
+
11
+ const result = rowOnColumnFunc({
12
+ columns,
13
+ data: rows,
14
+ columnSpec,
15
+ rowSpec
16
+ });
17
+
18
+ writeResult(context, instruction.destinationPath, result, showLog);
19
+ return result;
20
+ };
21
+
22
+ export default runRowOnColumn;
@@ -0,0 +1,28 @@
1
+ import applyTemplate from "json-transformer-funcs-primitive";
2
+ import fromCollection from "json-transformer-funcs-one-dimension";
3
+ import resolveSource from "./helpers/resolveSource.js";
4
+ import writeResult from "./helpers/writeResult.js";
5
+
6
+ const transformers = {
7
+ applyTemplate,
8
+ fromCollection
9
+ };
10
+
11
+ const runSingle = ({ instruction, context, spec, showLog = false }) => {
12
+ const template = instruction.templateKey ? spec[instruction.templateKey] : null;
13
+ const source = resolveSource(instruction.sourcePath, context);
14
+
15
+ let result;
16
+ if (instruction.func === null || instruction.func === undefined) {
17
+ result = structuredClone(template);
18
+ } else {
19
+ const fn = transformers[instruction.func];
20
+ if (!fn) throw new Error(`Unknown function: ${instruction.func}`);
21
+ result = fn(source, template);
22
+ }
23
+
24
+ writeResult(context, instruction.destinationPath, result, showLog);
25
+ return result;
26
+ };
27
+
28
+ export default runSingle;
@@ -0,0 +1,48 @@
1
+ import { spec, definitions } from "spec-repo";
2
+ import runInstructions from "json-renderers-run-instructions-steps";
3
+
4
+ import runOptions from "./runOptions/index.js";
5
+ import registerGlobal from "./registerGlobal/index.js";
6
+
7
+ const startFunc = ({
8
+ instructionMainKey,
9
+ instructionSubKey,
10
+ data,
11
+ options,
12
+ showLog = false
13
+ } = {}) => {
14
+ const definition = definitions[instructionMainKey]?.[instructionSubKey];
15
+ if (!definition) {
16
+ throw new Error(`Definition not found: ${instructionMainKey}.${instructionSubKey}`);
17
+ }
18
+
19
+ // The Canvas
20
+ const base = structuredClone(spec[definition.baseFromSpec]);
21
+
22
+ // Task 1: Execute blueprint instructions (Array-based, from dedicated steps package)
23
+ runInstructions({
24
+ instructions: definition.instructions ?? [],
25
+ base,
26
+ data,
27
+ showLog
28
+ });
29
+
30
+ // Task 2: Apply feature options (Key-Value switch-based)
31
+ const optionDefs = (definition.options && typeof definition.options === "object")
32
+ ? definition.options
33
+ : (definitions[instructionMainKey]?.options ?? {});
34
+
35
+ runOptions({
36
+ options,
37
+ optionDefs,
38
+ base,
39
+ data,
40
+ showLog
41
+ });
42
+
43
+ return base;
44
+ };
45
+
46
+ export default startFunc;
47
+
48
+ registerGlobal(startFunc);
@@ -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;
@@ -0,0 +1,6 @@
1
+ export const meta = {
2
+ name: "json-renderers-run-instructions",
3
+ version: "1.14.1"
4
+ };
5
+
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;