fix(settings): format the date format files prettier flagged

This commit is contained in:
Maurice
2026-11-03 22:13:14 +00:00
parent 154620b99a
commit 65e5d4ab72
12 changed files with 250 additions and 200 deletions
+139 -106
View File
@@ -1,5 +1,5 @@
// FE-UTIL-DATEFMT-001 to FE-UTIL-DATEFMT-020
import { describe, it, expect } from 'vitest'
import { describe, expect, it } from 'vitest';
import {
dateFieldOrder,
dateFormatOptions,
@@ -8,12 +8,12 @@ import {
formatDisplayDate,
formatDisplayDateTime,
isNumericDate,
} from './dateFormat'
} from './dateFormat';
// 31 December 2026, 15:04 UTC: no day of it reads as a month, so every layout is unambiguous.
const NYE = new Date(Date.UTC(2026, 11, 31, 15, 4))
const UTC = { timeZone: 'UTC' } as const
const NUMERIC: Intl.DateTimeFormatOptions = { day: '2-digit', month: '2-digit', year: 'numeric', timeZone: 'UTC' }
const NYE = new Date(Date.UTC(2026, 11, 31, 15, 4));
const UTC = { timeZone: 'UTC' } as const;
const NUMERIC: Intl.DateTimeFormatOptions = { day: '2-digit', month: '2-digit', year: 'numeric', timeZone: 'UTC' };
describe('dateFormat (#2971)', () => {
describe('formatDisplayDate', () => {
@@ -24,47 +24,54 @@ describe('dateFormat (#2971)', () => {
['de-DE', NUMERIC],
['ko-KR', { day: 'numeric', month: 'short', year: 'numeric', timeZone: 'UTC' }],
['en-GB', { day: '2-digit', month: '2-digit', year: '2-digit', timeZone: 'UTC' }],
]
];
for (const [locale, opts] of cases) {
const expected = NYE.toLocaleDateString(locale, opts)
expect(formatDisplayDate(NYE, locale, opts, 'auto')).toBe(expected)
expect(formatDisplayDate(NYE, locale, opts, 'DD-MM-YY')).toBe(expected)
expect(formatDisplayDate(NYE, locale, opts, null)).toBe(expected)
expect(formatDisplayDate(NYE, locale, opts)).toBe(expected)
const expected = NYE.toLocaleDateString(locale, opts);
expect(formatDisplayDate(NYE, locale, opts, 'auto')).toBe(expected);
expect(formatDisplayDate(NYE, locale, opts, 'DD-MM-YY')).toBe(expected);
expect(formatDisplayDate(NYE, locale, opts, null)).toBe(expected);
expect(formatDisplayDate(NYE, locale, opts)).toBe(expected);
}
})
});
it('FE-UTIL-DATEFMT-002: each format writes 31 December 2026 in its own layout', () => {
expect(formatDisplayDate(NYE, 'en-US', UTC, 'YYYY-MM-DD')).toBe('2026-12-31')
expect(formatDisplayDate(NYE, 'en-US', UTC, 'YYYY/MM/DD')).toBe('2026/12/31')
expect(formatDisplayDate(NYE, 'en-US', UTC, 'DD.MM.YYYY')).toBe('31.12.2026')
expect(formatDisplayDate(NYE, 'en-US', UTC, 'DD/MM/YYYY')).toBe('31/12/2026')
expect(formatDisplayDate(NYE, 'de-DE', UTC, 'MM/DD/YYYY')).toBe('12/31/2026')
})
expect(formatDisplayDate(NYE, 'en-US', UTC, 'YYYY-MM-DD')).toBe('2026-12-31');
expect(formatDisplayDate(NYE, 'en-US', UTC, 'YYYY/MM/DD')).toBe('2026/12/31');
expect(formatDisplayDate(NYE, 'en-US', UTC, 'DD.MM.YYYY')).toBe('31.12.2026');
expect(formatDisplayDate(NYE, 'en-US', UTC, 'DD/MM/YYYY')).toBe('31/12/2026');
expect(formatDisplayDate(NYE, 'de-DE', UTC, 'MM/DD/YYYY')).toBe('12/31/2026');
});
it('FE-UTIL-DATEFMT-003: the language does not change a chosen format', () => {
for (const locale of ['en-US', 'de-DE', 'ko-KR', 'ar', 'fr-FR']) {
expect(formatDisplayDate(NYE, locale, NUMERIC, 'YYYY-MM-DD')).toBe('2026-12-31')
expect(formatDisplayDate(NYE, locale, NUMERIC, 'YYYY-MM-DD')).toBe('2026-12-31');
}
})
});
it('FE-UTIL-DATEFMT-004: single-digit days and months are padded', () => {
const march = new Date(Date.UTC(2026, 2, 5))
expect(formatDisplayDate(march, 'en-US', { day: 'numeric', month: 'numeric', year: 'numeric', timeZone: 'UTC' }, 'DD.MM.YYYY')).toBe('05.03.2026')
})
const march = new Date(Date.UTC(2026, 2, 5));
expect(
formatDisplayDate(
march,
'en-US',
{ day: 'numeric', month: 'numeric', year: 'numeric', timeZone: 'UTC' },
'DD.MM.YYYY'
)
).toBe('05.03.2026');
});
it('FE-UTIL-DATEFMT-005: year 2-digit keeps two digits of the year', () => {
const opts: Intl.DateTimeFormatOptions = { day: '2-digit', month: '2-digit', year: '2-digit', timeZone: 'UTC' }
expect(formatDisplayDate(NYE, 'en-US', opts, 'DD.MM.YYYY')).toBe('31.12.26')
expect(formatDisplayDate(NYE, 'en-US', opts, 'YYYY-MM-DD')).toBe('26-12-31')
})
const opts: Intl.DateTimeFormatOptions = { day: '2-digit', month: '2-digit', year: '2-digit', timeZone: 'UTC' };
expect(formatDisplayDate(NYE, 'en-US', opts, 'DD.MM.YYYY')).toBe('31.12.26');
expect(formatDisplayDate(NYE, 'en-US', opts, 'YYYY-MM-DD')).toBe('26-12-31');
});
it('FE-UTIL-DATEFMT-006: day and month without a year write only those two, in layout order', () => {
const opts: Intl.DateTimeFormatOptions = { day: 'numeric', month: 'numeric', timeZone: 'UTC' }
expect(formatDisplayDate(NYE, 'en-US', opts, 'DD.MM.YYYY')).toBe('31.12')
expect(formatDisplayDate(NYE, 'en-US', opts, 'YYYY-MM-DD')).toBe('12-31')
expect(formatDisplayDate(NYE, 'en-US', opts, 'MM/DD/YYYY')).toBe('12/31')
})
const opts: Intl.DateTimeFormatOptions = { day: 'numeric', month: 'numeric', timeZone: 'UTC' };
expect(formatDisplayDate(NYE, 'en-US', opts, 'DD.MM.YYYY')).toBe('31.12');
expect(formatDisplayDate(NYE, 'en-US', opts, 'YYYY-MM-DD')).toBe('12-31');
expect(formatDisplayDate(NYE, 'en-US', opts, 'MM/DD/YYYY')).toBe('12/31');
});
it('FE-UTIL-DATEFMT-007: a month name or a long dateStyle is left to Intl', () => {
const named: Intl.DateTimeFormatOptions[] = [
@@ -72,112 +79,135 @@ describe('dateFormat (#2971)', () => {
{ day: 'numeric', month: 'short', year: 'numeric', timeZone: 'UTC' },
{ month: 'long', year: 'numeric', timeZone: 'UTC' },
{ dateStyle: 'medium', timeZone: 'UTC' },
]
];
for (const opts of named) {
expect(formatDisplayDate(NYE, 'de-DE', opts, 'YYYY-MM-DD')).toBe(NYE.toLocaleDateString('de-DE', opts))
expect(formatDisplayDate(NYE, 'de-DE', opts, 'YYYY-MM-DD')).toBe(NYE.toLocaleDateString('de-DE', opts));
}
})
});
it('FE-UTIL-DATEFMT-021: a weekday beside a numeric date keeps the language, the date takes the format', () => {
const opts: Intl.DateTimeFormatOptions = { weekday: 'short', day: '2-digit', month: '2-digit', year: 'numeric', timeZone: 'UTC' }
expect(formatDisplayDate(NYE, 'de-DE', opts, 'YYYY-MM-DD')).toBe(`${NYE.toLocaleDateString('de-DE', { weekday: 'short', timeZone: 'UTC' })}, 2026-12-31`)
expect(formatDisplayDate(NYE, 'de-DE', opts, 'auto')).toBe(NYE.toLocaleDateString('de-DE', opts))
})
const opts: Intl.DateTimeFormatOptions = {
weekday: 'short',
day: '2-digit',
month: '2-digit',
year: 'numeric',
timeZone: 'UTC',
};
expect(formatDisplayDate(NYE, 'de-DE', opts, 'YYYY-MM-DD')).toBe(
`${NYE.toLocaleDateString('de-DE', { weekday: 'short', timeZone: 'UTC' })}, 2026-12-31`
);
expect(formatDisplayDate(NYE, 'de-DE', opts, 'auto')).toBe(NYE.toLocaleDateString('de-DE', opts));
});
it('FE-UTIL-DATEFMT-008: dateStyle short counts as numeric', () => {
expect(formatDisplayDate(NYE, 'en-US', { dateStyle: 'short', timeZone: 'UTC' }, 'DD/MM/YYYY')).toBe('31/12/2026')
})
expect(formatDisplayDate(NYE, 'en-US', { dateStyle: 'short', timeZone: 'UTC' }, 'DD/MM/YYYY')).toBe('31/12/2026');
});
it('FE-UTIL-DATEFMT-009: an invalid date falls back to toLocaleDateString', () => {
const invalid = new Date('nope')
expect(formatDisplayDate(invalid, 'en-US', NUMERIC, 'YYYY-MM-DD')).toBe(invalid.toLocaleDateString('en-US', NUMERIC))
})
})
const invalid = new Date('nope');
expect(formatDisplayDate(invalid, 'en-US', NUMERIC, 'YYYY-MM-DD')).toBe(
invalid.toLocaleDateString('en-US', NUMERIC)
);
});
});
describe('formatDisplayDateTime', () => {
it('FE-UTIL-DATEFMT-010: auto, an unknown value or none is exactly toLocaleString', () => {
const opts: Intl.DateTimeFormatOptions = { ...NUMERIC, hour: '2-digit', minute: '2-digit' }
const opts: Intl.DateTimeFormatOptions = { ...NUMERIC, hour: '2-digit', minute: '2-digit' };
for (const locale of ['en-US', 'de-DE']) {
expect(formatDisplayDateTime(NYE, locale, opts, 'auto')).toBe(NYE.toLocaleString(locale, opts))
expect(formatDisplayDateTime(NYE, locale, opts, 'nope')).toBe(NYE.toLocaleString(locale, opts))
expect(formatDisplayDateTime(NYE, locale, UTC)).toBe(NYE.toLocaleString(locale, UTC))
expect(formatDisplayDateTime(NYE, locale, opts, 'auto')).toBe(NYE.toLocaleString(locale, opts));
expect(formatDisplayDateTime(NYE, locale, opts, 'nope')).toBe(NYE.toLocaleString(locale, opts));
expect(formatDisplayDateTime(NYE, locale, UTC)).toBe(NYE.toLocaleString(locale, UTC));
}
})
});
it('FE-UTIL-DATEFMT-011: the date follows the format, the time of day stays Intl', () => {
const opts: Intl.DateTimeFormatOptions = { ...NUMERIC, hour: '2-digit', minute: '2-digit' }
const time = NYE.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', timeZone: 'UTC' })
expect(formatDisplayDateTime(NYE, 'en-US', opts, 'YYYY-MM-DD')).toBe(`2026-12-31 ${time}`)
const hour12 = { ...opts, hour12: false }
const time24 = NYE.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', hour12: false, timeZone: 'UTC' })
expect(formatDisplayDateTime(NYE, 'en-US', hour12, 'DD.MM.YYYY')).toBe(`31.12.2026 ${time24}`)
})
const opts: Intl.DateTimeFormatOptions = { ...NUMERIC, hour: '2-digit', minute: '2-digit' };
const time = NYE.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', timeZone: 'UTC' });
expect(formatDisplayDateTime(NYE, 'en-US', opts, 'YYYY-MM-DD')).toBe(`2026-12-31 ${time}`);
const hour12 = { ...opts, hour12: false };
const time24 = NYE.toLocaleTimeString('en-US', {
hour: '2-digit',
minute: '2-digit',
hour12: false,
timeZone: 'UTC',
});
expect(formatDisplayDateTime(NYE, 'en-US', hour12, 'DD.MM.YYYY')).toBe(`31.12.2026 ${time24}`);
});
it('FE-UTIL-DATEFMT-012: without options it writes the date and the default time', () => {
expect(formatDisplayDateTime(NYE, 'en-US', UTC, 'DD/MM/YYYY')).toBe(
`31/12/2026 ${NYE.toLocaleTimeString('en-US', UTC)}`
)
})
);
});
it('FE-UTIL-DATEFMT-013: time-only options are passed through untouched', () => {
const opts: Intl.DateTimeFormatOptions = { hour: '2-digit', minute: '2-digit', timeZone: 'UTC' }
expect(formatDisplayDateTime(NYE, 'de-DE', opts, 'YYYY-MM-DD')).toBe(NYE.toLocaleString('de-DE', opts))
})
const opts: Intl.DateTimeFormatOptions = { hour: '2-digit', minute: '2-digit', timeZone: 'UTC' };
expect(formatDisplayDateTime(NYE, 'de-DE', opts, 'YYYY-MM-DD')).toBe(NYE.toLocaleString('de-DE', opts));
});
it('FE-UTIL-DATEFMT-014: date-only options give just the date, named dates stay Intl', () => {
expect(formatDisplayDateTime(NYE, 'en-US', NUMERIC, 'YYYY/MM/DD')).toBe('2026/12/31')
const named: Intl.DateTimeFormatOptions = { weekday: 'short', day: 'numeric', month: 'short', hour: '2-digit', timeZone: 'UTC' }
expect(formatDisplayDateTime(NYE, 'en-US', named, 'YYYY-MM-DD')).toBe(NYE.toLocaleString('en-US', named))
})
})
expect(formatDisplayDateTime(NYE, 'en-US', NUMERIC, 'YYYY/MM/DD')).toBe('2026/12/31');
const named: Intl.DateTimeFormatOptions = {
weekday: 'short',
day: 'numeric',
month: 'short',
hour: '2-digit',
timeZone: 'UTC',
};
expect(formatDisplayDateTime(NYE, 'en-US', named, 'YYYY-MM-DD')).toBe(NYE.toLocaleString('en-US', named));
});
});
describe('displayDateFormatter', () => {
it('FE-UTIL-DATEFMT-015: auto behaves like Intl.DateTimeFormat', () => {
const intl = new Intl.DateTimeFormat('en-US', NUMERIC)
expect(displayDateFormatter('en-US', NUMERIC, 'auto').format(NYE)).toBe(intl.format(NYE))
expect(displayDateFormatter('en-US', NUMERIC).format(NYE.getTime())).toBe(intl.format(NYE))
})
const intl = new Intl.DateTimeFormat('en-US', NUMERIC);
expect(displayDateFormatter('en-US', NUMERIC, 'auto').format(NYE)).toBe(intl.format(NYE));
expect(displayDateFormatter('en-US', NUMERIC).format(NYE.getTime())).toBe(intl.format(NYE));
});
it('FE-UTIL-DATEFMT-016: numeric dates follow the format, for Date and timestamp alike', () => {
const f = displayDateFormatter('en-US', NUMERIC, 'DD.MM.YYYY')
expect(f.format(NYE)).toBe('31.12.2026')
expect(f.format(NYE.getTime())).toBe('31.12.2026')
const withTime = displayDateFormatter('en-US', { ...NUMERIC, hour: '2-digit', minute: '2-digit' }, 'YYYY-MM-DD')
const f = displayDateFormatter('en-US', NUMERIC, 'DD.MM.YYYY');
expect(f.format(NYE)).toBe('31.12.2026');
expect(f.format(NYE.getTime())).toBe('31.12.2026');
const withTime = displayDateFormatter('en-US', { ...NUMERIC, hour: '2-digit', minute: '2-digit' }, 'YYYY-MM-DD');
expect(withTime.format(NYE)).toBe(
`2026-12-31 ${NYE.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', timeZone: 'UTC' })}`
)
})
);
});
it('FE-UTIL-DATEFMT-017: a named month is left to Intl', () => {
const opts: Intl.DateTimeFormatOptions = { day: 'numeric', month: 'long', timeZone: 'UTC' }
expect(displayDateFormatter('de-DE', opts, 'YYYY-MM-DD').format(NYE)).toBe(new Intl.DateTimeFormat('de-DE', opts).format(NYE))
})
})
const opts: Intl.DateTimeFormatOptions = { day: 'numeric', month: 'long', timeZone: 'UTC' };
expect(displayDateFormatter('de-DE', opts, 'YYYY-MM-DD').format(NYE)).toBe(
new Intl.DateTimeFormat('de-DE', opts).format(NYE)
);
});
});
describe('dateFieldOrder, dateLayout and isNumericDate', () => {
it('FE-UTIL-DATEFMT-018: a chosen format sets the order, auto asks the language', () => {
expect(dateFieldOrder('en-US', 'DD.MM.YYYY')).toEqual(['day', 'month', 'year'])
expect(dateFieldOrder('de-DE', 'MM/DD/YYYY')).toEqual(['month', 'day', 'year'])
expect(dateFieldOrder('en-US', 'YYYY/MM/DD')).toEqual(['year', 'month', 'day'])
expect(dateFieldOrder('de-DE', 'auto')).toEqual(['day', 'month', 'year'])
expect(dateFieldOrder('en-US')).toEqual(['month', 'day', 'year'])
expect(dateFieldOrder('ko-KR', null)).toEqual(['year', 'month', 'day'])
})
expect(dateFieldOrder('en-US', 'DD.MM.YYYY')).toEqual(['day', 'month', 'year']);
expect(dateFieldOrder('de-DE', 'MM/DD/YYYY')).toEqual(['month', 'day', 'year']);
expect(dateFieldOrder('en-US', 'YYYY/MM/DD')).toEqual(['year', 'month', 'day']);
expect(dateFieldOrder('de-DE', 'auto')).toEqual(['day', 'month', 'year']);
expect(dateFieldOrder('en-US')).toEqual(['month', 'day', 'year']);
expect(dateFieldOrder('ko-KR', null)).toEqual(['year', 'month', 'day']);
});
it('FE-UTIL-DATEFMT-019: dateLayout knows the five formats only, isNumericDate spots names', () => {
expect(dateLayout('DD/MM/YYYY')).toEqual({ order: ['day', 'month', 'year'], separator: '/' })
expect(dateLayout('auto')).toBeNull()
expect(dateLayout('nope')).toBeNull()
expect(dateLayout(undefined)).toBeNull()
expect(isNumericDate()).toBe(true)
expect(isNumericDate({ month: '2-digit' })).toBe(true)
expect(isNumericDate({ month: 'short' })).toBe(false)
expect(isNumericDate({ weekday: 'long' })).toBe(false)
expect(isNumericDate({ era: 'short' })).toBe(false)
expect(isNumericDate({ dateStyle: 'short' })).toBe(true)
expect(isNumericDate({ dateStyle: 'long' })).toBe(false)
})
})
expect(dateLayout('DD/MM/YYYY')).toEqual({ order: ['day', 'month', 'year'], separator: '/' });
expect(dateLayout('auto')).toBeNull();
expect(dateLayout('nope')).toBeNull();
expect(dateLayout(undefined)).toBeNull();
expect(isNumericDate()).toBe(true);
expect(isNumericDate({ month: '2-digit' })).toBe(true);
expect(isNumericDate({ month: 'short' })).toBe(false);
expect(isNumericDate({ weekday: 'long' })).toBe(false);
expect(isNumericDate({ era: 'short' })).toBe(false);
expect(isNumericDate({ dateStyle: 'short' })).toBe(true);
expect(isNumericDate({ dateStyle: 'long' })).toBe(false);
});
});
describe('dateFormatOptions', () => {
it('FE-UTIL-DATEFMT-020: every choice is labelled by how it writes 31 December, auto with its example', () => {
@@ -188,8 +218,11 @@ describe('dateFormat (#2971)', () => {
{ value: 'DD.MM.YYYY', label: '31.12.2026' },
{ value: 'DD/MM/YYYY', label: '31/12/2026' },
{ value: 'MM/DD/YYYY', label: '12/31/2026' },
])
expect(dateFormatOptions('de-DE', 'Automatisch')[0]).toEqual({ value: 'auto', label: 'Automatisch (31.12.2026)' })
})
})
})
]);
expect(dateFormatOptions('de-DE', 'Automatisch')[0]).toEqual({
value: 'auto',
label: 'Automatisch (31.12.2026)',
});
});
});
});