From 8b9eb61a69fb7076d6422a3620857b6dd7e8affc Mon Sep 17 00:00:00 2001 From: Bastien Chanot Date: Mon, 18 May 2026 20:59:05 +0200 Subject: [PATCH] feat(lib): implement formatDateFr (fr-FR locale) --- src/lib/dates.ts | 14 ++++++++++++-- tests/unit/lib.dates.test.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 tests/unit/lib.dates.test.ts diff --git a/src/lib/dates.ts b/src/lib/dates.ts index 490c1244..d3178b0a 100644 --- a/src/lib/dates.ts +++ b/src/lib/dates.ts @@ -8,8 +8,18 @@ export function todayIso(): string { /** * Formats an ISO date string as a French locale date (e.g. "18 mai 2026"). */ -export function formatDateFr(_isoDate: string): string { - throw new Error('not implemented') +export function formatDateFr(isoDate: string): string { + const match = isoDate.match(/^(\d{4})-(\d{2})-(\d{2})$/) + if (!match) throw new Error(`Invalid ISO date: ${isoDate}`) + const [, y, mo, d] = match + const date = new Date(Date.UTC(Number(y), Number(mo) - 1, Number(d))) + if (Number.isNaN(date.getTime())) throw new Error(`Invalid ISO date: ${isoDate}`) + return new Intl.DateTimeFormat('fr-FR', { + day: 'numeric', + month: 'long', + year: 'numeric', + timeZone: 'UTC', + }).format(date) } /** diff --git a/tests/unit/lib.dates.test.ts b/tests/unit/lib.dates.test.ts new file mode 100644 index 00000000..e08adb8e --- /dev/null +++ b/tests/unit/lib.dates.test.ts @@ -0,0 +1,28 @@ +import { describe, it, expect } from 'vitest' +import { formatDateFr, todayIso, nowIso } from '@/lib/dates' + +describe('formatDateFr', () => { + it('formats an ISO date as "18 mai 2026"', () => { + expect(formatDateFr('2026-05-18')).toBe('18 mai 2026') + }) + + it('formats a leading-zero day correctly', () => { + expect(formatDateFr('2026-01-03')).toBe('3 janvier 2026') + }) + + it('throws on invalid input', () => { + expect(() => formatDateFr('not-a-date')).toThrow() + }) +}) + +describe('todayIso', () => { + it('returns YYYY-MM-DD', () => { + expect(todayIso()).toMatch(/^\d{4}-\d{2}-\d{2}$/) + }) +}) + +describe('nowIso', () => { + it('returns a full ISO instant', () => { + expect(nowIso()).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/) + }) +})