json-renderers-run-instructions-steps-single 0.0.0-stage → 1.3.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,43 @@
1
- {
2
- "name": "json-renderers-run-instructions-steps-single",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
1
+ {
2
+ "name": "json-renderers-run-instructions-steps-single",
3
+ "version": "1.3.1",
4
+ "description": "Sequential blueprint instruction step runner for JSON renderers",
5
+ "type": "module",
6
+ "main": "src/index.js",
7
+ "module": "src/index.js",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./src/index.js",
11
+ "default": "./src/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "src",
16
+ "README.md"
17
+ ],
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/keshavsoft/json-renderers-run-instructions-steps-single.git"
21
+ },
22
+ "scripts": {
23
+ "build": "npx @keshavsoft/build-cli"
24
+ },
25
+ "keywords": [
26
+ "json-renderers",
27
+ "instructions",
28
+ "steps",
29
+ "orchestrator",
30
+ "declarative-ui",
31
+ "two-dimensions"
32
+ ],
33
+ "author": "KeshavSoft",
34
+ "license": "MIT",
35
+ "homepage": "https://keshavsoft.github.io/json-renderers-run-instructions-steps-single/",
36
+ "dependencies": {
37
+ "declarative-spec": "^1.4.1",
38
+ "json-transformer-funcs-one-dimension": "^1.10.1",
39
+ "json-transformer-funcs-primitive": "^1.9.3",
40
+ "json-transformer-funcs-two-dimensions": "^1.10.1",
41
+ "jsonpathly": "^4.0.0"
42
+ }
6
43
  }
