json-renderers-run-instructions 1.14.1 → 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 (52) hide show
  1. package/package.json +1 -5
  2. package/src/index.js +3 -3
  3. package/src/v12/index.js +178 -178
  4. package/src/v12/registerGlobal/index.js +16 -16
  5. package/src/v12/registerGlobal/meta.js +6 -6
  6. package/src/v12/runOptions/index.js +66 -66
  7. package/src/v12/transformers/columnOnRow.js +33 -33
  8. package/src/v12/transformers/index.js +21 -21
  9. package/src/v13/index.js +49 -49
  10. package/src/v13/registerGlobal/index.js +16 -16
  11. package/src/v13/registerGlobal/meta.js +6 -6
  12. package/src/v13/runOptions/index.js +66 -66
  13. package/src/v13/transformers/columnOnRow.js +33 -33
  14. package/src/v13/transformers/index.js +21 -21
  15. package/src/v13/types/columnOnRow.js +34 -34
  16. package/src/v13/types/helpers/guards.js +38 -38
  17. package/src/v13/types/helpers/resolveSource.js +27 -27
  18. package/src/v13/types/helpers/writeResult.js +19 -19
  19. package/src/v13/types/index.js +33 -33
  20. package/src/v13/types/rowOnColumn.js +22 -22
  21. package/src/v13/types/single.js +28 -28
  22. package/src/v14/index.js +48 -48
  23. package/src/v14/registerGlobal/index.js +16 -16
  24. package/src/v14/registerGlobal/meta.js +5 -5
  25. package/src/v14/runOptions/index.js +77 -77
  26. package/src/v15/index.js +48 -0
  27. package/src/{v10 → v15}/registerGlobal/index.js +1 -1
  28. package/src/{v10 → v15}/registerGlobal/meta.js +1 -1
  29. package/src/{v10 → v15}/runOptions/index.js +77 -70
  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/index.js +0 -219
  39. package/src/v11/registerGlobal/index.js +0 -16
  40. package/src/v11/registerGlobal/meta.js +0 -6
  41. package/src/v11/runOptions/index.js +0 -70
  42. package/src/v11/runOptions/instructionsWithTargets/index.js +0 -31
  43. package/src/v6/index.js +0 -68
  44. package/src/v7/index.js +0 -57
  45. package/src/v8/definitions.json +0 -165
  46. package/src/v8/index.js +0 -193
  47. package/src/v8/instructions.json +0 -275
  48. package/src/v8/spec.json +0 -900
  49. package/src/v9/index.js +0 -195
  50. package/src/v9/runOptions/index.js +0 -48
  51. package/src/v9/runOptions/insertSearch/index.js +0 -28
  52. package/src/v9/runOptions/instructionsWithTargets/index.js +0 -12
@@ -1,21 +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
- };
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
+ };
package/src/v13/index.js CHANGED
@@ -1,49 +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);
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);
@@ -1,16 +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
+ 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
- export const meta = {
2
- name: "json-renderers-run-instructions",
3
- version: "1.13.1"
4
- };
5
-
6
- export default meta;
1
+ export const meta = {
2
+ name: "json-renderers-run-instructions",
3
+ version: "1.13.1"
4
+ };
5
+
6
+ export default meta;
@@ -1,66 +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;
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;
@@ -1,33 +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;
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;
@@ -1,21 +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
- };
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
+ };
@@ -1,34 +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;
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;
@@ -1,38 +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;
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;
@@ -1,27 +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;
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;