json-instructions 0.0.0-stage → 1.4.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 (55) hide show
  1. package/README.md +211 -3
  2. package/package.json +26 -5
  3. package/src/guards/index.js +10 -0
  4. package/src/guards/references/func/index.js +8 -0
  5. package/src/guards/references/index.js +9 -0
  6. package/src/guards/references/templateKey/index.js +8 -0
  7. package/src/guards/structure/destinationPath/index.js +8 -0
  8. package/src/guards/structure/func/index.js +8 -0
  9. package/src/guards/structure/index.js +15 -0
  10. package/src/guards/structure/sourcePath/index.js +8 -0
  11. package/src/guards/structure/templateKey/index.js +9 -0
  12. package/src/index.js +2 -0
  13. package/src/singleInstruction/index.js +28 -0
  14. package/src/v1/actions/index.js +25 -0
  15. package/src/v1/actions/insertOptions/index.js +47 -0
  16. package/src/v1/actions/insertOptions/instructions/footer.json +22 -0
  17. package/src/v1/actions/insertOptions/instructions/single.json +62 -0
  18. package/src/v1/actions/insertSearch/index.js +7 -0
  19. package/src/v1/actions/insertSearch/instructions/template.json +20 -0
  20. package/src/v1/actions/insertSerial/index.js +47 -0
  21. package/src/v1/actions/insertSerial/instructions/footer.json +22 -0
  22. package/src/v1/actions/insertSerial/instructions/single.json +62 -0
  23. package/src/v1/index.js +21 -0
  24. package/src/v2/index.js +25 -0
  25. package/src/v2/insertOptions/index.js +47 -0
  26. package/src/v2/insertOptions/instructions/footer.json +22 -0
  27. package/src/v2/insertOptions/instructions/single.json +62 -0
  28. package/src/v2/insertSearch/index.js +7 -0
  29. package/src/v2/insertSearch/instructions/template.json +20 -0
  30. package/src/v2/insertSerial/index.js +47 -0
  31. package/src/v2/insertSerial/instructions/footer.json +22 -0
  32. package/src/v2/insertSerial/instructions/single.json +62 -0
  33. package/src/v3/index.js +25 -0
  34. package/src/v3/insertOptions/index.js +47 -0
  35. package/src/v3/insertOptions/instructions/footer.json +22 -0
  36. package/src/v3/insertOptions/instructions/single.json +62 -0
  37. package/src/v3/insertSearch/index.js +7 -0
  38. package/src/v3/insertSearch/instructions/template.json +20 -0
  39. package/src/v3/insertSerial/index.js +47 -0
  40. package/src/v3/insertSerial/instructions/footer.json +22 -0
  41. package/src/v3/insertSerial/instructions/single.json +62 -0
  42. package/src/v3/spec.json +107 -0
  43. package/src/v4/guards/index.js +10 -0
  44. package/src/v4/guards/references/func/index.js +8 -0
  45. package/src/v4/guards/references/index.js +9 -0
  46. package/src/v4/guards/references/templateKey/index.js +8 -0
  47. package/src/v4/guards/structure/destinationPath/index.js +8 -0
  48. package/src/v4/guards/structure/func/index.js +8 -0
  49. package/src/v4/guards/structure/index.js +15 -0
  50. package/src/v4/guards/structure/sourcePath/index.js +8 -0
  51. package/src/v4/guards/structure/templateKey/index.js +9 -0
  52. package/src/v4/index.js +28 -0
  53. package/src/v4/spec.json +107 -0
  54. package/src/v4/writePath/index.js +62 -0
  55. package/src/writePath/index.js +62 -0
