utils.ts
Overview
The utils.ts file contains utility functions that facilitate data transformation related to the BeginQuery interface. Its primary purpose is to convert an object with keyed entries into an array of BeginQuery objects, each augmented with its corresponding key. This transformation is useful for cases where an object-based representation needs to be converted into a list format, which is often easier to iterate over or process in workflows such as querying, UI rendering, or data manipulation.
Functions
buildBeginInputListFromObject
function buildBeginInputListFromObject(
inputs: Record<string, Omit<BeginQuery, 'key'>>
): BeginQuery[]
Description
Transforms an object whose keys are strings and values are partial BeginQuery objects (without the key property) into an array of complete BeginQuery objects, each including the original key as the key property.
Parameters
inputs: Record<string, Omit<BeginQuery, 'key'>>An object where each property key corresponds to a unique identifier and its value is a partial
BeginQueryobject missing thekeyproperty.
Returns
BeginQuery[]
An array ofBeginQueryobjects, where each object includes all original properties from the input value plus thekeyproperty set to the respective key from the input object.
Usage Example
import { buildBeginInputListFromObject } from './utils';
import { BeginQuery } from '../../interface';
const inputObject = {
first: { param1: 'value1', param2: 123 },
second: { param1: 'value2', param2: 456 },
};
const result = buildBeginInputListFromObject(inputObject);
/*
result = [
{ key: 'first', param1: 'value1', param2: 123 },
{ key: 'second', param1: 'value2', param2: 456 }
]
*/
Implementation Details
Uses
Object.entries()to iterate over the input object's keys and values.Utilizes
Array.prototype.reduce()to accumulate an array ofBeginQueryobjects.For each entry, it spreads the properties of the value and adds the
keyproperty, which corresponds to the original key in the input object.Handles
undefinedornullinputs gracefully by defaulting to an empty array.
Interaction with Other Parts of the System
Dependency: Imports the
BeginQueryinterface from../../interface, which presumably defines the structure of theBeginQuerytype.Usage Context: This utility function is likely used wherever there is a need to convert keyed objects into arrays of queries, potentially for API requests, querying databases, or rendering UI components that expect an array format.
It serves as a bridge between object-based data structures and array-based processing flows.
Mermaid Diagram
flowchart TD
A[buildBeginInputListFromObject] --> B[inputs: Record<string, Omit<BeginQuery, 'key'>>]
A --> C[returns: BeginQuery[]]
B --> D{Object.entries(inputs)}
D --> E[Iterate over [key, value]]
E --> F[Create BeginQuery object with key]
F --> G[Push to results array]
G --> C
Summary
The utils.ts file is a focused utility module that provides a single, well-defined function for transforming an object of partial BeginQuery entries into a complete array of BeginQuery objects. This transformation is essential for workflows that require list-based data manipulation and enhances type safety and consistency across the system by enforcing inclusion of the key property.