json-to-spec 19.1.0

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 (217) hide show
  1. package/README.md +118 -0
  2. package/docs/demo.html +426 -0
  3. package/docs/dist/min.js +164 -0
  4. package/docs/dist/v1/min.js +124 -0
  5. package/docs/dist/v17/min.js +355 -0
  6. package/docs/dist/v18/min.js +160 -0
  7. package/docs/dist/v19/min.js +164 -0
  8. package/docs/dist/v2/min.js +129 -0
  9. package/docs/dist/v3/min.js +201 -0
  10. package/docs/index.html +248 -0
  11. package/docs/pages/architecture.html +115 -0
  12. package/docs/pages/how-it-works.html +125 -0
  13. package/docs/pages/what-not.html +100 -0
  14. package/docs/pages/what.html +105 -0
  15. package/docs/pages/why.html +88 -0
  16. package/docs/samples/form.html +265 -0
  17. package/docs/samples/index.html +112 -0
  18. package/docs/samples/table.html +306 -0
  19. package/index.js +2 -0
  20. package/package.json +50 -0
  21. package/samples/dataList/index.html +31 -0
  22. package/samples/dataList/index.js +62 -0
  23. package/samples/dataList/input/data.json +5043 -0
  24. package/samples/dataList/input/structure.json +38 -0
  25. package/samples/dataList copy/index.html +30 -0
  26. package/samples/dataList copy/index.js +61 -0
  27. package/samples/dataList copy/input/data.json +35307 -0
  28. package/samples/dataList copy/input/structure.json +18 -0
  29. package/samples/form/index.html +29 -0
  30. package/samples/form/index.js +64 -0
  31. package/samples/form/input/data.json +9 -0
  32. package/samples/form/input/structure.json +192 -0
  33. package/samples/form-/data.json +42 -0
  34. package/samples/form-/structure.json +110 -0
  35. package/samples/select/index.html +31 -0
  36. package/samples/select/index.js +61 -0
  37. package/samples/select/input/data.json +35287 -0
  38. package/samples/select/input/select.json +18 -0
  39. package/samples/select/input/structure.json +47 -0
  40. package/samples/table/index.html +29 -0
  41. package/samples/table/index.js +61 -0
  42. package/samples/table/input/data.json +35287 -0
  43. package/samples/table/input/structure copy.json +47 -0
  44. package/samples/table/input/structure.json +86 -0
  45. package/samples/table/input/table.json +135 -0
  46. package/samples/table-/data.json +68 -0
  47. package/samples/table-/structure.json +235 -0
  48. package/src/index.js +2 -0
  49. package/src/v10/index.js +96 -0
  50. package/src/v10/instructionEngine/compile/compileTree.js +191 -0
  51. package/src/v10/instructionEngine/compileTree.js +1 -0
  52. package/src/v10/instructionEngine/expandIteration.js +1 -0
  53. package/src/v10/instructionEngine/index.js +29 -0
  54. package/src/v10/instructionEngine/interpolate.js +2 -0
  55. package/src/v10/instructionEngine/iteration/expandIteration.js +95 -0
  56. package/src/v10/instructionEngine/operation/operation.js +237 -0
  57. package/src/v10/instructionEngine/other/other.js +150 -0
  58. package/src/v10/instructionEngine/pipeline/pipeline.js +47 -0
  59. package/src/v10/instructionEngine/replace/replace.js +173 -0
  60. package/src/v10/instructionEngine/resolve/resolvePath.js +18 -0
  61. package/src/v10/instructionEngine/resolve/resolveTokenValue.js +22 -0
  62. package/src/v10/instructionEngine/resolvePath.js +1 -0
  63. package/src/v10/instructionEngine/resolveTokenValue.js +1 -0
  64. package/src/v10/instructionEngine/story/storyInjector.js +50 -0
  65. package/src/v10/instructionEngine/storyInjector.js +1 -0
  66. package/src/v10/instructionEngine/value/interpolate.js +38 -0
  67. package/src/v11/index.js +116 -0
  68. package/src/v11/iterate.js +270 -0
  69. package/src/v11/replace.js +169 -0
  70. package/src/v11/resolvePath.js +29 -0
  71. package/src/v12/index.js +122 -0
  72. package/src/v12/iterate/bindFormValues.js +74 -0
  73. package/src/v12/iterate/expandIteration.js +181 -0
  74. package/src/v12/iterate/index.js +10 -0
  75. package/src/v12/iterate/iterate.js +56 -0
  76. package/src/v12/iterate/iterateNode.js +86 -0
  77. package/src/v12/iterate/resolveCollection.js +37 -0
  78. package/src/v12/iterate.js +270 -0
  79. package/src/v12/replace.js +169 -0
  80. package/src/v12/resolvePath.js +29 -0
  81. package/src/v13/index.js +110 -0
  82. package/src/v13/iterate/bindFormValues.js +74 -0
  83. package/src/v13/iterate/expandIteration.js +188 -0
  84. package/src/v13/iterate/index.js +9 -0
  85. package/src/v13/iterate/iterate.js +32 -0
  86. package/src/v13/iterate/iterateNode.js +86 -0
  87. package/src/v13/iterate/resolveCollection.js +37 -0
  88. package/src/v13/iterate/walk.js +90 -0
  89. package/src/v13/iterate.js +270 -0
  90. package/src/v13/replace.js +169 -0
  91. package/src/v13/resolvePath.js +29 -0
  92. package/src/v14/index.js +112 -0
  93. package/src/v14/iterate/bindFormValues.js +74 -0
  94. package/src/v14/iterate/expandIteration.js +188 -0
  95. package/src/v14/iterate/index.js +9 -0
  96. package/src/v14/iterate/iterate.js +32 -0
  97. package/src/v14/iterate/resolveCollection.js +37 -0
  98. package/src/v14/iterate/walk/walk copy 2.js +114 -0
  99. package/src/v14/iterate/walk/walk copy.js +90 -0
  100. package/src/v14/iterate/walk/walk.js +98 -0
  101. package/src/v14/iterate.js +270 -0
  102. package/src/v14/replace/index.js +16 -0
  103. package/src/v14/replace/walk.js +90 -0
  104. package/src/v14/replace.js +153 -0
  105. package/src/v14/resolvePath.js +29 -0
  106. package/src/v15/index.js +123 -0
  107. package/src/v15/iterate/bindFormValues.js +74 -0
  108. package/src/v15/iterate/expandIteration.js +188 -0
  109. package/src/v15/iterate/index.js +9 -0
  110. package/src/v15/iterate/iterate.js +32 -0
  111. package/src/v15/iterate/resolveCollection.js +37 -0
  112. package/src/v15/iterate/walk/walk copy 2.js +114 -0
  113. package/src/v15/iterate/walk/walk copy.js +90 -0
  114. package/src/v15/iterate/walk/walk.js +195 -0
  115. package/src/v15/iterate.js +270 -0
  116. package/src/v15/replace/index.js +16 -0
  117. package/src/v15/replace/walk.js +90 -0
  118. package/src/v15/replace.js +155 -0
  119. package/src/v15/resolvePath.js +29 -0
  120. package/src/v16/index.js +129 -0
  121. package/src/v16/iterate/bindFormValues.js +74 -0
  122. package/src/v16/iterate/expandIteration.js +188 -0
  123. package/src/v16/iterate/index.js +9 -0
  124. package/src/v16/iterate/iterate.js +32 -0
  125. package/src/v16/iterate/resolveCollection.js +37 -0
  126. package/src/v16/iterate/walk/walk copy 2.js +114 -0
  127. package/src/v16/iterate/walk/walk copy.js +90 -0
  128. package/src/v16/iterate/walk/walk.js +196 -0
  129. package/src/v16/iterate.js +270 -0
  130. package/src/v16/replace/index.js +16 -0
  131. package/src/v16/replace/walk.js +90 -0
  132. package/src/v16/replace.js +155 -0
  133. package/src/v16/resolvePath.js +29 -0
  134. package/src/v17/index.js +119 -0
  135. package/src/v17/iterate/bindFormValues.js +74 -0
  136. package/src/v17/iterate/expandIteration.js +188 -0
  137. package/src/v17/iterate/index.js +9 -0
  138. package/src/v17/iterate/iterate.js +32 -0
  139. package/src/v17/iterate/resolveCollection.js +37 -0
  140. package/src/v17/iterate/walk/iterateDo.js +42 -0
  141. package/src/v17/iterate/walk/replaceWithData.js +60 -0
  142. package/src/v17/iterate/walk/walk.js +94 -0
  143. package/src/v17/iterate.js +270 -0
  144. package/src/v17/replace/index.js +16 -0
  145. package/src/v17/replace/walk.js +90 -0
  146. package/src/v17/replace.js +160 -0
  147. package/src/v17/resolvePath.js +29 -0
  148. package/src/v18/index.js +66 -0
  149. package/src/v18/meta.js +7 -0
  150. package/src/v18/registerGlobal.js +17 -0
  151. package/src/v18/replace.js +18 -0
  152. package/src/v18/resolvePath.js +29 -0
  153. package/src/v18/walk/iterate/v1/index.js +42 -0
  154. package/src/v18/walk/iterate/v2/index.js +32 -0
  155. package/src/v18/walk/iterate/v3/index.js +46 -0
  156. package/src/v18/walk/iterate/v4/createChildren.js +24 -0
  157. package/src/v18/walk/iterate/v4/index.js +25 -0
  158. package/src/v18/walk/replaceWithData/v1/index.js +42 -0
  159. package/src/v18/walk/replaceWithData/v1/replaceCommon.js +21 -0
  160. package/src/v18/walk/replaceWithData/v2/index.js +105 -0
  161. package/src/v18/walk/replaceWithData/v2/replaceCommon.js +21 -0
  162. package/src/v18/walk/replaceWithData/v3/index.js +15 -0
  163. package/src/v18/walk/replaceWithData/v3/replaceAttributes.js +29 -0
  164. package/src/v18/walk/replaceWithData/v3/replaceCommon.js +23 -0
  165. package/src/v18/walk/replaceWithData/v3/replaceNodeValue.js +27 -0
  166. package/src/v18/walk/walk.js +94 -0
  167. package/src/v19/index.js +55 -0
  168. package/src/v19/meta.js +7 -0
  169. package/src/v19/registerGlobal.js +17 -0
  170. package/src/v19/replace.js +18 -0
  171. package/src/v19/resolvePath.js +29 -0
  172. package/src/v19/walk/iterate/v1/index.js +42 -0
  173. package/src/v19/walk/iterate/v2/index.js +32 -0
  174. package/src/v19/walk/iterate/v3/index.js +46 -0
  175. package/src/v19/walk/iterate/v4/createChildren.js +27 -0
  176. package/src/v19/walk/iterate/v4/index.js +25 -0
  177. package/src/v19/walk/replaceWithData/v1/index.js +42 -0
  178. package/src/v19/walk/replaceWithData/v1/replaceCommon.js +21 -0
  179. package/src/v19/walk/replaceWithData/v2/index.js +105 -0
  180. package/src/v19/walk/replaceWithData/v2/replaceCommon.js +21 -0
  181. package/src/v19/walk/replaceWithData/v3/index.js +15 -0
  182. package/src/v19/walk/replaceWithData/v3/replaceAttributes.js +29 -0
  183. package/src/v19/walk/replaceWithData/v3/replaceCommon.js +23 -0
  184. package/src/v19/walk/replaceWithData/v3/replaceNodeValue.js +27 -0
  185. package/src/v19/walk/walk.js +97 -0
  186. package/src/v8/index.js +69 -0
  187. package/src/v8/instructionEngine/compile/compileTree.js +191 -0
  188. package/src/v8/instructionEngine/compileTree.js +1 -0
  189. package/src/v8/instructionEngine/expandIteration.js +1 -0
  190. package/src/v8/instructionEngine/index.js +16 -0
  191. package/src/v8/instructionEngine/interpolate.js +2 -0
  192. package/src/v8/instructionEngine/iteration/expandIteration.js +95 -0
  193. package/src/v8/instructionEngine/resolve/resolvePath.js +18 -0
  194. package/src/v8/instructionEngine/resolve/resolveTokenValue.js +22 -0
  195. package/src/v8/instructionEngine/resolvePath.js +1 -0
  196. package/src/v8/instructionEngine/resolveTokenValue.js +1 -0
  197. package/src/v8/instructionEngine/story/storyInjector.js +50 -0
  198. package/src/v8/instructionEngine/storyInjector.js +1 -0
  199. package/src/v8/instructionEngine/value/interpolate.js +38 -0
  200. package/src/v9/index.js +96 -0
  201. package/src/v9/instructionEngine/compile/compileTree.js +191 -0
  202. package/src/v9/instructionEngine/compileTree.js +1 -0
  203. package/src/v9/instructionEngine/expandIteration.js +1 -0
  204. package/src/v9/instructionEngine/index.js +29 -0
  205. package/src/v9/instructionEngine/interpolate.js +2 -0
  206. package/src/v9/instructionEngine/iteration/expandIteration.js +95 -0
  207. package/src/v9/instructionEngine/operation/operation.js +237 -0
  208. package/src/v9/instructionEngine/other/other.js +150 -0
  209. package/src/v9/instructionEngine/pipeline/pipeline.js +47 -0
  210. package/src/v9/instructionEngine/replace/replace.js +173 -0
  211. package/src/v9/instructionEngine/resolve/resolvePath.js +18 -0
  212. package/src/v9/instructionEngine/resolve/resolveTokenValue.js +22 -0
  213. package/src/v9/instructionEngine/resolvePath.js +1 -0
  214. package/src/v9/instructionEngine/resolveTokenValue.js +1 -0
  215. package/src/v9/instructionEngine/story/storyInjector.js +50 -0
  216. package/src/v9/instructionEngine/storyInjector.js +1 -0
  217. package/src/v9/instructionEngine/value/interpolate.js +38 -0
