-
Notifications
You must be signed in to change notification settings - Fork 375
/
helpers.ts
35 lines (30 loc) · 1.22 KB
/
helpers.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
import {
KeepOtherSorting, GetColumnSortingDirectionFn, GetPersistentSortedColumnsFn,
CalculateKeepOtherFn,
} from '../../types';
const unique = (arr: any[]) => [...Array.from(new Set(arr))];
export const getColumnSortingDirection: GetColumnSortingDirectionFn = (
sorting, columnName,
) => {
const columnSorting = sorting.filter(s => s.columnName === columnName)[0];
return columnSorting ? columnSorting.direction : null;
};
export const getPersistentSortedColumns: GetPersistentSortedColumnsFn = (
sorting, columnExtensions = [],
) => columnExtensions.reduce((acc, { columnName, sortingEnabled }) => {
if (sortingEnabled === false) {
if (sorting.findIndex(sortItem => sortItem.columnName === columnName) > -1) {
acc.push(columnName);
}
}
return acc;
}, [] as string[]);
export const calculateKeepOther: CalculateKeepOtherFn = (
sorting, keepOther, persistentSortedColumns = [],
) => {
if (!persistentSortedColumns.length) return keepOther as KeepOtherSorting;
if (!keepOther) return persistentSortedColumns as KeepOtherSorting;
return Array.isArray(keepOther)
? unique([...keepOther, ...persistentSortedColumns])
: unique([...sorting.map(item => item.columnName), ...persistentSortedColumns]);
};