Skip to content

Add year removal support#113

Open
ligiapopescu wants to merge 4 commits intomicrosoft:masterfrom
ligiapopescu:feature/omit-year-removal
Open

Add year removal support#113
ligiapopescu wants to merge 4 commits intomicrosoft:masterfrom
ligiapopescu:feature/omit-year-removal

Conversation

@ligiapopescu
Copy link
Collaborator

@ligiapopescu ligiapopescu commented Mar 6, 2026

Summary

  • add optional omitYear formatting option to DateTimeFormatter.formatDateTime
  • add internal year-removal helper for locale-aware OS mask cleanup
  • preserve explicit WITH_YEAR behavior in OS switch handling
  • add direct year-removal unit tests and expand formatter tests
  • document the new API usage in README

Validation

  • yarn lint
  • yarn test
  • yarn build

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>
@ligiapopescu ligiapopescu requested a review from a team as a code owner March 6, 2026 10:08
Copilot AI review requested due to automatic review settings March 6, 2026 10:08
@ligiapopescu ligiapopescu changed the title Add omitYear support and year-removal tests Add year removal support Mar 6, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 DateTimeFormatter to accept an omitYear option 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.

Comment on lines +155 to +158
const d = this.formatter.dateToString(
date,
this.getDateMask(localeInfo.shortDate, loc, omitYear)
);
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copilot uses AI. Check for mistakes.
Comment on lines +63 to +68
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, "");
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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);

Copilot uses AI. Check for mistakes.
Comment on lines 93 to 98
public formatOsDateTime(
date: number | Date,
format: DateTimeFormatOptions,
localeInfo: ILocaleInfo
localeInfo: ILocaleInfo,
options?: { omitYear?: boolean }
): string {
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
ligiapopescu and others added 3 commits March 6, 2026 12:24
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>
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.

2 participants