Improve financial statement value formatting

This commit is contained in:
2026-03-09 18:58:15 -04:00
parent fae8c54121
commit fa2de3e259
3 changed files with 306 additions and 88 deletions

View File

@@ -1,5 +1,9 @@
import { describe, expect, it } from 'bun:test';
import { formatCurrencyByScale, formatScaledNumber } from './format';
import {
formatCurrencyByScale,
formatFinancialStatementValue,
formatScaledNumber
} from './format';
describe('formatScaledNumber', () => {
it('keeps values below one thousand unscaled', () => {
@@ -45,3 +49,59 @@ describe('formatCurrencyByScale', () => {
expect(formatCurrencyByScale(-2_500_000, 'millions')).toBe('-$2.5M');
});
});
describe('formatFinancialStatementValue', () => {
it('renders null statement values as em dashes', () => {
expect(formatFinancialStatementValue({
value: null,
unit: 'currency',
scale: 'millions',
rowKey: 'revenue',
surfaceKind: 'income_statement'
})).toBe('—');
});
it('formats scaled currency values without a currency symbol', () => {
expect(formatFinancialStatementValue({
value: 15_940_899_000,
unit: 'currency',
scale: 'millions',
rowKey: 'revenue',
surfaceKind: 'income_statement'
})).toBe('15,940.9');
expect(formatFinancialStatementValue({
value: 1_100_000_000,
unit: 'currency',
scale: 'millions',
rowKey: 'long_term_debt_issued',
surfaceKind: 'cash_flow_statement'
})).toBe('1,100');
});
it('keeps extra precision for per-share rows', () => {
expect(formatFinancialStatementValue({
value: 14.64,
unit: 'currency',
scale: 'millions',
rowKey: 'diluted_eps',
surfaceKind: 'income_statement'
})).toBe('14.64');
});
it('formats statement percents and ratios with trimmed precision', () => {
expect(formatFinancialStatementValue({
value: 0.233,
unit: 'percent',
scale: 'millions',
rowKey: 'effective_tax_rate',
surfaceKind: 'income_statement'
})).toBe('23.3%');
expect(formatFinancialStatementValue({
value: 1.5,
unit: 'ratio',
scale: 'millions',
rowKey: 'debt_to_equity',
surfaceKind: 'ratios'
})).toBe('1.5x');
});
});