Internal architecture and reasoning behind this date formatting library.
-
Token-to-token conversion only
-
No Date objects
- Avoids
new Date()andIntl - No built-in timezone logic
- Avoids
-
No dependencies — designed for embeddable, zero-cost builds
-
Predictable behavior:
- Parsing is greedy, literal-safe, and unambiguous
- Rendering uses a fixed resolution hierarchy
Each module follows single-responsibility principles:
| Module | Role |
|---|---|
extractTokens |
Slices inputDate using tokens from inputFormat |
normalizeFields |
Maps raw values to year, month, etc. + converter |
validateOutput |
Checks all output tokens are satisfiable |
buildTemplate |
Prepares reusable formatter chunks |
formatter |
Glue layer — manages control flow + options handling |
- Default strict mode
- Fails on unknown tokens, missing inputs, or invalid configurations
-
Returns best-effort formatting
-
Prioritizes:
overrideTokens- parsed input
defaultTokens- literal fallback (token name)
'warn': log errors, continue formatting'coerce': use fallback/defaults automatically, no literal leakage
This library supports full per-call token customization:
| Feature | Purpose | Priority |
|---|---|---|
customTokens |
Add new renderable tokens | Low |
overrideTokens |
Force output (overrides input) | Highest |
defaultTokens |
Fill missing values | Conditional |
No global registration. You control all tokens through options.
- No timezone shifts or offset parsing
- No locale-based formatting or pluralization
- No calendar correctness (unless enabled)
enableDateValidation: check for leap years, invalid dates, etc.- TS typings: native
.d.tssupport for options and fields - Token aliases: support mapping
MMM→monorDo→dth - Grouping output templates for formatting reuse
This design ensures maximum control, low surface area, and clear extension paths for any future need.