@avaelus/super-project-utils
v1.1.5
Published
Shared utility library
Downloads
12
Readme
Documentation for Code Module
Introduction
This documentation describes a TypeScript module designed to facilitate mapping and configuration handling for routing configurations in a web application context. The module includes utility functions and classes.
Functions and Classes
1. mapValuesAsKeys
export function mapValuesAsKeys<
T extends Record<string, string | { _: string }>,
RequiredKeys extends keyof T
>(source: T): Record<string, keyof T & RequiredKeys> {
// Function logic described in the implementation
}
- Description: This function maps values of an object (
source
) to keys, handling both string values directly and objects with a_
property as keys. - Parameters:
source
: An object where values can be strings or objects with a_
property.
- Returns: A new object where keys are values from
source
and values are keys fromsource
corresponding to those values. - Usage: Used in various parts of the module to convert configurations and blueprints into key-value mappings.
2. mapRouteToValue
export function mapRouteToValue(
routes: Record<string, string | { _: string }>,
replaceValue = ""
) {
// Function logic described in the implementation
}
- Description: This function maps routes to a specified
replaceValue
, based on the mappings generated bymapValuesAsKeys
. - Parameters:
routes
: An object where values can be strings or objects with a_
property.replaceValue
: Optional parameter to replace mapped values.
- Returns: A cloned object with route values replaced by
replaceValue
. - Usage: Primarily used to generate configurations or mappings with default values.
3. routeEnum
export function routeEnum<T>(obj: T) {
// Function logic described in the implementation
}
- Description: This function flattens nested objects and arrays into an enumeration of key-value pairs.
- Parameters:
obj
: The object or array to be flattened.
- Returns: An object where keys are string representations of values found
within
obj
. - Usage: Used to simplify nested structures into a flat list of values, often used in configuration mapping.
4. RoutesConfig
Class
export class RoutesConfig<r, config> {
// Class implementation described in the provided code
}
- Description: This class generates a configuration map (
configMap
) based on provided route blueprints (routes
). - Properties:
routes
: Contains the route blueprints used to generate the configuration.configMap
: Final mapping of routes to their configurations.rEnum
: Enumerated values derived fromroutes
.
- Methods:
constructor
: Initializes the class and populatesconfigMap
based onroutes
and optional configuration (c
).for
: Retrieves configuration for a specified route path (path
).hydrate
: Replaces placeholders in a route path with corresponding parameters.
- Usage: Essential for managing and retrieving configurations based on predefined blueprints.
5. RoutesConfigFromBluePrints
Class
export class RoutesConfigFromBluePrints<bp, config> {
// Class implementation described in the provided code
}
- Description: This class extends
RoutesConfig
and initializes it using blueprint configurations (blueprints
). - Properties:
blueprints
: Blueprint configurations for routes.routes
: Generated routes frommMap(blueprints)
.configMap
: Configuration mapping similar toRoutesConfig
.
- Methods:
constructor
: InitializesRoutesConfig
using blueprint configurations.
- Usage: Simplifies the setup of
RoutesConfig
instances using predefined blueprints.
Testing
- Framework: The module is tested using
chai
andmocha
, ensuring functionalities likerouteEnum
andmapValuesAsKeys
behave as expected under different scenarios. - Purpose: Tests validate correct behavior across nested structures, empty inputs, and mixed data types, ensuring reliability and accuracy in a TypeScript environment.
Conclusion
This documentation provides an overview of the module's functionalities, utility
functions (mapValuesAsKeys
, mapRouteToValue
, routeEnum
), and classes
(RoutesConfig
, RoutesConfigFromBluePrints
). It outlines how each component
contributes to handling routing configurations and mappings within a TypeScript
application, ensuring clarity and ease of use for developers integrating the
module.