package/src/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./v3/index.js";
2
+
3
+ export { default } from "./v3/index.js";
@@ -0,0 +1,36 @@
1
+ import spec from "declarative-spec";
2
+ import { query } from "jsonpathly";
3
+
4
+ const staticWriteActions = {
5
+ unshift: (target, template) => target?.unshift(structuredClone(template)),
6
+ push: (target, template) => target?.push(structuredClone(template)),
7
+ unshiftSerialToRows: (target, template) => {
8
+ target?.forEach((row, rowIndex) => {
9
+ const item = structuredClone(template);
10
+ item.textContent = rowIndex + 1;
11
+ row?.children?.unshift(item);
12
+ });
13
+ },
14
+ pushToRows: (target, template) => {
15
+ target?.forEach((row) => {
16
+ row?.children?.push(structuredClone(template));
17
+ });
18
+ }
19
+ };
20
+
21
+ const applyStaticInstruction = (step, state, showLog = false) => {
22
+ const template = spec[step.templateKey];
23
+ if (template === undefined) {
24
+ throw new Error(`Instruction spec not found: ${step.templateKey}`);
25
+ }
26
+
27
+ const target = query(state, step.destinationPath?.jsonPath, { returnArray: true });
28
+ const action = staticWriteActions[step.destinationPath?.action];
29
+ action?.(target[0], template);
30
+
31
+ if (showLog) console.log("template : ", target, template);
32
+ if (showLog) console.log("state : ", state);
33
+ return state;
34
+ };
35
+
36
+ export default applyStaticInstruction;
@@ -0,0 +1,38 @@
1
+ import spec from "declarative-spec";
2
+ import applyTemplate from "json-transformer-funcs-primitive";
3
+ import fromCollection from "json-transformer-funcs-one-dimension";
4
+ import { rowOnColumn, columnOnRow } from "json-transformer-funcs-two-dimensions";
5
+
6
+ const resultBuilders = {
7
+ applyTemplate: (step, source) => {
8
+ const template = spec[step.templateKey];
9
+ return applyTemplate(source, template);
10
+ },
11
+ fromCollection: (step, source) => {
12
+ const template = spec[step.templateKey];
13
+ return fromCollection(source, template);
14
+ },
15
+ rowOnColumn: (step, source) => rowOnColumn({
16
+ columns: source.columns,
17
+ data: source.rows,
18
+ columnSpec: spec[step.templateKey?.columnKey],
19
+ rowSpec: spec[step.templateKey?.rowKey]
20
+ }),
21
+ columnOnRow: (step, source) => columnOnRow({
22
+ columns: source.columns,
23
+ data: source.rows,
24
+ columnSpec: spec[step.templateKey?.columnKey],
25
+ rowSpec: spec[step.templateKey?.rowKey]
26
+ })
27
+ };
28
+
29
+ const buildResult = (step, source) => {
30
+ const build = resultBuilders[step.func];
31
+ if (!build) {
32
+ throw new Error(`Unsupported instruction func: ${step.func}`);
33
+ }
34
+
35
+ return build(step, source);
36
+ };
37
+
38
+ export default buildResult;
@@ -0,0 +1,30 @@
1
+ import applyStaticInstruction from "./applyStaticInstruction.js";
2
+ import buildResult from "./buildResult.js";
3
+ import readSource from "./readSource.js";
4
+ import shouldSkip from "./shouldSkip.js";
5
+ import writeResult from "./writeResult.js";
6
+ import registerGlobal from "./registerGlobal/index.js";
7
+ /**
8
+ * Runs one instruction as a short sequence: skip, read, build, and write.
9
+ */
10
+ const runStep = (step, state, showLog = false) => {
11
+ if (showLog) console.log("start : ", step, state);
12
+ if (shouldSkip(step, state)) {
13
+ return state;
14
+ }
15
+
16
+ if (step.func === null) {
17
+ return applyStaticInstruction(step, state, showLog);
18
+ }
19
+
20
+ const source = readSource(step, state);
21
+ const result = buildResult(step, source);
22
+ writeResult(step, state, result);
23
+
24
+ if (showLog) console.log("state : ", state);
25
+ return state;
26
+ };
27
+
28
+ export default runStep;
29
+
30
+ registerGlobal(runStep);
@@ -0,0 +1,25 @@
1
+ import { query } from "jsonpathly";
2
+
3
+ const sourceReaders = {
4
+ applyTemplate: (step, state) => query(state, step.sourcePath),
5
+ fromCollection: (step, state) => query(state, step.sourcePath),
6
+ rowOnColumn: (step, state) => ({
7
+ columns: query(state, step.sourcePath?.columns),
8
+ rows: query(state, step.sourcePath?.rows)
9
+ }),
10
+ columnOnRow: (step, state) => ({
11
+ columns: query(state, step.sourcePath?.columns),
12
+ rows: query(state, step.sourcePath?.rows)
13
+ })
14
+ };
15
+
16
+ const readSource = (step, state) => {
17
+ const read = sourceReaders[step.func];
18
+ if (!read) {
19
+ throw new Error(`Unsupported instruction func: ${step.func}`);
20
+ }
21
+
22
+ return read(step, state);
23
+ };
24
+
25
+ export default readSource;
@@ -0,0 +1,17 @@
1
+ import meta from "./meta.js";
2
+
3
+ const registerGlobal = (runInstructions) => {
4
+ if (typeof globalThis === "undefined" || !runInstructions) return;
5
+
6
+ globalThis.ks ??= {};
7
+ globalThis.ks.jsonRenderers ??= {};
8
+ globalThis.ks.jsonRenderers.runInstructions ??= {};
9
+ globalThis.ks.jsonRenderers.runInstructions.steps ??= {};
10
+
11
+ globalThis.ks.jsonRenderers.runInstructions.steps.single = {
12
+ meta,
13
+ runInstructions
14
+ };
15
+ };
16
+
17
+ export default registerGlobal;
@@ -0,0 +1,6 @@
1
+ export const meta = {
2
+ name: "json-renderers-run-instructions-steps-single",
3
+ version: "1.2.1"
4
+ };
5
+
6
+ export default meta;
@@ -0,0 +1,7 @@
1
+ import { query } from "jsonpathly";
2
+
3
+ const shouldSkip = (step, state) => {
4
+ return step.skipIf && query(state.base, step.skipIf, { returnArray: true }).length > 0;
5
+ };
6
+
7
+ export default shouldSkip;
@@ -0,0 +1,14 @@
1
+ import { query } from "jsonpathly";
2
+
3
+ const writeResult = (step, state, result) => {
4
+ const target = query(state, step.destinationPath?.jsonPath, { returnArray: true });
5
+
6
+ if (step.func === "applyTemplate") {
7
+ target[0]?.push(result);
8
+ return;
9
+ }
10
+
11
+ target[0]?.push(...result);
12
+ };
13
+
14
+ export default writeResult;
@@ -0,0 +1,36 @@
1
+ import spec from "declarative-spec";
2
+ import { query } from "jsonpathly";
3
+
4
+ const staticWriteActions = {
5
+ unshift: (target, template) => target?.unshift(structuredClone(template)),
6
+ push: (target, template) => target?.push(structuredClone(template)),
7
+ unshiftSerialToRows: (target, template) => {
8
+ target?.forEach((row, rowIndex) => {
9
+ const item = structuredClone(template);
10
+ item.textContent = rowIndex + 1;
11
+ row?.children?.unshift(item);
12
+ });
13
+ },
14
+ pushToRows: (target, template) => {
15
+ target?.forEach((row) => {
16
+ row?.children?.push(structuredClone(template));
17
+ });
18
+ }
19
+ };
20
+
21
+ const applyStaticInstruction = (step, state, showLog = false) => {
22
+ const template = spec[step.templateKey];
23
+ if (template === undefined) {
24
+ throw new Error(`Instruction spec not found: ${step.templateKey}`);
25
+ }
26
+
27
+ const target = query(state, step.destinationPath?.jsonPath, { returnArray: true });
28
+ const action = staticWriteActions[step.destinationPath?.action];
29
+ action?.(target[0], template);
30
+
31
+ if (showLog) console.log("template : ", target, template);
32
+ if (showLog) console.log("state : ", state);
33
+ return state;
34
+ };
35
+
36
+ export default applyStaticInstruction;
@@ -0,0 +1,38 @@
1
+ import spec from "declarative-spec";
2
+ import applyTemplate from "json-transformer-funcs-primitive";
3
+ import fromCollection from "json-transformer-funcs-one-dimension";
4
+ import { rowOnColumn, columnOnRow } from "json-transformer-funcs-two-dimensions";
5
+
6
+ const resultBuilders = {
7
+ applyTemplate: (step, source) => {
8
+ const template = spec[step.templateKey];
9
+ return applyTemplate(source, template);
10
+ },
11
+ fromCollection: (step, source) => {
12
+ const template = spec[step.templateKey];
13
+ return fromCollection(source, template);
14
+ },
15
+ rowOnColumn: (step, source) => rowOnColumn({
16
+ columns: source.columns,
17
+ data: source.rows,
18
+ columnSpec: spec[step.templateKey?.columnKey],
19
+ rowSpec: spec[step.templateKey?.rowKey]
20
+ }),
21
+ columnOnRow: (step, source) => columnOnRow({
22
+ columns: source.columns,
23
+ data: source.rows,
24
+ columnSpec: spec[step.templateKey?.columnKey],
25
+ rowSpec: spec[step.templateKey?.rowKey]
26
+ })
27
+ };
28
+
29
+ const buildResult = (step, source) => {
30
+ const build = resultBuilders[step.func];
31
+ if (!build) {
32
+ throw new Error(`Unsupported instruction func: ${step.func}`);
33
+ }
34
+
35
+ return build(step, source);
36
+ };
37
+
38
+ export default buildResult;
@@ -0,0 +1,32 @@
1
+ import applyStaticInstruction from "./applyStaticInstruction.js";
2
+ import buildResult from "./buildResult.js";
3
+ import readSource from "./readSource.js";
4
+ import shouldSkip from "./shouldSkip.js";
5
+ import writeResult from "./writeResult.js";
6
+ import registerGlobal from "./registerGlobal/index.js";
7
+ /**
8
+ * Runs one instruction as a short sequence: skip, read, build, and write.
9
+ */
10
+ const runStep = (step, state, showLog = false) => {
11
+ if (showLog) console.log("start : ", step, state);
12
+ if (shouldSkip(step, state)) {
13
+ return state;
14
+ }
15
+
16
+ if (step.func === null) {
17
+ return applyStaticInstruction(step, state, showLog);
18
+ }
19
+
20
+ const source = readSource(step, state);
21
+ if (showLog) console.log("source : ", source);
22
+ const result = buildResult(step, source);
23
+ if (showLog) console.log("result : ", result);
24
+ writeResult(step, state, result);
25
+
26
+ if (showLog) console.log("state : ", state);
27
+ return state;
28
+ };
29
+
30
+ export default runStep;
31
+
32
+ registerGlobal(runStep);
@@ -0,0 +1,25 @@
1
+ import { query } from "jsonpathly";
2
+
3
+ const sourceReaders = {
4
+ applyTemplate: (step, state) => query(state, step.sourcePath),
5
+ fromCollection: (step, state) => query(state, step.sourcePath),
6
+ rowOnColumn: (step, state) => ({
7
+ columns: query(state, step.sourcePath?.columns),
8
+ rows: query(state, step.sourcePath?.rows)
9
+ }),
10
+ columnOnRow: (step, state) => ({
11
+ columns: query(state, step.sourcePath?.columns),
12
+ rows: query(state, step.sourcePath?.rows)
13
+ })
14
+ };
15
+
16
+ const readSource = (step, state) => {
17
+ const read = sourceReaders[step.func];
18
+ if (!read) {
19
+ throw new Error(`Unsupported instruction func: ${step.func}`);
20
+ }
21
+
22
+ return read(step, state);
23
+ };
24
+
25
+ export default readSource;
@@ -0,0 +1,17 @@
1
+ import meta from "./meta.js";
2
+
3
+ const registerGlobal = (runInstructions) => {
4
+ if (typeof globalThis === "undefined" || !runInstructions) return;
5
+
6
+ globalThis.ks ??= {};
7
+ globalThis.ks.jsonRenderers ??= {};
8
+ globalThis.ks.jsonRenderers.runInstructions ??= {};
9
+ globalThis.ks.jsonRenderers.runInstructions.steps ??= {};
10
+
11
+ globalThis.ks.jsonRenderers.runInstructions.steps.single = {
12
+ meta,
13
+ runInstructions
14
+ };
15
+ };
16
+
17
+ export default registerGlobal;
@@ -0,0 +1,6 @@
1
+ export const meta = {
2
+ name: "json-renderers-run-instructions-steps-single",
3
+ version: "1.2.1"
4
+ };
5
+
6
+ export default meta;
@@ -0,0 +1,7 @@
1
+ import { query } from "jsonpathly";
2
+
3
+ const shouldSkip = (step, state) => {
4
+ return step.skipIf && query(state.base, step.skipIf, { returnArray: true }).length > 0;
5
+ };
6
+
7
+ export default shouldSkip;
@@ -0,0 +1,14 @@
1
+ import { query } from "jsonpathly";
2
+
3
+ const writeResult = (step, state, result) => {
4
+ const target = query(state, step.destinationPath?.jsonPath, { returnArray: true });
5
+
6
+ if (step.func === "applyTemplate") {
7
+ target[0]?.push(result);
8
+ return;
9
+ }
10
+
11
+ target[0]?.push(...result);
12
+ };
13
+
14
+ export default writeResult;
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.