How apteryxxyz/enhanced-ms works
Main components
• API Entry Point: Represent the main “ms” function (exported via src/index.ts) that users call. This function accepts either a number (to format) or a string (to parse).
• Factory Module: (src/factory.ts) which provides the createMs function to generate instances of the ms utility with custom language configurations and default options.
• Formatting Module: (located in src/format) that handles converting milliseconds into a formatted, human-readable duration. Within this, include:
• Helper Files: format-unit.ts, parse-milliseconds.ts, resolve-options.ts – these modules encapsulate logic for unit formatting, value parsing, and option resolution.
• Parsing Module: (located in src/parse) that parses duration strings back into milliseconds.
• Language Support Module: (in src/languages) that holds available language files (en, de, es, fr, it, mi, nl, ru) plus helper utilities (in src/languages/helpers) for building language definitions.
• Time Utility: (src/time.ts) which may encompass additional time/duration related computations.
• Test Suite: Although not part of the runtime architecture, note the tests in the tests directory that verify functionality across different languages.
Relationships and Interactions
• Data Flow:
• The user calls the ms function (entry point). Depending on the argument type, the ms function either directs the call to the formatting module (for number inputs) or to the parsing module (for string inputs).
• When formatting durations, the ms function may consult the factory module to apply custom configuration (language, formatOptions) and then delegate the task to the Format module.
• Inside the formatting module, helper functions take charge of resolving options and formatting each unit appropriately.
• When parsing duration strings, the parsing module processes the string into its numeric millisecond value.
• Throughout both processes, the Language module provides localized unit names and definitions that tailor the output to the user's language choice.
• Internal Dependencies:
• The factory module connects the overall configuration with both formatting and parsing, ensuring correct defaults and language rules are applied.
• The language helper utilities abstract language-specific definitions so that the formatting engine remains decoupled from the actual language details.
Important Architectural Patterns
• Modularity: The project exhibits a clear separation of concerns where formatting, parsing, language support, and factory configuration are encapsulated in separate modules.
• Extensibility: By isolating language definitions and helper functions, the project allows additional languages and more complex formatting options to be integrated without altering the core logic.
• Reusability: Helper modules (for parsing milliseconds or formatting units) ensure that common functionalities can be maintained and reused across the system.
Relevant Technologies
• Typescript: Noted by the .ts file extensions and explicit type definitions across the modules.
• Node Package Manager: Clearly seen through package.json, pnpm-lock.yaml, and the installation instructions.