MCPcopy
hub / github.com/loggerhead/json4u / createObjectNode

Function createObjectNode

src/lib/table/createNode.ts:187–238  ·  view source on GitHub ↗

* Creates a `TableNode` for a JSON object. This function arranges the key-value pairs * into a two-column layout. The first column contains the keys, and the second column * contains the values. If a value is a nested object or array, it is represented by a * `dummy` node that will be recursively

(ctx: CreateContext, tree: Tree, node: Node)

Source from the content-addressed store, hash-verified

185 * @param node The JSON `Node` representing the object.
186 */
187function createObjectNode(ctx: CreateContext, tree: Tree, node: Node): TableNode {
188 let maxKeyWidth = 0;
189 let maxValWidth = 0;
190 let span = 0;
191 let maxSpan = 0;
192
193 const childCnt = node.childrenKeys?.length || 0;
194 // Generate a key and a value node for each child property.
195 const kvNodes = tree.mapChildren(node, (child, key, idx) => {
196 const newCtx = { ...ctx, row: ctx.row + span };
197 const isFirst = idx === 0;
198 const isLast = idx === childCnt - 1;
199
200 const keyNode = newNode(newCtx, "key")
201 .setText(key || '""')
202 .addClass(key ? "text-hl-key" : "text-hl-empty")
203 .setId(child.id)
204 .setBorderFlag("left")
205 .setBorderFlag(isFirst ? "top" : "")
206 .setBorderFlag(isLast ? "bottom" : "");
207 const valNode = createNode(newCtx, tree, child)
208 .setBorderFlag("right")
209 .setBorderFlag(isFirst ? "top" : "")
210 .setBorderFlag(isLast ? "bottom" : "");
211
212 span += valNode.span;
213 maxSpan = Math.max(maxSpan, valNode.span);
214 maxKeyWidth = Math.max(maxKeyWidth, keyNode.width);
215 maxValWidth = Math.max(maxValWidth, valNode.width);
216 return { keyNode, valNode };
217 });
218
219 const parent = newNode(ctx, "leftHeaderTable");
220 const heads = kvNodes.map(({ keyNode, valNode }, i) => {
221 valNode
222 .setWidth(maxValWidth)
223 .setParent(parent)
224 .setUp(kvNodes?.[i - 1]?.valNode);
225 keyNode
226 .setRight(valNode)
227 .setSpan(valNode.span)
228 .setWidth(maxKeyWidth)
229 .setParent(parent)
230 .setUp(kvNodes?.[i - 1]?.keyNode);
231 return keyNode;
232 });
233
234 return parent
235 .setWidth(maxKeyWidth + maxValWidth)
236 .setSpan(span)
237 .setHeads(heads);
238}
239
240function newNode(ctx: CreateContext, type: TableNodeType, id?: string) {
241 return new TableNode(type)

Callers

nothing calls this directly

Calls 13

newNodeFunction · 0.85
createNodeFunction · 0.85
mapChildrenMethod · 0.80
setBorderFlagMethod · 0.80
setIdMethod · 0.80
addClassMethod · 0.80
setTextMethod · 0.80
setUpMethod · 0.80
setParentMethod · 0.80
setWidthMethod · 0.80
setSpanMethod · 0.80
setRightMethod · 0.80

Tested by

no test coverage detected