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

Returns

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

This approach ensures immutability by creating new objects rather than mutating the input.

Interaction With Other Parts of The System

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.