made tables more feature complete and started splitting of all the data handling to dataset.ts

This commit is contained in:
Chris Kruining 2024-12-10 15:07:45 +01:00
parent 17e49c23d8
commit 977670b9e0
No known key found for this signature in database
GPG key ID: EB894A3560CCCAD2
7 changed files with 325 additions and 123 deletions

View file

@ -0,0 +1,27 @@
export type RowNode<T> = { kind: 'row', key: string, value: T }
export type GroupNode<T> = { kind: 'group', key: string, groupedBy: keyof T, nodes: Node<T>[] };
export type Node<T> = RowNode<T> | GroupNode<T>;
export type DataSet<T extends Record<string, any>> = Node<T>[];
export const createDataSet = <T extends Record<string, any>>(data: T[]): Node<T>[] => {
return Object.entries(data).map<RowNode<T>>(([key, value]) => ({ kind: 'row', key, value }));
};
type SortingFunction<T> = (a: T, b: T) => -1 | 0 | 1;
type SortOptions<T extends Record<string, any>> = { by: keyof T, reversed: boolean, with: SortingFunction<T> };
export const toSorted = <T extends Record<string, any>>(dataSet: DataSet<T>, sort: SortOptions<T>): DataSet<T> => {
const sorted = dataSet.toSorted((a, b) => sort.with(a.value[sort.by], b.value[sort.by]));
if (sort.reversed) {
sorted.reverse();
}
return sorted;
};
type GroupingFunction<T> = (nodes: RowNode<T>[]) => Node<T>[];
type GroupOptions<T extends Record<string, any>> = { by: keyof T, with: GroupingFunction<T> };
export const toGrouped = <T extends Record<string, any>>(dataSet: DataSet<T>, group: GroupOptions<T>): DataSet<T> => group.with(dataSet as any);