import { ColumnType, ITypeOptions, NumberFormat } from '../../../types'; import { exhaustiveSwitch } from '../../../typeUtils'; /** * Get formatted string value for number * @param typeOptions number column config * @param value number value */ export const transformNumberToString = ( value: number, typeOptions: Pick< ITypeOptions, 'precision' | 'numberFormat' > ) => { if (!isFinite(value)) return ''; const precision = typeOptions ? typeOptions.precision : 0; const numberFormat = typeOptions.numberFormat ?? NumberFormat.DECIMAL; switch (numberFormat) { case NumberFormat.DECIMAL: return value.toFixed(precision); case NumberFormat.PERCENTAGE: return `${(value * 100).toFixed(precision)}%`; case NumberFormat.DECIMAL_WITH_COMMAS: return value.toLocaleString('en', { maximumFractionDigits: precision, minimumFractionDigits: precision, }); case NumberFormat.CURRENCY: return ''; default: return exhaustiveSwitch({ switchValue: numberFormat, returnValue: '', }) } };