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

Function createNode

src/lib/table/createNode.ts:20–50  ·  view source on GitHub ↗
(ctx: CreateContext, tree: Tree, node: Node)

Source from the content-addressed store, hash-verified

18 * @param node The JSON `Node` to process.
19 */
20export function createNode(ctx: CreateContext, tree: Tree, node: Node): TableNode {
21 // Handles cases where a key is present in some objects in an array but not others.
22 // For example, in `[{"a":1, "b":2}, {"a":1}]`, the second object is missing the key "b".
23 if (node === undefined) {
24 return newNode(ctx, "value").setText("miss").addClass("text-hl-empty");
25 }
26
27 const { id, type } = node;
28
29 // If the node is an object or array, delegate to a specific creation function.
30 if (isIterable(node)) {
31 if (hasChildren(node)) {
32 const genFn = type === "array" ? createArrayNode : createObjectNode;
33 const newCtx = tree.isRoot(node) ? ctx : { ...ctx, level: ctx.level + 1 };
34 return genFn(newCtx, tree, node).setId(id);
35 } else {
36 // Handle empty objects or arrays.
37 const text = type === "object" ? "{}" : "[]";
38 return newNode(ctx, "value", id).setText(text).addClass("text-hl-empty");
39 }
40 }
41
42 // Handle literal values (string, number, boolean, null).
43 if (type === "string") {
44 const text = node.value || '""';
45 const cls = node.value ? "text-hl-string" : "text-hl-empty";
46 return newNode(ctx, "value", id).setText(text).addClass(cls);
47 } else {
48 return newNode(ctx, "value", id).setText(getRawValue(node)!).addClass(`text-hl-${type}`);
49 }
50}
51
52/**
53 * Creates a `TableNode` for a JSON array. This is one of the most complex parts of the builder,

Callers 3

buildTableGridFunction · 0.90
createArrayNodeFunction · 0.85
createObjectNodeFunction · 0.85

Calls 8

isIterableFunction · 0.90
hasChildrenFunction · 0.90
getRawValueFunction · 0.90
newNodeFunction · 0.85
addClassMethod · 0.80
setTextMethod · 0.80
isRootMethod · 0.80
setIdMethod · 0.80

Tested by

no test coverage detected