feat(lib): implement formatDateFr (fr-FR locale)
This commit is contained in:
1 parent
ff3989c41f
commit
8b9eb61a69
2 files changed
+40
-2
No files matched your search
+12
-2
@@ -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)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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}/)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user