import { CellValueType, ColumnConfig } from './dataTable.types'; export const columnWidth = (width: number) => ({ width: `${width}%`, 'min-width': `${width}%`, }); export const getDefaultColumnWidth = (config: ColumnConfig[]) => { const fixedWidthSum = config.map(item => item.width ?? 0).reduce((first, second) => first + second); const defaultWidth = (100 - fixedWidthSum) / (config.filter(item => !item.width).length); return defaultWidth; }; type SupportedOrderingMethods = 'date' | 'fullDate' | 'boolean' | 'number' | 'text'; export const reorderData = (data: T[], valueExtractor: (item: T) => CellValueType, orderMethod: SupportedOrderingMethods, ascending: boolean): T[] => { switch (orderMethod) { case 'date': case 'fullDate': return data.sort((first, second) => { const firstValue = new Date(valueExtractor(first) as string).getTime(); const secondValue = new Date(valueExtractor(second) as string).getTime(); const sortResult = firstValue - secondValue; return ascending ? sortResult : sortResult * -1; }); case 'boolean': return data.sort((first, second) => { const firstValue = Boolean(valueExtractor(first)); const secondValue = Boolean(valueExtractor(second)); const sortResult = (firstValue === secondValue) ? 0 : (firstValue ? 1 : -1); return ascending ? sortResult : sortResult * -1; }); case 'number': return data.sort((first, second) => { const firstValue = Number(valueExtractor(first)); const secondValue = Number(valueExtractor(second)); const sortResult = firstValue - secondValue; return ascending ? sortResult : sortResult * -1; }); case 'text': return data.sort((first, second) => { const firstValue = valueExtractor(first) as string; const secondValue = valueExtractor(second) as string; return ascending ? firstValue.localeCompare(secondValue) : secondValue.localeCompare(firstValue); }); default: return data; } }; export default { columnWidth, getDefaultColumnWidth, reorderData, };