utils.ts
Overview
The utils.ts file provides a utility function designed to transform a structured object into a list of query input objects. Specifically, it converts a dictionary-like object, where each key maps to a partial BeginQuery object (missing the key property), into an array of complete BeginQuery objects with the key property included.
This transformation is useful in scenarios where query parameters or configurations are initially stored in an object for easy access by key, but later need to be processed as a list of query inputs—such as for batch processing, validation, or passing to APIs expecting arrays.
Detailed Explanation
Function: buildBeginInputListFromObject
export function buildBeginInputListFromObject(
inputs: Record<string, Omit<BeginQuery, 'key'>>,
): BeginQuery[]
Purpose
Transforms an object of input fragments into an array of complete BeginQuery objects by injecting the corresponding object key as the key property in each.
Parameters
inputs:Record<string, Omit<BeginQuery, 'key'>>
An object where each property key is a string and the value is a partialBeginQueryobject missing thekeyproperty.The keys represent unique identifiers or names for each query input.
The values contain the rest of the properties expected in a
BeginQuery.
Returns
BeginQuery[]
An array ofBeginQueryobjects, each constructed by merging the original partial object with its key added as thekeyproperty.
Usage Example
import { buildBeginInputListFromObject } from './utils';
interface BeginQuery {
key: string;
param1: string;
param2?: number;
}
// Example input object missing the 'key' property in values
const inputObject = {
queryA: { param1: 'value1', param2: 10 },
queryB: { param1: 'value2' },
};
const inputList = buildBeginInputListFromObject(inputObject);
/* Result:
[
{ key: 'queryA', param1: 'value1', param2: 10 },
{ key: 'queryB', param1: 'value2' }
]
*/
Implementation Details
The function uses
Object.entries()to iterate over the input object entries.It applies
Array.prototype.reduce()to accumulate the output array.For each
[key, value]pair:It creates a new object by spreading the
valueproperties.It adds the
keyproperty.Pushes this new object into the accumulator array.
If the
inputsparameter isnullorundefined, it safely defaults to an empty object{}, resulting in an empty array output.
This approach ensures immutability by creating new objects rather than mutating the input.
Interaction With Other Parts of The System
The file imports the
BeginQueryinterface from'../../interface', indicating that this utility depends on a shared interface definition located elsewhere in the project.This function likely serves as a helper in modules that require converting keyed input maps into lists for processing queries or batch operations.
It does not modify external state and can be safely used wherever such transformation is necessary.
Visual Diagram
Below is a flowchart illustrating the data transformation process inside the buildBeginInputListFromObject utility function:
flowchart TD
A[Input: inputs object<br/>{ key: partial BeginQuery }] --> B[Object.entries(inputs)]
B --> C[Array of [key, partial BeginQuery] tuples]
C --> D[reduce over tuples]
D --> E[For each tuple]
E --> F[Create new BeginQuery object:<br/> {...value, key }]
F --> G[Push to accumulator array]
G --> D
D --> H[Output: BeginQuery[] array]
This completes the documentation for utils.ts. The file is concise, containing a single, clear utility function that converts keyed objects into lists while injecting keys into each item.