utils.ts
Overview
The utils.ts file provides utility functions for converting between two common data representations related to tagged frequency data:
An array of objects, where each object (
FormListItem) contains atagstring and its associatedfrequencynumber.An object (dictionary) mapping tag strings to their frequency numbers.
This file defines a type to represent the array items and two transformation functions that convert data from one format to the other. These utilities are useful in scenarios where data needs to be manipulated, stored, or transmitted in different formats depending on the context, such as form data processing, API communication, or internal state management.
Types
FormListItem
export type FormListItem = {
frequency: number;
tag: string;
};
Description: Represents a single tagged frequency item.
Properties:
frequency: A number indicating how often the tag appears or is relevant.tag: A string identifier associated with the frequency.
Functions
transformTagFeaturesArrayToObject
export function transformTagFeaturesArrayToObject(
list: Array<FormListItem> = [],
): Record<string, number>
Purpose: Converts an array of
FormListItemobjects into an object mapping tags to their frequencies.Parameters:
list: An array ofFormListItem. Defaults to an empty array if not provided.
Returns: An object where each key is a tag string, and its value is the corresponding frequency number.
Usage example:
const arrayData = [
{ tag: "apple", frequency: 4 },
{ tag: "banana", frequency: 2 },
];
const objData = transformTagFeaturesArrayToObject(arrayData);
// Result: { apple: 4, banana: 2 }
Implementation Details:
Uses
Array.prototype.reduceto iterate over the array.Accumulates entries into a new object by setting
pre[cur.tag] = cur.frequency.Returns the fully constructed object.
transformTagFeaturesObjectToArray
export function transformTagFeaturesObjectToArray(
object: Record<string, number> = {},
): Array<FormListItem>
Purpose: Converts an object mapping tags to frequencies into an array of
FormListItemobjects.Parameters:
object: An object where keys are tag strings and values are frequency numbers. Defaults to an empty object.
Returns: An array of
FormListItem, each representing one tag-frequency pair.Usage example:
const objData = { apple: 4, banana: 2 };
const arrayData = transformTagFeaturesObjectToArray(objData);
// Result: [
// { tag: "apple", frequency: 4 },
// { tag: "banana", frequency: 2 }
// ]
Implementation Details:
Uses
Object.keysto get all tags.Uses
Array.prototype.reduceto build an array by pushing a newFormListItemfor each key.Returns the constructed array.
Important Implementation Notes
Both functions use default parameters to allow safe calls with omitted arguments.
The conversions are lossless and inverse operations of each other:
transformTagFeaturesArrayToObject(transformTagFeaturesObjectToArray(obj))returns the originalobj.transformTagFeaturesObjectToArray(transformTagFeaturesArrayToObject(arr))returns the originalarr(assuming no duplicate tags).
The functions assume tags are unique within the array input. Duplicate tags in the array will result in the last occurrence overwriting previous frequencies in the object output.
Interaction with Other Parts of the System
These utilities can be employed wherever frequency-tag mappings need to be converted between formats, for example:
User interface components that collect or display tag-frequency pairs as arrays.
Backend or API layers that expect or return data in object form for easier serialization.
State management where objects are preferred for quick lookup by tag.
Since this is a utility module, it is likely imported and used by other modules handling tag data transformations or form processing.
Diagram: Function Workflow in utils.ts
flowchart TD
A[Array<FormListItem>] -->|transformTagFeaturesArrayToObject| B[Record<string, number>]
B -->|transformTagFeaturesObjectToArray| A
This flowchart illustrates the two primary functions and their inverse relationship converting between array and object representations.
Summary
File Purpose: Provide type and two utility functions for converting between array and object representations of tag-frequency data.
Key Types:
FormListItem(tag + frequency).Key Functions:
transformTagFeaturesArrayToObject(array → object)transformTagFeaturesObjectToArray(object → array)
Use Cases: Data transformation for forms, APIs, and internal data handling.
Code Approach: Simple, clean use of JavaScript built-in methods (
reduce,Object.keys) with safe defaults.
This utility file is a straightforward yet essential helper in managing tag-frequency datasets consistently across different layers of an application.