Initial commit: Add project files
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
export function formatCurrency(value: number | null | undefined, currency: string, locale = 'ko-KR'): string {
|
||||
if (value == null || Number.isNaN(value)) return '—'
|
||||
return new Intl.NumberFormat(locale, { style: 'currency', currency, maximumFractionDigits: 2 }).format(value)
|
||||
}
|
||||
export function formatPercent(value: number | null | undefined, digits = 2, locale = 'ko-KR'): string {
|
||||
if (value == null || Number.isNaN(value)) return '—'
|
||||
return new Intl.NumberFormat(locale, { style: 'percent', minimumFractionDigits: digits, maximumFractionDigits: digits }).format(value)
|
||||
}
|
||||
export function formatQuantity(value: number | null | undefined, digits = 4, locale = 'ko-KR'): string {
|
||||
if (value == null || Number.isNaN(value)) return '—'
|
||||
return new Intl.NumberFormat(locale, { maximumFractionDigits: digits }).format(value)
|
||||
}
|
||||
export function formatAsOf(value: string | Date | null | undefined, locale = 'ko-KR'): string {
|
||||
if (!value) return '—'
|
||||
const date = value instanceof Date ? value : new Date(value)
|
||||
if (Number.isNaN(date.getTime())) return '—'
|
||||
return new Intl.DateTimeFormat(locale, { dateStyle: 'medium', timeStyle: 'short', timeZone: 'Asia/Seoul' }).format(date)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { formatCurrency, formatPercent, formatQuantity } from '../financial'
|
||||
describe('financial formatters', () => {
|
||||
it('renders missing values as an explicit em dash', () => { expect(formatCurrency(null, 'KRW')).toBe('—'); expect(formatPercent(undefined)).toBe('—') })
|
||||
it('keeps percentage inputs in decimal-return units', () => { expect(formatPercent(0.125, 1)).toContain('12.5') })
|
||||
it('uses bounded quantity precision', () => { expect(formatQuantity(1.234567, 2)).toContain('1.23') })
|
||||
})
|
||||
Reference in New Issue
Block a user