build-output-list.ts
Overview
The build-output-list.ts file contains a utility function designed to transform a complex nested object of outputs into a simplified array of output descriptors. Specifically, it takes an input object where each key corresponds to an output name and its associated metadata, and returns an array of objects with a concise structure containing only the output title and its type. This transformation facilitates easier consumption and rendering of output data, especially within UI components that require a list format.
Detailed Explanation
Function: buildOutputList
export function buildOutputList(outputs: Record<string, Record<string, any>>): OutputType[]
Purpose
Transforms an object containing output metadata into an array of simplified output descriptor objects.
Parameters
outputs:Record<string, Record<string, any>>
A nested object where each key is a string representing the output's name, and its value is another object containing arbitrary metadata about that output, including a mandatorytypeproperty.Example input structure:
{ outputName1: { type: 'string', otherMetadata: ... }, outputName2: { type: 'number', otherMetadata: ... }, ... }
Return Value
OutputType[]
An array of objects, each containing:title: The original key from the input object, representing the output name.type: The type of the output, extracted from the nested object.
Example output:
[ { title: 'outputName1', type: 'string' }, { title: 'outputName2', type: 'number' }, ... ]
Usage Example
import { buildOutputList } from './build-output-list';
const outputs = {
result: { type: 'boolean', description: 'Success flag' },
data: { type: 'object', fields: ['id', 'value'] },
};
const outputList = buildOutputList(outputs);
/*
outputList will be:
[
{ title: 'result', type: 'boolean' },
{ title: 'data', type: 'object' }
]
*/
Implementation Details
The function uses
Object.entriesto iterate over the key-value pairs in the input object.It applies
Array.prototype.reduceto accumulate an array ofOutputTypeobjects.For each entry, it extracts the key as
titleand the nestedtypeproperty astype.This approach ensures immutability by not modifying the input and constructing a new array.
Important Implementation Notes
The function assumes that each nested object in
outputscontains atypeproperty. Iftypeis missing, the resulting output will havetypeasundefined.The function is generic enough to handle any nested metadata but only extracts
typefor the returned list.The return type
OutputType[]is imported from'../form/components/output'. This implies thatOutputTypeis a defined interface or type in the project that has at leasttitleandtypeproperties.
Interaction with Other Parts of the System
Imports
OutputTypefrom a form-related component module ('../form/components/output'), indicating that this function plays a role in preparing data for UI form components or output-related display components.Likely used in scenarios where output data from a process, API, or calculation needs to be summarized or presented in a form or UI list.
By converting a complex object into a uniform list structure, it facilitates rendering components that expect an array of outputs with titles and types.
Visual Diagram
flowchart TD
A[buildOutputList(outputs)] --> B[Object.entries(outputs)]
B --> C[Array of [key, value] pairs]
C --> D[Reduce over entries]
D --> E[For each entry]
E --> F[Extract key as title]
E --> G[Extract value.type as type]
F & G --> H[Push {title, type} into accumulator array]
H --> I[Return OutputType[] array]
Summary
File purpose: Utility to convert output metadata objects into a list of simplified output descriptors.
Main export:
buildOutputListfunction.Input: Object mapping output names to metadata.
Output: Array of { title, type } objects.
Usage: Primarily for preparing output data for UI components.
Dependencies: Relies on
OutputTypefrom a form component module.
This file is a small but crucial helper in transforming backend or processing outputs into a front-end consumable format, enabling consistent output presentation across the application.