/
Sorter.ts
242 lines (233 loc) · 7.57 KB
/
Sorter.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
export type SortOptions = {
/** Sort case sensitively. */
caseSensitive?: boolean;
/** Reverse the sort order. */
reverse?: boolean;
/** Options to pass to Intl.Collator for string comparisons. This overrides the caseSensitive options. */
collatorOptions?: Intl.CollatorOptions;
/** A custom comparator to use. This overrides all other options except reverse. */
comparator?: (a: any, b: any) => number;
};
/**
* Entity Array Sorter
*
* Sorting functions sort the array in place and also return the sorted array.
*/
export default class Sorter<Entity extends Object> {
public array: (Entity & { [k: string]: any })[];
private sortProperty: string | null = null;
private sortParent: string | null = null;
private collator: Intl.Collator = new Intl.Collator();
private comparator: ((a: any, b: any) => number) | undefined = undefined;
constructor(array: Entity[]) {
this.array = array as (Entity & { [k: string]: any })[];
}
private _arraySortProperty(
a: Entity & { [k: string]: any },
b: Entity & { [k: string]: any }
) {
const prop = this.sortProperty as string;
const parent = this.sortParent as string;
if (
parent != null &&
a[parent] instanceof Object &&
b[parent] instanceof Object
) {
const aParentProp = a[parent][prop];
const bParentProp = b[parent][prop];
if (
typeof aParentProp !== 'undefined' ||
typeof bParentProp !== 'undefined'
) {
if (this.comparator != null) {
const result = this.comparator(aParentProp, bParentProp);
if (result !== 0) {
return result;
}
} else if (
typeof aParentProp === 'string' &&
typeof bParentProp === 'string'
) {
const result = this.collator.compare(aParentProp, bParentProp);
if (result !== 0) {
return result;
}
} else if (aParentProp > bParentProp) {
return 1;
} else if (aParentProp < bParentProp) {
return -1;
}
}
}
// If they have the same parent, order them by their own prop.
const aProp = a[prop];
const bProp = b[prop];
if (this.comparator != null) {
return this.comparator(aProp, bProp);
} else if (typeof aProp === 'string' && typeof bProp === 'string') {
return this.collator.compare(aProp, bProp);
} else if (aProp > bProp) {
return 1;
} else if (aProp < bProp) {
return -1;
}
return 0;
}
/**
* Sort an array of entities hierarchically by a specified property's value.
*
* Entities will be placed immediately after their parents. The
* `parentProperty` property, if present, should hold either null, undefined,
* or the entity's parent.
*
* @param property The name of the property to sort entities by.
* @param parentProperty The name of the property which holds the parent of the entity.
*/
public hsort(
property: string,
parentProperty: string,
sortOptions?: SortOptions
): Entity[] {
// First sort by the requested property.
this.sort(property, sortOptions);
if (typeof parentProperty === 'undefined' || parentProperty === null) {
return this.array;
}
// Now sort by children.
let newArray: (Entity & { [k: string]: any })[] = [];
// Look for entities ready to go in order.
let changed: boolean;
while (this.array.length) {
changed = false;
for (let key = 0; key < this.array.length; key++) {
// Must break after adding one, so any following children don't go in
// the wrong order.
if (
!(parentProperty in this.array[key]) ||
this.array[key][parentProperty] == null ||
typeof this.array[key][parentProperty].$inArray !== 'function' ||
!this.array[key][parentProperty].$inArray([
...newArray,
...this.array,
])
) {
// If they have no parent (or their parent isn't in the array), they
// go on the end.
newArray.push(this.array[key]);
this.array.splice(key, 1);
changed = true;
break;
} else if (
typeof this.array[key][parentProperty].$arraySearch === 'function'
) {
// Else find the parent.
const pkey = this.array[key][parentProperty].$arraySearch(newArray);
if (pkey !== -1) {
// And insert after the parent.
// This makes entities go to the end of the child list.
const ancestry = [this.array[key][parentProperty].guid];
let newKey = pkey;
while (
newKey + 1 < newArray.length &&
parentProperty in newArray[newKey + 1] &&
newArray[newKey + 1][parentProperty] != null &&
ancestry.indexOf(newArray[newKey + 1][parentProperty].guid) !== -1
) {
ancestry.push(newArray[newKey + 1].guid);
newKey += 1;
}
// Where to place the entity.
newKey += 1;
if (newKey < newArray.length) {
// If it already exists, we have to splice it in.
newArray.splice(newKey, 0, this.array[key]);
} else {
// Else just add it.
newArray.push(this.array[key]);
}
this.array.splice(key, 1);
changed = true;
break;
}
}
}
if (!changed) {
// If there are any unexpected errors and the array isn't changed, just
// stick the rest on the end.
if (this.array.length) {
newArray = newArray.concat(this.array);
this.array.splice(0, this.array.length);
}
}
}
// Now push the new array out.
this.array.splice(0, 0, ...newArray);
return this.array as Entity[];
}
/**
* Sort an array of entities by parent and a specified property's value.
*
* Entities' will be sorted by their parents' properties, then the entities'
* properties.
*
* @param property The name of the property to sort entities by.
* @param parentProperty The name of the property which holds the parent of the entity.
*/
public psort(
property: string,
parentProperty: string,
{
caseSensitive = false,
reverse = false,
collatorOptions = undefined,
comparator = undefined,
}: SortOptions = {}
): Entity[] {
// Sort by the requested property.
this.sortProperty = property;
this.sortParent = parentProperty;
this.collator = new Intl.Collator(
undefined,
collatorOptions || {
sensitivity: caseSensitive ? 'case' : 'base',
caseFirst: 'false',
}
);
this.comparator = comparator;
this.array.sort(this._arraySortProperty.bind(this));
if (reverse) {
this.array.reverse();
}
return this.array as Entity[];
}
/**
* Sort an array of entities by a specified property's value.
*
* @param property The name of the property to sort entities by.
*/
public sort(
property: string,
{
caseSensitive = false,
reverse = false,
collatorOptions = undefined,
comparator = undefined,
}: SortOptions = {}
): Entity[] {
// Sort by the requested property.
this.sortProperty = property;
this.sortParent = null;
this.collator = new Intl.Collator(
undefined,
collatorOptions || {
sensitivity: caseSensitive ? 'case' : 'base',
}
);
this.comparator = comparator;
this.array.sort(this._arraySortProperty.bind(this));
if (reverse) {
this.array.reverse();
}
return this.array as Entity[];
}
}