util.ts


Overview

This utility file provides functionality centered around generating unique keys for grouping nodes, classifying nodes into combos based on relational edges, and verifying the existence of certain data structures. It primarily includes:

These utilities are useful in graph-related applications where nodes need to be categorized or grouped dynamically based on their connections or metadata.


Detailed Documentation

Class: KeyGenerator

Description

Generates sequential alphabetical keys starting from 'a' to 'z'. Designed to be used for assigning unique identifiers or labels in a simple incremental fashion.

Properties

Constructor

constructor()

Initializes the chars array with 26 lowercase English letters and sets idx to 0.

Methods

generateKey(): string

Generates and returns the next alphabetical key in sequence.


Class: Converter

Description

Processes graph nodes and edges to classify nodes into groups ("combos") based on their edge relationships. It assigns a shared combo key to connected nodes.

Properties

Constructor

constructor()

Initializes a new KeyGenerator and creates an empty dictionary dict.

Methods

buildDict(edges: { source: string; target: string }[]): Record<string, string>

Assigns combo keys to nodes based on their edges.

buildNodesAndCombos(nodes: any[], edges: any[]): { nodes: any[]; combos: any[] }

Generates updated nodes with combo assignments and a list of unique combo objects.


Function: isDataExist

Description

Checks if the given data object contains a non-boolean data property and a non-empty graph property inside data.

Signature

export const isDataExist = (data: any) => boolean;

Parameters

Returns

Usage Example

const data = { data: { graph: { nodes: [] } } };
console.log(isDataExist(data)); // true

Implementation Details

Uses Lodash's isEmpty to check if graph is empty.


Function: buildNodesAndCombos

Description

Creates combo groupings for nodes based on their communities property. Each unique community label results in a combo, and nodes are assigned the corresponding combo ID.

Signature

export const buildNodesAndCombos = (nodes: any[]) => { nodes: any[]; combos: any[] };

Parameters

Returns

Behavior

Usage Example

const nodes = [
  { id: '1', communities: ['group1'] },
  { id: '2', communities: ['group2'] },
  { id: '3', communities: ['group1'] },
];
const { nodes: updatedNodes, combos } = buildNodesAndCombos(nodes);
console.log(combos);
// [{ isCombo: true, id: 'uuid-1', data: { label: 'group1' } }, ...]
console.log(updatedNodes);
// Nodes with 'combo' property referencing combo IDs

Helper Function: findCombo


Implementation Details & Algorithms


Interaction with Other System Parts


Visual Diagram: Function Flowchart

flowchart TD
    A[Start] --> B[isDataExist(data)]
    B -- true --> C[buildNodesAndCombos(nodes)]
    C --> D[Return nodes with combo IDs and combos]
    B -- false --> E[Return false or skip processing]

    subgraph Converter
      F[Converter Instance]
      F --> G[buildDict(edges)]
      G --> H[Assign combos to nodes]
      H --> I[buildNodesAndCombos(nodes, edges)]
      I --> J[Return nodes with combos and combo list]
    end

    style Converter fill:#f9f,stroke:#333,stroke-width:1px

Summary

util.ts provides utilities to generate keys, classify graph nodes into combos based on edges or community membership, and validate data structures. It supports graph processing workflows by grouping nodes for better visualization or analysis. The file is self-contained but relies on lodash and uuid libraries, and its output is designed to integrate with UI or graph modules that consume node and combo data.