Conversation
Implement omitYear-aware formatting in DateTimeFormatter, add internal year-removal helper and dedicated tests, and document the API update in README. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds an omitYear formatting option to allow locale-aware year removal from date masks and Intl formatting, with supporting helper logic, tests, and documentation.
Changes:
- Introduced
removeYearFromMask()with locale-aware dot-separator handling. - Extended
DateTimeFormatterto accept anomitYearoption across Intl and OS formatting paths. - Added targeted unit tests for year removal and expanded formatter tests, plus README usage docs.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/year-removal.ts | Adds the locale-aware mask year-removal helper and locale dot-preservation rules. |
| src/year-removal.test.ts | Adds direct unit coverage for year-removal behavior across locales. |
| src/date-time-formatter.ts | Threads omitYear through formatting paths and applies year-stripping logic. |
| src/date-time-formatter.test.ts | Adds integration tests verifying omitYear behavior for OS/Intl paths. |
| README.md | Documents the new options.omitYear API parameter. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
| const d = this.formatter.dateToString( | ||
| date, | ||
| this.getDateMask(localeInfo.shortDate, loc, omitYear) | ||
| ); |
There was a problem hiding this comment.
omitYearis currently applied toSHORT_DATE_WITH_SHORT_YEAR/SHORT_DATE_WITH_YEARin the OS-formatting switch viagetDateMask(..., omitYear), but the new tests assert that WITH_YEAR OS formats must *not* omit the year. To match the intended behavior, ignore omitYearfor these WITH_YEAR cases (e.g., passfalsetogetDateMaskhere, or bypassgetDateMaskand uselocaleInfo.shortDate` directly for these formats).
src/year-removal.ts
Outdated
| export function removeYearFromMask(mask: string, localeCode: string): string { | ||
| // Locales requiring dot-after-month use a regex variant that does not strip leading dots. | ||
| const regex = doesLocaleRequireDotAfterMonth(localeCode) | ||
| ? NO_YEAR_REGEX | ||
| : NO_YEAR_REGEX_REMOVE_DOT_AFTER_MONTH; | ||
| return mask.replace(regex, ""); |
There was a problem hiding this comment.
removeYearFromMask()can return masks with leading/trailing whitespace artifacts (and sometimes doubled spaces) after removal (as evidenced by several unit expectations like"d. MMMM "/"d MMMM "/" d. MMMM"`). This makes the helper output inconsistently “valid” depending on how downstream formatters treat whitespace. Consider normalizing the result (e.g., trim and/or collapse repeated spaces, and remove leftover separator-adjacent whitespace) so the returned mask is stable and doesn’t depend on formatter-specific trimming behavior.
| export function removeYearFromMask(mask: string, localeCode: string): string { | |
| // Locales requiring dot-after-month use a regex variant that does not strip leading dots. | |
| const regex = doesLocaleRequireDotAfterMonth(localeCode) | |
| ? NO_YEAR_REGEX | |
| : NO_YEAR_REGEX_REMOVE_DOT_AFTER_MONTH; | |
| return mask.replace(regex, ""); | |
| function normalizeMaskWhitespace(mask: string): string { | |
| // Remove whitespace immediately around common separator characters. | |
| let result = mask.replace(/\s*([.,\/-])\s*/g, "$1"); | |
| // Collapse remaining consecutive whitespace to a single space and trim ends. | |
| result = result.replace(/\s+/g, " ").trim(); | |
| return result; | |
| } | |
| export function removeYearFromMask(mask: string, localeCode: string): string { | |
| // Locales requiring dot-after-month use a regex variant that does not strip leading dots. | |
| const regex = doesLocaleRequireDotAfterMonth(localeCode) | |
| ? NO_YEAR_REGEX | |
| : NO_YEAR_REGEX_REMOVE_DOT_AFTER_MONTH; | |
| const withoutYear = mask.replace(regex, ""); | |
| return normalizeMaskWhitespace(withoutYear); |
| public formatOsDateTime( | ||
| date: number | Date, | ||
| format: DateTimeFormatOptions, | ||
| localeInfo: ILocaleInfo | ||
| localeInfo: ILocaleInfo, | ||
| options?: { omitYear?: boolean } | ||
| ): string { |
There was a problem hiding this comment.
The inline options?: { omitYear?: boolean } type is duplicated across public APIs. To keep the API consistent and easier to extend (e.g., if more formatting flags are added later), consider introducing a named exported type/interface (e.g., DateTimeFormatterOptions) and reusing it in both method signatures and documentation examples.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Rename the formatting behavior options type for clarity and normalize whitespace artifacts in year-removal masks, with updated tests and docs. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Summary
omitYearformatting option toDateTimeFormatter.formatDateTimeyear-removalhelper for locale-aware OS mask cleanupyear-removalunit tests and expand formatter testsValidation