/** How the Research posts write dates and numbers. */ const MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; /** 2026-10-05 as "5 October". */ export const longDate = (date: string) => `${Number(date.slice(8, 10))} ${MONTHS[Number(date.slice(5, 7)) - 1]}`; /** 2026-10-04 as "4 Oct". */ export const shortDate = (date: string) => `${Number(date.slice(8, 10))} ${MONTHS[Number(date.slice(5, 7)) - 1]!.slice(0, 3)}`; /** 2026-10-12 as "12 October 2026". */ export const fullDate = (date: string) => `${longDate(date)} ${date.slice(0, 4)}`; /** Minutes as the posts print them: two decimals, as measured. */ export const minutes = (value: number | null) => (value === null ? '–' : value.toFixed(2)); export const percent = (share: number) => `${Math.round(share * 100)}%`; export const times = (ratio: number) => `${ratio.toFixed(1)}×`; /** 0.9 as "90th percentile". */ export function percentileName(level: number): string { const n = Math.round(level * 100); const suffix = n % 100 >= 11 && n % 100 <= 13 ? 'th' : ({ 1: 'st', 2: 'nd', 3: 'rd' } as Record)[n % 10] ?? 'th'; return `${n}${suffix} percentile`; } /** A small probability as a mantissa with one decimal and a power of ten, for "9.0 × 10⁻¹⁹". */ export function scientific(value: number): { mantissa: string; base: string; exponent: string } { let exponent = Math.floor(Math.log10(value)); let mantissa = value / 10 ** exponent; if (Number(mantissa.toFixed(1)) >= 10) { mantissa /= 10; exponent += 1; } return { mantissa: mantissa.toFixed(1), base: '10', exponent: String(exponent).replace('-', '−') }; }