(ctx: CreateContext, tree: Tree, node: Node)
| 18 | * @param node The JSON `Node` to process. |
| 19 | */ |
| 20 | export 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, |
no test coverage detected