forked from smart-table/smart-table-sort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (51 loc) · 1.53 KB
/
index.js
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
60
import {swap} from 'smart-table-operators';
import pointer from 'smart-table-json-pointer';
/**
* Standard comparator that uses default comparison operator
*
* @param aVal
* @param bVal the two values to compare
* @return -1 if aVal is to be before after bVal, 1 if bVal is before aVal
*/
function standardComparator(aVal, bVal) {
if (aVal === bVal) {
return 0;
}
if (bVal === undefined) {
return -1;
}
if (aVal === undefined) {
return 1;
}
return aVal < bVal ? -1 : 1;
}
/**
* Return a compartor that reads the property given by prop to compare two values
* @param prop - the path to the property
* @param comparator - the comparator to use to compare values
*/
function compareByProperty(prop, comparator) {
const propGetter = pointer(prop).get;
return (a, b) => {
const aVal = propGetter(a);
const bVal = propGetter(b);
return comparator(aVal, bVal);
};
}
/**
* Create a sort function that sorts an array according to one property
* @param pointer - the path to the property used for sorting
* @param direction - the direction; 'asc', 'desc', or 'none'
* @param comparator - optional comparator function used for sorting
*/
export default function sortFactory({pointer, direction, comparator} = {}) {
if (!comparator) {
comparator = standardComparator;
}
if (!pointer || direction === 'none') {
return array => [...array];
}
const orderFunc = compareByProperty(pointer, comparator);
const compareFunc = direction === 'desc' ? swap(orderFunc) : orderFunc;
return array => [...array].sort(compareFunc);
}