-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortContext.js
63 lines (63 loc) · 1.84 KB
/
SortContext.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
/*!
* @author electricessence / https://github.com/electricessence/
* Licensing: MIT
*/
/**
* A class for helping in complex sorting patterns.
*/
export default class SortContext {
/**
* Constructs a SortContext.
* @param {Comparer | null} _next If provided (not null) any items that are considered equal will use this comparer to decided their order.
* @param {Comparison} _comparer The comparison function that will differentiate between items.
* @param {Order} _order Ascending or Descending.
*/
constructor(_next, _comparer, _order = 1 /* Order.Ascending */) {
this._next = _next;
this._comparer = _comparer;
this._order = _order;
}
/**
* Direction of the comparison.
* @type {Order}
*/
get order() {
return this._order;
}
/**
* A scope safe comparison function (delegate).
* @return {Comparison}
*/
get comparison() {
if (this._comparison)
return this._comparison;
const c = (a, b) => this.compare(a, b);
this._comparison = c;
return c;
}
/**
* Generates an array of indexes from the source in order of their expected internalSort without modifying the source.
* @param source
* @returns {number[]}
*/
generateSortedIndexes(source) {
if (source == null)
return [];
const result = source.map((s, i) => i);
result.sort((a, b) => this.compare(source[a], source[b]));
return result;
}
/**
* Compares two values based upon SortContext parameters.
* @param a
* @param b
* @returns {any}
*/
compare(a, b) {
const d = this._comparer(a, b);
if (d === 0 && this._next)
return this._next.compare(a, b);
return this._order * d;
}
}
//# sourceMappingURL=SortContext.js.map