forked from Donkie/Spoolman
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting.ts
More file actions
59 lines (52 loc) · 1.89 KB
/
Copy pathsorting.ts
File metadata and controls
59 lines (52 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { CrudSort } from "@refinedev/core";
import { SortOrder } from "antd/es/table/interface";
import { getCustomFieldKey, isCustomField } from "./queryFields";
interface TypedCrudSort<Obj> {
field: keyof Obj | string;
order: "asc" | "desc";
}
/**
* Returns the sort order for a given field based on the provided sorters.
* @param sorters An array of `CrudSort` objects that define the sorting criteria.
* @param field The field to get the sort order for.
* @returns The sort order for the given field, or undefined if the field is not being sorted.
*/
export function getSortOrderForField<Obj>(
sorters: TypedCrudSort<Obj>[],
field: Field | string,
): SortOrder | undefined {
const sorter = sorters.find((s) => s.field === field);
if (sorter) {
return sorter.order === "asc" ? "ascend" : "descend";
}
return undefined;
}
export function typeSorters<Obj>(sorters: CrudSort[]): TypedCrudSort<Obj>[] {
return sorters as TypedCrudSort<Obj>[]; // <-- Unsafe cast
}
/**
* Checks if a sorter is for a custom field
* @param sorter The sorter to check
* @returns True if the sorter is for a custom field
*/
export function isCustomFieldSorter<Obj = any>(sorter: TypedCrudSort<Obj> | CrudSort): boolean {
return typeof sorter.field === 'string' && isCustomField(sorter.field);
}
/**
* Extracts all custom field sorters from a list of sorters
* @param sorters The list of sorters
* @returns An object with custom field keys and their sort orders
*/
export function getCustomFieldSorters<Obj = any>(
sorters: TypedCrudSort<Obj>[] | CrudSort[]
): Record<string, "asc" | "desc"> {
const customFieldSorters: Record<string, "asc" | "desc"> = {};
sorters.forEach((sorter) => {
if (isCustomFieldSorter(sorter)) {
const field = sorter.field.toString();
const key = getCustomFieldKey(field);
customFieldSorters[key] = sorter.order;
}
});
return customFieldSorters;
}