@@ -0,0 +1,191 @@
1
+ import { interpolateValue, interpolateString } from "../value/interpolate.js";
2
+ import { injectStory } from "../story/storyInjector.js";
3
+ import { expandIterationNode } from "../iteration/expandIteration.js";
4
+
5
+ const compileArrayNode = ({ inNode, inContext, inRootData }) => {
6
+ const flattened = [];
7
+
8
+ for (const child of inNode) {
9
+ const childContext = child && child.__loopItemContext
10
+ ? { ...inContext, ...child.__loopItemContext }
11
+ : inContext;
12
+
13
+ const compiledChild = compileNode({
14
+ inNode: child,
15
+ inContext: childContext,
16
+ inRootData
17
+ });
18
+
19
+ if (Array.isArray(compiledChild)) {
20
+ flattened.push(...compiledChild);
21
+ } else if (compiledChild !== null && compiledChild !== undefined) {
22
+ flattened.push(compiledChild);
23
+ }
24
+ }
25
+
26
+ return flattened;
27
+ };
28
+
29
+ const compilePrimitiveNode = ({ inNode, inContext, inRootData }) => {
30
+ return interpolateString({
31
+ inText: inNode,
32
+ inItemContext: inContext,
33
+ inRootData
34
+ });
35
+ };
36
+
37
+ const applyValueBindings = ({ inNode, inItemContext, inRootData }) => {
38
+ const clone = { ...inNode };
39
+
40
+ if (clone.textContent) {
41
+ clone.textContent = interpolateString({
42
+ inText: clone.textContent,
43
+ inItemContext,
44
+ inRootData
45
+ });
46
+ }
47
+
48
+ if (clone.attributes) {
49
+ clone.attributes = { ...clone.attributes };
50
+ for (const [name, value] of Object.entries(clone.attributes)) {
51
+ if (typeof value === "string") {
52
+ clone.attributes[name] = interpolateValue({
53
+ inValue: value,
54
+ inItemContext,
55
+ inRootData
56
+ });
57
+ }
58
+ }
59
+ }
60
+
61
+ if (clone.properties) {
62
+ clone.properties = { ...clone.properties };
63
+ for (const [name, value] of Object.entries(clone.properties)) {
64
+ if (typeof value === "string") {
65
+ clone.properties[name] = interpolateValue({
66
+ inValue: value,
67
+ inItemContext,
68
+ inRootData
69
+ });
70
+ }
71
+ }
72
+ }
73
+
74
+ return clone;
75
+ };
76
+
77
+ const compileStandardNode = ({ inNode, inContext, inRootData }) => {
78
+ const currentItem = {
79
+ ...(typeof inContext.item === "object" ? inContext.item : {}),
80
+ ...inContext
81
+ };
82
+
83
+ const withBindings = applyValueBindings({
84
+ inNode: { ...inNode },
85
+ inItemContext: currentItem,
86
+ inRootData
87
+ });
88
+
89
+ if ("jsonToSpec" in withBindings) {
90
+ delete withBindings.jsonToSpec;
91
+ }
92
+
93
+ if (withBindings.attributes) {
94
+ const isFormField = ["input", "textarea", "select", "option"].includes(withBindings.tagName?.toLowerCase());
95
+ if (isFormField) {
96
+ const fieldName = currentItem.field || currentItem.columnName || currentItem.name;
97
+ if (currentItem.value !== undefined && currentItem.value !== null) {
98
+ withBindings.attributes.value = String(currentItem.value);
99
+ if (withBindings.tagName === "textarea" && !withBindings.textContent) {
100
+ withBindings.textContent = String(currentItem.value);
101
+ }
102
+ } else if (fieldName && inRootData[fieldName] !== undefined && inRootData[fieldName] !== null) {
103
+ withBindings.attributes.value = String(inRootData[fieldName]);
104
+ if (withBindings.tagName === "textarea" && !withBindings.textContent) {
105
+ withBindings.textContent = String(inRootData[fieldName]);
106
+ }
107
+ } else if (fieldName && inRootData.values && inRootData.values[fieldName] !== undefined && inRootData.values[fieldName] !== null) {
108
+ withBindings.attributes.value = String(inRootData.values[fieldName]);
109
+ if (withBindings.tagName === "textarea" && !withBindings.textContent) {
110
+ withBindings.textContent = String(inRootData.values[fieldName]);
111
+ }
112
+ }
113
+ }
114
+ }
115
+
116
+ const storyNode = injectStory(withBindings, inNode, currentItem, inRootData);
117
+
118
+ if (Array.isArray(storyNode.children)) {
119
+ const compiledChildren = [];
120
+ for (const child of storyNode.children) {
121
+ const childContext = child && child.__loopItemContext
122
+ ? { ...inContext, ...child.__loopItemContext }
123
+ : inContext;
124
+
125
+ const compiledChild = compileNode({
126
+ inNode: child,
127
+ inContext: childContext,
128
+ inRootData
129
+ });
130
+
131
+ if (Array.isArray(compiledChild)) {
132
+ compiledChildren.push(...compiledChild);
133
+ } else if (compiledChild !== null && compiledChild !== undefined) {
134
+ compiledChildren.push(compiledChild);
135
+ }
136
+ }
137
+ storyNode.children = compiledChildren;
138
+ }
139
+
140
+ delete storyNode.__loopItemContext;
141
+ return storyNode;
142
+ };
143
+
144
+ export const compileNode = ({ inNode, inContext = {}, inRootData = {} } = {}) => {
145
+ if (inNode === null || inNode === undefined) return null;
146
+
147
+ if (Array.isArray(inNode)) {
148
+ return compileArrayNode({
149
+ inNode,
150
+ inContext,
151
+ inRootData
152
+ });
153
+ }
154
+
155
+ if (typeof inNode !== "object") {
156
+ return compilePrimitiveNode({
157
+ inNode,
158
+ inContext,
159
+ inRootData
160
+ });
161
+ }
162
+
163
+ const iterationResult = expandIterationNode({
164
+ inNode,
165
+ inContext,
166
+ inRootData
167
+ });
168
+
169
+ if (iterationResult !== undefined) {
170
+ if (Array.isArray(iterationResult)) {
171
+ return compileArrayNode({
172
+ inNode: iterationResult,
173
+ inContext,
174
+ inRootData
175
+ });
176
+ }
177
+ return compileStandardNode({
178
+ inNode: iterationResult,
179
+ inContext,
180
+ inRootData
181
+ });
182
+ }
183
+
184
+ return compileStandardNode({
185
+ inNode,
186
+ inContext,
187
+ inRootData
188
+ });
189
+ };
190
+
191
+ export default compileNode;
@@ -0,0 +1 @@
1
+ export { compileNode, default } from "./compile/compileTree.js";
@@ -0,0 +1 @@
1
+ export { expandIterationNode, default } from "./iteration/expandIteration.js";
@@ -0,0 +1,29 @@
1
+ import compileNode from "./compile/compileTree.js";
2
+ import resolvePath from "./resolve/resolvePath.js";
3
+ import { expandIterationNode } from "./iteration/expandIteration.js";
4
+ import { interpolateValue, interpolateString } from "./value/interpolate.js";
5
+ import { injectStory } from "./story/storyInjector.js";
6
+ import { operation, operationNode } from "./operation/operation.js";
7
+ import { replace, replaceNode } from "./replace/replace.js";
8
+ import { other, otherNode } from "./other/other.js";
9
+ import { pipeline, pipe, defaultPipeline } from "./pipeline/pipeline.js";
10
+
11
+ export {
12
+ compileNode,
13
+ expandIterationNode,
14
+ interpolateValue,
15
+ interpolateString,
16
+ injectStory,
17
+ resolvePath,
18
+ operation,
19
+ operationNode,
20
+ replace,
21
+ replaceNode,
22
+ other,
23
+ otherNode,
24
+ pipeline,
25
+ pipe,
26
+ defaultPipeline
27
+ };
28
+
29
+ export default compileNode;
@@ -0,0 +1,2 @@
1
+ export { interpolateValue, interpolateString } from "./value/interpolate.js";
2
+ export { default } from "./value/interpolate.js";
@@ -0,0 +1,95 @@
1
+ import resolvePath from "../resolve/resolvePath.js";
2
+
3
+ const resolveCollection = ({ inNode, inContext, inRootData }) => {
4
+ const instruction = inNode && inNode.jsonToSpec;
5
+ if (!instruction) return undefined;
6
+
7
+ const sourceKey = instruction.source || instruction.iterateOn || instruction.$iterate;
8
+ if (!sourceKey) return undefined;
9
+
10
+ return resolvePath({ inData: inRootData, inPath: sourceKey })
11
+ || (inRootData && inRootData[sourceKey])
12
+ || resolvePath({ inData: inContext, inPath: sourceKey })
13
+ || (inContext && inContext[sourceKey]);
14
+ };
15
+
16
+ export const expandIterationNode = ({ inNode, inContext = {}, inRootData = {} }) => {
17
+ const instruction = inNode && inNode.jsonToSpec;
18
+ if (!instruction) return undefined;
19
+
20
+ const operation = instruction.operation;
21
+ if (operation !== "iterate" && !instruction.source && !instruction.$iterate) {
22
+ return undefined;
23
+ }
24
+
25
+ const sourceKey = instruction.source || instruction.iterateOn || instruction.$iterate;
26
+ const collection = resolveCollection({ inNode, inContext, inRootData }) || [];
27
+
28
+ if (!Array.isArray(collection)) {
29
+ if (inNode && inNode.tagName) {
30
+ const cloned = { ...inNode };
31
+ delete cloned.jsonToSpec;
32
+ cloned.children = [];
33
+ return cloned;
34
+ }
35
+ return [];
36
+ }
37
+
38
+ const template = instruction.template || instruction.item || {};
39
+ const filter = instruction.filter;
40
+
41
+ const filtered = collection.filter(item => {
42
+ if (!item || typeof item !== "object") return true;
43
+ if (filter && typeof filter === "object") {
44
+ for (const [key, value] of Object.entries(filter)) {
45
+ if (item[key] !== value) return false;
46
+ }
47
+ }
48
+ if (item.isVisible === false) return false;
49
+ return true;
50
+ });
51
+
52
+ const expandedChildren = [];
53
+ filtered.forEach((item, index) => {
54
+ const itemContext = {
55
+ ...inContext,
56
+ item,
57
+ ...(typeof item === "object" && item !== null ? item : {}),
58
+ $index: index,
59
+ $number: index + 1
60
+ };
61
+
62
+ if (sourceKey === "rows" || sourceKey.endsWith(".rows") || sourceKey === "items" || sourceKey.endsWith("Rows")) {
63
+ itemContext.row = item;
64
+ }
65
+
66
+ if (inContext.row && (item.field || item.columnName || item.name)) {
67
+ const fieldKey = item.field || item.columnName || item.name;
68
+ const cellValue = inContext.row[fieldKey];
69
+ if (cellValue !== undefined) {
70
+ itemContext.cellValue = cellValue;
71
+ itemContext.value = cellValue;
72
+ if (typeof item === "object" && item !== null && item.value === undefined) {
73
+ item.value = cellValue;
74
+ }
75
+ }
76
+ }
77
+
78
+ const templateNode = typeof template === "object" && template !== null
79
+ ? { ...template, __loopItemContext: itemContext }
80
+ : { tagName: "span", textContent: template, __loopItemContext: itemContext };
81
+
82
+ expandedChildren.push(templateNode);
83
+ });
84
+
85
+ if (inNode && inNode.tagName) {
86
+ const cloned = { ...inNode };
87
+ delete cloned.jsonToSpec;
88
+ cloned.children = expandedChildren;
89
+ return cloned;
90
+ }
91
+
92
+ return expandedChildren;
93
+ };
94
+
95
+ export default expandIterationNode;
@@ -0,0 +1,237 @@
1
+ import resolvePath from "../resolve/resolvePath.js";
2
+
3
+ const resolveCollection = ({ inNode, inContext, inRootData }) => {
4
+ const localNode = inNode;
5
+ const localContext = inContext;
6
+ const localRootData = inRootData;
7
+
8
+ const instruction = localNode && (localNode.jsonToSpec || (localNode.operation ? localNode : null));
9
+ if (!instruction) return undefined;
10
+
11
+ const sourceKey = instruction.source || instruction.iterateOn || instruction.$iterate;
12
+ if (!sourceKey) return undefined;
13
+
14
+ return resolvePath({ inData: localRootData, inPath: sourceKey })
15
+ || (localRootData && localRootData[sourceKey])
16
+ || resolvePath({ inData: localContext, inPath: sourceKey })
17
+ || (localContext && localContext[sourceKey]);
18
+ };
19
+
20
+ const expandSingleIteration = ({ inNode, inContext, inRootData }) => {
21
+ const localNode = inNode;
22
+ const localContext = inContext;
23
+ const localRootData = inRootData;
24
+
25
+ const instruction = localNode && (localNode.jsonToSpec || (localNode.operation ? localNode : null));
26
+ if (!instruction) return undefined;
27
+
28
+ const operationType = instruction.operation;
29
+ if (operationType !== "iterate" && !instruction.source && !instruction.$iterate) {
30
+ return undefined;
31
+ }
32
+
33
+ const sourceKey = instruction.source || instruction.iterateOn || instruction.$iterate;
34
+ const collection = resolveCollection({
35
+ inNode: localNode,
36
+ inContext: localContext,
37
+ inRootData: localRootData
38
+ }) || [];
39
+
40
+ if (!Array.isArray(collection)) {
41
+ if (localNode && localNode.tagName) {
42
+ const cloned = { ...localNode };
43
+ delete cloned.jsonToSpec;
44
+ cloned.children = [];
45
+ return cloned;
46
+ }
47
+ return [];
48
+ }
49
+
50
+ const template = instruction.template || instruction.item || {};
51
+ const filter = instruction.filter;
52
+
53
+ const filtered = collection.filter(item => {
54
+ if (!item || typeof item !== "object") return true;
55
+ if (filter && typeof filter === "object") {
56
+ for (const [key, value] of Object.entries(filter)) {
57
+ if (item[key] !== value) return false;
58
+ }
59
+ }
60
+ if (item.isVisible === false) return false;
61
+ return true;
62
+ });
63
+
64
+ const expandedChildren = [];
65
+ filtered.forEach((item, index) => {
66
+ const itemContext = {
67
+ ...localContext,
68
+ item,
69
+ ...(typeof item === "object" && item !== null ? item : {}),
70
+ $index: index,
71
+ $number: index + 1
72
+ };
73
+
74
+ if (sourceKey === "rows" || sourceKey.endsWith(".rows") || sourceKey === "items" || sourceKey.endsWith("Rows")) {
75
+ itemContext.row = item;
76
+ }
77
+
78
+ if (localContext.row && (item.field || item.columnName || item.name)) {
79
+ const fieldKey = item.field || item.columnName || item.name;
80
+ const cellValue = localContext.row[fieldKey];
81
+ if (cellValue !== undefined) {
82
+ itemContext.cellValue = cellValue;
83
+ itemContext.value = cellValue;
84
+ if (typeof item === "object" && item !== null && item.value === undefined) {
85
+ item.value = cellValue;
86
+ }
87
+ }
88
+ }
89
+
90
+ const templateNode = typeof template === "object" && template !== null
91
+ ? { ...template, __loopItemContext: itemContext }
92
+ : { tagName: "span", textContent: template, __loopItemContext: itemContext };
93
+
94
+ const expandedItem = operationNode({
95
+ inNode: templateNode,
96
+ inContext: itemContext,
97
+ inRootData: localRootData
98
+ });
99
+
100
+ if (Array.isArray(expandedItem)) {
101
+ expandedChildren.push(...expandedItem);
102
+ } else if (expandedItem !== null && expandedItem !== undefined) {
103
+ expandedChildren.push(expandedItem);
104
+ }
105
+ });
106
+
107
+ if (localNode && localNode.tagName) {
108
+ const cloned = { ...localNode };
109
+ delete cloned.jsonToSpec;
110
+ cloned.children = expandedChildren;
111
+ return cloned;
112
+ }
113
+
114
+ return expandedChildren;
115
+ };
116
+
117
+ const operationArrayNode = ({ inNode, inContext, inRootData }) => {
118
+ const localNode = inNode;
119
+ const localContext = inContext;
120
+ const localRootData = inRootData;
121
+
122
+ const flattened = [];
123
+
124
+ for (const child of localNode) {
125
+ const childContext = child && child.__loopItemContext
126
+ ? { ...localContext, ...child.__loopItemContext }
127
+ : localContext;
128
+
129
+ const processed = operationNode({
130
+ inNode: child,
131
+ inContext: childContext,
132
+ inRootData: localRootData
133
+ });
134
+
135
+ if (Array.isArray(processed)) {
136
+ flattened.push(...processed);
137
+ } else if (processed !== null && processed !== undefined) {
138
+ flattened.push(processed);
139
+ }
140
+ }
141
+
142
+ return flattened;
143
+ };
144
+
145
+ const operationStandardNode = ({ inNode, inContext, inRootData }) => {
146
+ const localNode = inNode;
147
+ const localContext = inContext;
148
+ const localRootData = inRootData;
149
+
150
+ const cloned = { ...localNode };
151
+
152
+ if (Array.isArray(cloned.children)) {
153
+ const processedChildren = [];
154
+ for (const child of cloned.children) {
155
+ const childContext = child && child.__loopItemContext
156
+ ? { ...localContext, ...child.__loopItemContext }
157
+ : localContext;
158
+
159
+ const processedChild = operationNode({
160
+ inNode: child,
161
+ inContext: childContext,
162
+ inRootData: localRootData
163
+ });
164
+
165
+ if (Array.isArray(processedChild)) {
166
+ processedChildren.push(...processedChild);
167
+ } else if (processedChild !== null && processedChild !== undefined) {
168
+ processedChildren.push(processedChild);
169
+ }
170
+ }
171
+ cloned.children = processedChildren;
172
+ }
173
+
174
+ return cloned;
175
+ };
176
+
177
+ export const operationNode = ({ inNode, inContext = {}, inRootData = {} } = {}) => {
178
+ const localNode = inNode;
179
+ const localContext = inContext;
180
+ const localRootData = inRootData;
181
+
182
+ if (localNode === null || localNode === undefined) return null;
183
+
184
+ if (Array.isArray(localNode)) {
185
+ return operationArrayNode({
186
+ inNode: localNode,
187
+ inContext: localContext,
188
+ inRootData: localRootData
189
+ });
190
+ }
191
+
192
+ if (typeof localNode !== "object") {
193
+ return localNode;
194
+ }
195
+
196
+ const iterationResult = expandSingleIteration({
197
+ inNode: localNode,
198
+ inContext: localContext,
199
+ inRootData: localRootData
200
+ });
201
+
202
+ if (iterationResult !== undefined) {
203
+ return iterationResult;
204
+ }
205
+
206
+ return operationStandardNode({
207
+ inNode: localNode,
208
+ inContext: localContext,
209
+ inRootData: localRootData
210
+ });
211
+ };
212
+
213
+ export const operation = (inStructureOrOptions, inData = {}) => {
214
+ let localStructure = inStructureOrOptions;
215
+ let localData = inData;
216
+ let localContext = {};
217
+
218
+ if (inStructureOrOptions && typeof inStructureOrOptions === "object" && !Array.isArray(inStructureOrOptions)) {
219
+ if ("inStructure" in inStructureOrOptions || "inTree" in inStructureOrOptions || "inNode" in inStructureOrOptions) {
220
+ localStructure = inStructureOrOptions.inStructure || inStructureOrOptions.inTree || inStructureOrOptions.inNode;
221
+ localData = inStructureOrOptions.inData || localData;
222
+ localContext = inStructureOrOptions.inContext || localContext;
223
+ } else if ("structure" in inStructureOrOptions) {
224
+ localStructure = inStructureOrOptions.structure;
225
+ localData = inStructureOrOptions.data || localData;
226
+ localContext = inStructureOrOptions.context || localContext;
227
+ }
228
+ }
229
+
230
+ return operationNode({
231
+ inNode: localStructure,
232
+ inContext: localContext,
233
+ inRootData: localData
234
+ });
235
+ };
236
+
237
+ export default operation;