This repository was archived by the owner on Jun 4, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathindex.ts
58 lines (47 loc) · 1.62 KB
/
index.ts
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
import * as R from 'ramda';
import { ColumnId } from 'dash-table/components/Table/props';
export interface ISortSetting {
column_id: ColumnId;
direction: SortDirection;
}
export enum SortDirection {
Ascending = 'asc',
Descending = 'desc',
None = 'none'
}
export type SortSettings = ISortSetting[];
type IsNullyFn = (value: any) => boolean;
export const defaultIsNully: IsNullyFn = (value: any) => value === undefined || value === null;
export default (data: any[], settings: SortSettings, isNully: IsNullyFn = defaultIsNully): any[] => {
if (!settings.length) {
return data;
}
return R.sortWith(
R.map(setting => {
return setting.direction === SortDirection.Descending ?
R.comparator((d1: any, d2: any) => {
const id = setting.column_id;
const prop1 = d1[id];
const prop2 = d2[id];
if (isNully(prop1)) {
return false;
} else if (isNully(prop2)) {
return true;
}
return prop1 > prop2;
}) :
R.comparator((d1: any, d2: any) => {
const id = setting.column_id;
const prop1 = d1[id];
const prop2 = d2[id];
if (isNully(prop1)) {
return false;
} else if (isNully(prop2)) {
return true;
}
return prop1 < prop2;
});
}, settings),
data
);
};