Make profile SSR formatting deterministic
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
const PROFILE_LOCALE = 'en-US'
|
||||
const PROFILE_TIME_ZONE = 'UTC'
|
||||
|
||||
export function formatProfileNumber(value) {
|
||||
const numeric = Number(value ?? 0)
|
||||
return Number.isFinite(numeric) ? new Intl.NumberFormat(PROFILE_LOCALE).format(numeric) : '0'
|
||||
}
|
||||
|
||||
export function formatProfileDate(value, options = {}) {
|
||||
if (!value) return null
|
||||
|
||||
const date = value instanceof Date ? value : new Date(value)
|
||||
if (Number.isNaN(date.getTime())) return null
|
||||
|
||||
return new Intl.DateTimeFormat(PROFILE_LOCALE, {
|
||||
...options,
|
||||
timeZone: PROFILE_TIME_ZONE,
|
||||
}).format(date)
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { formatProfileDate, formatProfileNumber } from './profileFormatters'
|
||||
|
||||
describe('profile formatters', () => {
|
||||
it('uses a stable en-US number format independent of the browser locale', () => {
|
||||
expect(formatProfileNumber(9529106)).toBe('9,529,106')
|
||||
})
|
||||
|
||||
it('uses UTC for stable calendar dates at timezone boundaries', () => {
|
||||
expect(formatProfileDate('2024-01-01T00:30:00.000Z', {
|
||||
month: 'long',
|
||||
day: 'numeric',
|
||||
year: 'numeric',
|
||||
})).toBe('January 1, 2024')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user