utils.ts

Overview

The utils.ts file provides utility functions and constants to generate and manage image resource paths used throughout the application. It primarily focuses on creating arrays of image names with consistent naming patterns, organized by categories such as book, laws, manual, and others. This helps maintain a centralized and standardized way of referencing image assets related to different topics or components.

Detailed Explanation

Function: getImageName

const getImageName = (prefix: string, length: number) => 
  new Array(length)
    .fill(0)
    .map((x, idx) => `chunk-method/${prefix}-0${idx + 1}`);

Purpose

Generates an array of image file name strings following a specific naming convention.

Parameters

Returns

Usage Example

const bookImages = getImageName('book', 4);
console.log(bookImages);
// Output:
// [
//   "chunk-method/book-01",
//   "chunk-method/book-02",
//   "chunk-method/book-03",
//   "chunk-method/book-04"
// ]

Implementation Details


Constant: ImageMap

export const ImageMap = {
  book: getImageName('book', 4),
  laws: getImageName('law', 2),
  manual: getImageName('manual', 4),
  picture: getImageName('media', 2),
  naive: getImageName('naive', 2),
  paper: getImageName('paper', 2),
  presentation: getImageName('presentation', 2),
  qa: getImageName('qa', 2),
  resume: getImageName('resume', 2),
  table: getImageName('table', 2),
  one: getImageName('one', 2),
  knowledge_graph: getImageName('knowledge-graph', 2),
  tag: getImageName('tag', 2),
};

Purpose

Provides a centralized mapping of categories to their respective arrays of image names.

Structure

Usage Example

import { ImageMap } from './utils';

console.log(ImageMap.book);
// [
//   "chunk-method/book-01",
//   "chunk-method/book-02",
//   "chunk-method/book-03",
//   "chunk-method/book-04"
// ]

console.log(ImageMap.laws);
// [
//   "chunk-method/law-01",
//   "chunk-method/law-02"
// ]

Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram

flowchart TD
    A[getImageName(prefix: string, length: number)] --> B[Array of image names]
    C[ImageMap Object] -->|calls| A
    C --> D[book: string[]]
    C --> E[laws: string[]]
    C --> F[manual: string[]]
    C --> G[picture: string[]]
    C --> H[naive: string[]]
    C --> I[paper: string[]]
    C --> J[presentation: string[]]
    C --> K[qa: string[]]
    C --> L[resume: string[]]
    C --> M[table: string[]]
    C --> N[one: string[]]
    C --> O[knowledge_graph: string[]]
    C --> P[tag: string[]]

Summary

The utils.ts file provides a small but essential utility function to generate standardized image names and organizes them into a convenient mapping object. This design promotes consistent asset referencing across the application, reducing errors and simplifying maintenance of image-related data.