UNPKG

guardz

Version:

A simple and lightweight TypeScript type guard library for runtime type validation.

50 lines (49 loc) 1.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createSimplifiedTree = void 0; const simplifyChildNode = (tree) => { if (tree.children && Object.keys(tree.children).length > 0) { const value = {}; Object.entries(tree.children).forEach(([key, childTree]) => { value[key] = simplifyChildNode(childTree); }); return { valid: tree.valid, value, ...(tree.expectedType && { expectedType: tree.expectedType }), }; } return { valid: tree.valid, value: tree.actualValue, ...(tree.expectedType && { expectedType: tree.expectedType }), }; }; /** * Create a simplified flat tree structure for JSON output * @param tree - The validation tree to simplify * @returns A simplified tree structure as a plain object */ const createSimplifiedTree = (tree) => { const rootKey = tree.path.split('.').pop() || 'root'; const result = {}; if (tree.children && Object.keys(tree.children).length) { const value = {}; Object.entries(tree.children).forEach(([key, childTree]) => { value[key] = simplifyChildNode(childTree); }); result[rootKey] = { valid: tree.valid, value, }; } else { result[rootKey] = { valid: tree.valid, value: tree.actualValue, ...(tree.expectedType && { expectedType: tree.expectedType }), }; } return result; }; exports.createSimplifiedTree = createSimplifiedTree;