Skip to content

Add locale-aware date formatter - #157

Open
TrySound wants to merge 1 commit into
mainfrom
add-date-formatter
Open

Add locale-aware date formatter#157
TrySound wants to merge 1 commit into
mainfrom
add-date-formatter

Conversation

@TrySound

@TrySound TrySound commented Sep 4, 2026

Copy link
Copy Markdown
Owner

Summary

  • add createDateFormatter as a dependency-free rifm/date entry point
  • derive numeric date field order and separators from Intl.DateTimeFormat.formatToParts()
  • support locale fallback lists and year: "short" | "long" (long by default)
  • preserve partial editable input, enforce fixed two-digit day/month fields, and limit the year to two or four digits
  • use the Gregorian calendar and Latin digits for predictable digit-based caret tracking
  • support both insertion-style formatting and RIFM's existing replacement-style editing via mask: true
  • document usage and the deliberate absence of calendar-date validation

Examples

createDateFormatter({ locales: "en-US", year: "long" }).format("12082026");
// "12/08/2026"

createDateFormatter({ locales: "de-DE", year: "long" }).format("12082026");
// "12.08.2026"

createDateFormatter({ locales: "en-GB", year: "short" }).format("120826");
// "12/08/26"

The returned { format, accept } object can be spread into RIFM options. Adding mask: true enables replacement-style editing of occupied date positions; leaving it out keeps normal insertion behavior.

Size

Measured from the generated dist/date.js entry with Rolldown minification and gzip level 9:

  • 705 bytes minified
  • 480 bytes minified + gzip

For comparison, the number formatter measured 780 bytes minified and 489 bytes minified + gzip with the same process.

Testing

  • pnpm test:ts
  • pnpm test:unit
  • pnpm test:browser
  • pnpm build
  • pnpm exec oxfmt --check README.md package.json vitest.config.mts src/date.ts tests/date-formatter.test.ts tests/date-formatter.browser.test.ts

Unit coverage includes locale ordering/separators, partial and excessive input, idempotence, short years, and accepted characters. Browser coverage verifies replacement-style date mask editing and caret behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant