-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchainsorter.js
66 lines (56 loc) · 1.72 KB
/
chainsorter.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
61
62
63
64
65
66
(function (fake_exports) { "use strict";
class Sortable {
constructor(data) {
this.data = data;
this.definition = [];
}
// Append each value to the definition along with an associated sort code
push(code, value) {
value.forEach(function(f) {
if (Array.isArray(f)) // In case they send in an array accidentally
this.push(code, f);
else
this.definition.push([code, f]);
}.bind(this));
return this;
}
// Ascending order; takes one or more property name as arguments
asc(field) {
return this.push('asc', [...arguments]);
}
// Descending order; takes one or more property name as arguments
desc(field) {
return this.push('desc', [...arguments]);
}
// Order by a custom function. Pass in a function that takes two params, and in it compare them
// and return -1, 1, or 0 as appropriate.
// Also takes one or more functions as arguments
custom(fn) {
return this.push('fn', [...arguments]);
}
// Sorts the data and also returns it
sort() {
return this.data.sort((a,b) => {
for(var i=0; i < this.definition.length; i++) {
const [code, key] = this.definition[i];
switch(code) {
case 'asc':
case 'desc':
if (a[key] > b[key])
return code == 'asc' ? 1 : -1;
if (a[key] < b[key])
return code == 'asc' ? -1 : 1;
break;
case 'fn':
let result = key(a, b);
if (result != 0) // If it's zero the sort wasn't decided.
return result;
break;
}
}
return 0;
});
}
}
fake_exports['ChainSorter'] = (data) => { return new Sortable(data); };
})(typeof exports != "undefined" ? exports : typeof window != "undefined" ? window : typeof self != "undefined" ? self : this);