@@ -0,0 +1,62 @@
1
+ [
2
+ {
3
+ "narration": "Prepend the serial column width to the table colgroup.",
4
+ "destinationPath": {
5
+ "fileName": "base",
6
+ "dataRole": "table-colgroup",
7
+ "filePath": [
8
+ "children"
9
+ ],
10
+ "action": "unshift"
11
+ },
12
+ "sourcePath": {
13
+ "fileName": "data",
14
+ "filePath": [
15
+ "serial",
16
+ "col"
17
+ ]
18
+ },
19
+ "templateKey": "buildColGroup",
20
+ "func": "applyTemplate"
21
+ },
22
+ {
23
+ "narration": "Prepend the serial header cell to the table header row.",
24
+ "destinationPath": {
25
+ "fileName": "base",
26
+ "dataRole": "table-header-row",
27
+ "filePath": [
28
+ "children"
29
+ ],
30
+ "action": "unshift"
31
+ },
32
+ "sourcePath": {
33
+ "fileName": "data",
34
+ "filePath": [
35
+ "serial",
36
+ "header"
37
+ ]
38
+ },
39
+ "templateKey": "buildHeader",
40
+ "func": "applyTemplate"
41
+ },
42
+ {
43
+ "narration": "Prepend the serial number cell into each table body row.",
44
+ "destinationPath": {
45
+ "fileName": "base",
46
+ "dataRole": "table-body",
47
+ "filePath": [
48
+ "children"
49
+ ],
50
+ "action": "unshiftSerialToRows"
51
+ },
52
+ "sourcePath": {
53
+ "fileName": "data",
54
+ "filePath": [
55
+ "serial",
56
+ "body"
57
+ ]
58
+ },
59
+ "templateKey": "buildBodyCell",
60
+ "func": "applyTemplate"
61
+ }
62
+ ]
@@ -0,0 +1,107 @@
1
+ {
2
+ "buildBodyTr": {
3
+ "tagName": "tr",
4
+ "attributes": {
5
+ "class": "align-middle"
6
+ },
7
+ "children": []
8
+ },
9
+ "buildColGroup": {
10
+ "tagName": "col",
11
+ "attributes": {
12
+ "width": "${width}"
13
+ }
14
+ },
15
+ "buildHeader": {
16
+ "tagName": "th",
17
+ "textContent": "${columnName}",
18
+ "attributes": {
19
+ "class": "py-3 px-3 fw-semibold text-uppercase small"
20
+ }
21
+ },
22
+ "buildFooter": {
23
+ "tagName": "td",
24
+ "children": [
25
+ {
26
+ "tagName": "input",
27
+ "attributes": {
28
+ "type": "text",
29
+ "placeholder": "enter ${columnName}",
30
+ "name": "${columnName}",
31
+ "id": "${columnName}",
32
+ "list": "${columnName}",
33
+ "class": "form-control"
34
+ }
35
+ }
36
+ ]
37
+ },
38
+ "buildBodyCell": {
39
+ "tagName": "td",
40
+ "textContent": "${rowValue}",
41
+ "attributes": {
42
+ "class": "py-3 px-3 text-secondary"
43
+ }
44
+ },
45
+ "button": {
46
+ "tagName": "button",
47
+ "textContent": "${columnName}",
48
+ "attributes": {
49
+ "class": "btn btn-primary btn-sm"
50
+ }
51
+ },
52
+ "buttonCell": {
53
+ "tagName": "td",
54
+ "attributes": {
55
+ "class": "py-2 px-3 text-center"
56
+ },
57
+ "children": [
58
+ {
59
+ "tagName": "button",
60
+ "textContent": "${agg:count}",
61
+ "attributes": {
62
+ "class": "btn btn-primary btn-sm"
63
+ }
64
+ }
65
+ ]
66
+ },
67
+ "optionsButtonCell": {
68
+ "tagName": "td",
69
+ "attributes": {
70
+ "class": "py-2 px-3 text-center"
71
+ },
72
+ "children": [
73
+ {
74
+ "tagName": "button",
75
+ "textContent": "${columnName}",
76
+ "attributes": {
77
+ "class": "btn btn-primary btn-sm"
78
+ }
79
+ }
80
+ ]
81
+ },
82
+ "buildOption": {
83
+ "tagName": "option",
84
+ "textContent": "${rowValue}",
85
+ "attributes": {
86
+ "value": "${rowValue}"
87
+ }
88
+ },
89
+ "buildSelect": {
90
+ "tagName": "select",
91
+ "attributes": {
92
+ "id": "LedgerName"
93
+ },
94
+ "children": []
95
+ },
96
+ "buildDataList": {
97
+ "tagName": "datalist",
98
+ "attributes": {
99
+ "data-name": "${columnName}"
100
+ },
101
+ "children": []
102
+ },
103
+ "emptyCell": {
104
+ "tagName": "td",
105
+ "children": []
106
+ }
107
+ }
@@ -0,0 +1,10 @@
1
+ import checkStructure from "./structure/index.js";
2
+ import checkReferences from "./references/index.js";
3
+
4
+ const checkInstruction = (instruction, functions, spec, showLog, returnLog) => {
5
+ const structurePassed = checkStructure(instruction, showLog, returnLog);
6
+ if (!structurePassed) return false;
7
+ return checkReferences(instruction, functions, spec, showLog, returnLog);
8
+ };
9
+ export { checkStructure, checkReferences, checkInstruction };
10
+ export default checkInstruction;
@@ -0,0 +1,8 @@
1
+ const checkFunctionReference = (instruction, functions, showLog, returnLog) => {
2
+ const passed = typeof functions[instruction.func] === "function";
3
+ const message = `references/func (${instruction.func}): ${passed ? "passed" : "failed"}`;
4
+ returnLog.push(message);
5
+ if (showLog) console.log(message);
6
+ return passed;
7
+ };
8
+ export default checkFunctionReference;
@@ -0,0 +1,9 @@
1
+ import checkFunctionReference from "./func/index.js";
2
+ import checkSpecReference from "./templateKey/index.js";
3
+
4
+ const checkReferences = (instruction, functions, spec, showLog, returnLog) => {
5
+ const functionPassed = checkFunctionReference(instruction, functions, showLog, returnLog);
6
+ const specPassed = checkSpecReference(instruction, spec, showLog, returnLog);
7
+ return functionPassed && specPassed;
8
+ };
9
+ export default checkReferences;
@@ -0,0 +1,8 @@
1
+ const checkSpecReference = (instruction, spec, showLog, returnLog) => {
2
+ const passed = instruction.templateKey === undefined || Object.hasOwn(spec, instruction.templateKey);
3
+ const message = `references/templateKey${instruction.templateKey === undefined ? " (not used)" : ` (${instruction.templateKey})`}: ${passed ? "passed" : "failed"}`;
4
+ returnLog.push(message);
5
+ if (showLog) console.log(message);
6
+ return passed;
7
+ };
8
+ export default checkSpecReference;
@@ -0,0 +1,8 @@
1
+ const checkDestinationPath = (instruction, showLog, returnLog) => {
2
+ const passed = Boolean(instruction.destinationPath?.fileName && Array.isArray(instruction.destinationPath?.filePath));
3
+ const message = `structure/destinationPath: ${passed ? "passed" : "failed"}`;
4
+ returnLog.push(message);
5
+ if (showLog) console.log(message);
6
+ return passed;
7
+ };
8
+ export default checkDestinationPath;
@@ -0,0 +1,8 @@
1
+ const checkFunc = (instruction, showLog, returnLog) => {
2
+ const passed = typeof instruction.func === "string" && instruction.func.length > 0;
3
+ const message = `structure/func: ${passed ? "passed" : "failed"}`;
4
+ returnLog.push(message);
5
+ if (showLog) console.log(message);
6
+ return passed;
7
+ };
8
+ export default checkFunc;
@@ -0,0 +1,15 @@
1
+ import checkSourcePath from "./sourcePath/index.js";
2
+ import checkDestinationPath from "./destinationPath/index.js";
3
+ import checkTemplateKey from "./templateKey/index.js";
4
+ import checkFunc from "./func/index.js";
5
+
6
+ const checkStructure = (instruction, showLog, returnLog) => {
7
+ const results = [
8
+ checkSourcePath(instruction, showLog, returnLog),
9
+ checkDestinationPath(instruction, showLog, returnLog),
10
+ checkTemplateKey(instruction, showLog, returnLog),
11
+ checkFunc(instruction, showLog, returnLog)
12
+ ];
13
+ return results.every(Boolean);
14
+ };
15
+ export default checkStructure;
@@ -0,0 +1,8 @@
1
+ const checkSourcePath = (instruction, showLog, returnLog) => {
2
+ const passed = Boolean(instruction.sourcePath?.fileName && Array.isArray(instruction.sourcePath?.filePath));
3
+ const message = `structure/sourcePath: ${passed ? "passed" : "failed"}`;
4
+ returnLog.push(message);
5
+ if (showLog) console.log(message);
6
+ return passed;
7
+ };
8
+ export default checkSourcePath;
@@ -0,0 +1,9 @@
1
+ const checkTemplateKey = (instruction, showLog, returnLog) => {
2
+ const passed = instruction.templateKey === undefined ||
3
+ (typeof instruction.templateKey === "string" && instruction.templateKey.length > 0);
4
+ const message = `structure/templateKey: ${passed ? "passed" : "failed"}`;
5
+ returnLog.push(message);
6
+ if (showLog) console.log(message);
7
+ return passed;
8
+ };
9
+ export default checkTemplateKey;
@@ -0,0 +1,28 @@
1
+ import { resolvePath } from "json-transformer-funcs";
2
+ import writePath from "./writePath/index.js";
3
+ import checkInstruction from "./guards/index.js";
4
+
5
+ const runSingleInstruction = (instruction, registry, functions, spec, showLog, returnLog) => {
6
+ if (!checkInstruction(instruction, functions, spec, showLog, returnLog)) return;
7
+
8
+ const localShowLog = showLog;
9
+ const localSpec = spec;
10
+ const templateKey = instruction.templateKey;
11
+ const localSpecKey = localSpec[templateKey];
12
+
13
+ if (localShowLog) console.log("singleInstruction ; ", templateKey, localSpecKey);
14
+
15
+ const source = resolvePath(registry, instruction.sourcePath);
16
+ const result = functions[instruction.func](source, localSpecKey);
17
+
18
+ writePath(registry, instruction.destinationPath, result);
19
+ };
20
+
21
+ const runSingleInstructions = (instructions, registry, functions, spec, showLog, returnLog) => {
22
+ instructions.forEach(instruction => {
23
+ runSingleInstruction(instruction, registry, functions, spec, showLog, returnLog);
24
+ });
25
+ };
26
+
27
+ export { runSingleInstruction, runSingleInstructions };
28
+ export default runSingleInstructions;
@@ -0,0 +1,107 @@
1
+ {
2
+ "buildBodyTr": {
3
+ "tagName": "tr",
4
+ "attributes": {
5
+ "class": "align-middle"
6
+ },
7
+ "children": []
8
+ },
9
+ "buildColGroup": {
10
+ "tagName": "col",
11
+ "attributes": {
12
+ "width": "${width}"
13
+ }
14
+ },
15
+ "buildHeader": {
16
+ "tagName": "th",
17
+ "textContent": "${columnName}",
18
+ "attributes": {
19
+ "class": "py-3 px-3 fw-semibold text-uppercase small"
20
+ }
21
+ },
22
+ "buildFooter": {
23
+ "tagName": "td",
24
+ "children": [
25
+ {
26
+ "tagName": "input",
27
+ "attributes": {
28
+ "type": "text",
29
+ "placeholder": "enter ${columnName}",
30
+ "name": "${columnName}",
31
+ "id": "${columnName}",
32
+ "list": "${columnName}",
33
+ "class": "form-control"
34
+ }
35
+ }
36
+ ]
37
+ },
38
+ "buildBodyCell": {
39
+ "tagName": "td",
40
+ "textContent": "${rowValue}",
41
+ "attributes": {
42
+ "class": "py-3 px-3 text-secondary"
43
+ }
44
+ },
45
+ "button": {
46
+ "tagName": "button",
47
+ "textContent": "${columnName}",
48
+ "attributes": {
49
+ "class": "btn btn-primary btn-sm"
50
+ }
51
+ },
52
+ "buttonCell": {
53
+ "tagName": "td",
54
+ "attributes": {
55
+ "class": "py-2 px-3 text-center"
56
+ },
57
+ "children": [
58
+ {
59
+ "tagName": "button",
60
+ "textContent": "${agg:count}",
61
+ "attributes": {
62
+ "class": "btn btn-primary btn-sm"
63
+ }
64
+ }
65
+ ]
66
+ },
67
+ "optionsButtonCell": {
68
+ "tagName": "td",
69
+ "attributes": {
70
+ "class": "py-2 px-3 text-center"
71
+ },
72
+ "children": [
73
+ {
74
+ "tagName": "button",
75
+ "textContent": "${columnName}",
76
+ "attributes": {
77
+ "class": "btn btn-primary btn-sm"
78
+ }
79
+ }
80
+ ]
81
+ },
82
+ "buildOption": {
83
+ "tagName": "option",
84
+ "textContent": "${rowValue}",
85
+ "attributes": {
86
+ "value": "${rowValue}"
87
+ }
88
+ },
89
+ "buildSelect": {
90
+ "tagName": "select",
91
+ "attributes": {
92
+ "id": "LedgerName"
93
+ },
94
+ "children": []
95
+ },
96
+ "buildDataList": {
97
+ "tagName": "datalist",
98
+ "attributes": {
99
+ "data-name": "${columnName}"
100
+ },
101
+ "children": []
102
+ },
103
+ "emptyCell": {
104
+ "tagName": "td",
105
+ "children": []
106
+ }
107
+ }
@@ -0,0 +1,62 @@
1
+ import { findByRole } from "json-transformer-funcs";
2
+
3
+ const toArray = value => Array.isArray(value) ? value : [value];
4
+
5
+ const set = (parent, key, value) => {
6
+ parent[key] = value;
7
+ };
8
+
9
+ const push = (parent, key, value) => {
10
+ parent[key].push(...toArray(value));
11
+ };
12
+
13
+ const unshift = (parent, key, value) => {
14
+ parent[key].unshift(...toArray(value));
15
+ };
16
+
17
+ const pushToRows = (parent, key, value) => {
18
+ parent[key].forEach(row => {
19
+ row.children.push(...structuredClone(toArray(value)));
20
+ });
21
+ };
22
+
23
+ const unshiftToRows = (parent, key, value) => {
24
+ parent[key].forEach(row => {
25
+ row.children.unshift(...structuredClone(toArray(value)));
26
+ });
27
+ };
28
+
29
+ const unshiftSerialToRows = (parent, key, value) => {
30
+ parent[key].forEach((row, rowIndex) => {
31
+ const item = structuredClone(value);
32
+ item.textContent = rowIndex + 1;
33
+ row.children.unshift(item);
34
+ });
35
+ };
36
+
37
+ const actions = {
38
+ set,
39
+ push,
40
+ unshift,
41
+ pushToRows,
42
+ unshiftToRows,
43
+ unshiftSerialToRows
44
+ };
45
+
46
+ const resolveParent = (registry, path) => {
47
+ const root = path.dataRole
48
+ ? findByRole(registry[path.fileName], path.dataRole)
49
+ : registry[path.fileName];
50
+ const keys = [...(path.filePath ?? [])];
51
+ const key = keys.pop();
52
+ const parent = keys.reduce((current, nextKey) => current[nextKey], root);
53
+ return { parent, key };
54
+ };
55
+
56
+ const writePath = (registry, path, value) => {
57
+ const { parent, key } = resolveParent(registry, path);
58
+ const action = actions[path.action] || set;
59
+ action(parent, key, value);
60
+ };
61
+
62
+ export default writePath;
@@ -0,0 +1,62 @@
1
+ import { findByRole } from "json-transformer-funcs";
2
+
3
+ const toArray = value => Array.isArray(value) ? value : [value];
4
+
5
+ const set = (parent, key, value) => {
6
+ parent[key] = value;
7
+ };
8
+
9
+ const push = (parent, key, value) => {
10
+ parent[key].push(...toArray(value));
11
+ };
12
+
13
+ const unshift = (parent, key, value) => {
14
+ parent[key].unshift(...toArray(value));
15
+ };
16
+
17
+ const pushToRows = (parent, key, value) => {
18
+ parent[key].forEach(row => {
19
+ row.children.push(...structuredClone(toArray(value)));
20
+ });
21
+ };
22
+
23
+ const unshiftToRows = (parent, key, value) => {
24
+ parent[key].forEach(row => {
25
+ row.children.unshift(...structuredClone(toArray(value)));
26
+ });
27
+ };
28
+
29
+ const unshiftSerialToRows = (parent, key, value) => {
30
+ parent[key].forEach((row, rowIndex) => {
31
+ const item = structuredClone(value);
32
+ item.textContent = rowIndex + 1;
33
+ row.children.unshift(item);
34
+ });
35
+ };
36
+
37
+ const actions = {
38
+ set,
39
+ push,
40
+ unshift,
41
+ pushToRows,
42
+ unshiftToRows,
43
+ unshiftSerialToRows
44
+ };
45
+
46
+ const resolveParent = (registry, path) => {
47
+ const root = path.dataRole
48
+ ? findByRole(registry[path.fileName], path.dataRole)
49
+ : registry[path.fileName];
50
+ const keys = [...(path.filePath ?? [])];
51
+ const key = keys.pop();
52
+ const parent = keys.reduce((current, nextKey) => current[nextKey], root);
53
+ return { parent, key };
54
+ };
55
+
56
+ const writePath = (registry, path, value) => {
57
+ const { parent, key } = resolveParent(registry, path);
58
+ const action = actions[path.action] || set;
59
+ action(parent, key, value);
60
+ };
61
+
62
+ export default writePath;