utils.ts

Overview

The utils.ts file provides utility functions for converting between two common data representations related to tagged frequency data:

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;
};

Functions

transformTagFeaturesArrayToObject

export function transformTagFeaturesArrayToObject(
  list: Array<FormListItem> = [],
): Record<string, number>
const arrayData = [
  { tag: "apple", frequency: 4 },
  { tag: "banana", frequency: 2 },
];

const objData = transformTagFeaturesArrayToObject(arrayData);
// Result: { apple: 4, banana: 2 }

transformTagFeaturesObjectToArray

export function transformTagFeaturesObjectToArray(
  object: Record<string, number> = {},
): Array<FormListItem>
const objData = { apple: 4, banana: 2 };

const arrayData = transformTagFeaturesObjectToArray(objData);
// Result: [
//   { tag: "apple", frequency: 4 },
//   { tag: "banana", frequency: 2 }
// ]

Important Implementation Notes


Interaction with Other Parts of the System


Diagram: Function Workflow in utils.ts

flowchart TD
    A[Array<FormListItem>] -->|transformTagFeaturesArrayToObject| B[Record<string, number>]
    B -->|transformTagFeaturesObjectToArray| A

Summary

This utility file is a straightforward yet essential helper in managing tag-frequency datasets consistently across different layers of an application.