@nymphjs/sorter 1.0.0-beta.12 → 1.0.0-beta.121

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/Sorter.js ADDED
@@ -0,0 +1,216 @@
1
+ /**
2
+ * Entity Array Sorter
3
+ *
4
+ * Sorting functions sort the array in place and also return the sorted array.
5
+ */
6
+ export default class Sorter {
7
+ array;
8
+ sortProperty = null;
9
+ sortParent = null;
10
+ collator = new Intl.Collator();
11
+ comparator = undefined;
12
+ identiticalResolver = (a, b) => 0;
13
+ constructor(array) {
14
+ this.array = array;
15
+ }
16
+ _arraySortProperty(a, b) {
17
+ const prop = this.sortProperty;
18
+ const parent = this.sortParent;
19
+ if (parent != null &&
20
+ a[parent] instanceof Object &&
21
+ b[parent] instanceof Object) {
22
+ const aParentProp = a[parent][prop];
23
+ const bParentProp = b[parent][prop];
24
+ if (typeof aParentProp !== 'undefined' ||
25
+ typeof bParentProp !== 'undefined') {
26
+ if (this.comparator != null) {
27
+ const result = this.comparator(aParentProp, bParentProp);
28
+ if (result !== 0) {
29
+ return result;
30
+ }
31
+ }
32
+ else if (typeof aParentProp === 'string' &&
33
+ typeof bParentProp === 'string') {
34
+ const result = this.collator.compare(aParentProp, bParentProp);
35
+ if (result !== 0) {
36
+ return result;
37
+ }
38
+ }
39
+ else if (aParentProp > bParentProp) {
40
+ return 1;
41
+ }
42
+ else if (aParentProp < bParentProp) {
43
+ return -1;
44
+ }
45
+ }
46
+ }
47
+ // If they have the same parent, order them by their own prop.
48
+ const aProp = a[prop];
49
+ const bProp = b[prop];
50
+ if (this.comparator != null) {
51
+ const result = this.comparator(aProp, bProp);
52
+ if (result === 0) {
53
+ return this.identiticalResolver(a, b);
54
+ }
55
+ else {
56
+ return result;
57
+ }
58
+ }
59
+ else if (typeof aProp === 'string' && typeof bProp === 'string') {
60
+ const result = this.collator.compare(aProp, bProp);
61
+ if (result === 0) {
62
+ return this.identiticalResolver(a, b);
63
+ }
64
+ else {
65
+ return result;
66
+ }
67
+ }
68
+ else if (aProp > bProp) {
69
+ return 1;
70
+ }
71
+ else if (aProp < bProp) {
72
+ return -1;
73
+ }
74
+ else {
75
+ return this.identiticalResolver(a, b);
76
+ }
77
+ }
78
+ /**
79
+ * Sort an array of entities hierarchically by a specified property's value.
80
+ *
81
+ * Entities will be placed immediately after their parents. The
82
+ * `parentProperty` property, if present, should hold either null, undefined,
83
+ * or the entity's parent.
84
+ *
85
+ * @param property The name of the property to sort entities by.
86
+ * @param parentProperty The name of the property which holds the parent of the entity.
87
+ */
88
+ hsort(property, parentProperty, { caseSensitive = false, reverse = false, collatorOptions = undefined, comparator = undefined, identiticalResolver = (a, b) => 0, } = {}) {
89
+ if (typeof parentProperty === 'undefined' || parentProperty === null) {
90
+ // Just sort by the requested property.
91
+ this.sort(property, {
92
+ caseSensitive,
93
+ reverse,
94
+ collatorOptions,
95
+ comparator,
96
+ });
97
+ return this.array;
98
+ }
99
+ this.sortProperty = property;
100
+ this.sortParent = null;
101
+ this.collator = new Intl.Collator(undefined, collatorOptions || {
102
+ sensitivity: caseSensitive ? 'case' : 'base',
103
+ });
104
+ this.comparator = comparator;
105
+ this.identiticalResolver = identiticalResolver;
106
+ const sort = (array) => {
107
+ // Sort by the requested property.
108
+ array.sort(this._arraySortProperty.bind(this));
109
+ if (reverse) {
110
+ array.reverse();
111
+ }
112
+ return array;
113
+ };
114
+ // Pick out all of the children.
115
+ const guidEntries = {};
116
+ const newEntries = new Map();
117
+ // There is no incrementor in this for loop on purpose. It is incremented
118
+ // conditionally.
119
+ for (let i = 0; i < this.array.length;) {
120
+ const entity = this.array[i];
121
+ const parent = entity[parentProperty];
122
+ if (parent == null) {
123
+ i++;
124
+ }
125
+ else if (parent.guid != null) {
126
+ this.array.splice(i, 1);
127
+ if (!(parent.guid in guidEntries)) {
128
+ guidEntries[parent.guid] = [];
129
+ }
130
+ guidEntries[parent.guid].push(entity);
131
+ }
132
+ else {
133
+ this.array.splice(i, 1);
134
+ let entries = [];
135
+ if (newEntries.has(parent)) {
136
+ entries = newEntries.get(parent) || [];
137
+ }
138
+ entries.push(entity);
139
+ newEntries.set(parent, entries);
140
+ }
141
+ }
142
+ // Sort the top level entries.
143
+ sort(this.array);
144
+ // Now place all of the children back in order.
145
+ for (let i = 0; i < this.array.length; i++) {
146
+ const entity = this.array[i];
147
+ if (entity.guid != null) {
148
+ if (entity.guid in guidEntries) {
149
+ this.array.splice(i + 1, 0, ...sort(guidEntries[entity.guid]));
150
+ delete guidEntries[entity.guid];
151
+ }
152
+ }
153
+ else {
154
+ if (newEntries.has(entity)) {
155
+ this.array.splice(i + 1, 0, ...sort(newEntries.get(entity.guid) || []));
156
+ newEntries.delete(entity);
157
+ }
158
+ }
159
+ }
160
+ // Now place any orphans on the end.
161
+ for (let orphans of Object.values(guidEntries)) {
162
+ this.array.splice(this.array.length, 0, ...sort(orphans));
163
+ }
164
+ for (let [parent, orphans] of newEntries) {
165
+ this.array.splice(this.array.length, 0, ...sort(orphans));
166
+ }
167
+ return this.array;
168
+ }
169
+ /**
170
+ * Sort an array of entities by parent and a specified property's value.
171
+ *
172
+ * Entities' will be sorted by their parents' properties, then the entities'
173
+ * properties.
174
+ *
175
+ * @param property The name of the property to sort entities by.
176
+ * @param parentProperty The name of the property which holds the parent of the entity.
177
+ */
178
+ psort(property, parentProperty, { caseSensitive = false, reverse = false, collatorOptions = undefined, comparator = undefined, identiticalResolver = (a, b) => 0, } = {}) {
179
+ // Sort by the requested property.
180
+ this.sortProperty = property;
181
+ this.sortParent = parentProperty;
182
+ this.collator = new Intl.Collator(undefined, collatorOptions || {
183
+ sensitivity: caseSensitive ? 'case' : 'base',
184
+ caseFirst: 'false',
185
+ });
186
+ this.comparator = comparator;
187
+ this.identiticalResolver = identiticalResolver;
188
+ this.array.sort(this._arraySortProperty.bind(this));
189
+ if (reverse) {
190
+ this.array.reverse();
191
+ }
192
+ return this.array;
193
+ }
194
+ /**
195
+ * Sort an array of entities by a specified property's value.
196
+ *
197
+ * @param property The name of the property to sort entities by.
198
+ * @param identiticalResolver A function to resolve order of identical entities.
199
+ */
200
+ sort(property, { caseSensitive = false, reverse = false, collatorOptions = undefined, comparator = undefined, identiticalResolver = (a, b) => 0, } = {}) {
201
+ // Sort by the requested property.
202
+ this.sortProperty = property;
203
+ this.sortParent = null;
204
+ this.collator = new Intl.Collator(undefined, collatorOptions || {
205
+ sensitivity: caseSensitive ? 'case' : 'base',
206
+ });
207
+ this.comparator = comparator;
208
+ this.identiticalResolver = identiticalResolver;
209
+ this.array.sort(this._arraySortProperty.bind(this));
210
+ if (reverse) {
211
+ this.array.reverse();
212
+ }
213
+ return this.array;
214
+ }
215
+ }
216
+ //# sourceMappingURL=Sorter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Sorter.js","sourceRoot":"","sources":["../src/Sorter.ts"],"names":[],"mappings":"AAaA;;;;GAIG;AACH,MAAM,CAAC,OAAO,OAAO,MAAM;IAClB,KAAK,CAAoC;IACxC,YAAY,GAAkB,IAAI,CAAC;IACnC,UAAU,GAAkB,IAAI,CAAC;IACjC,QAAQ,GAAkB,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;IAC9C,UAAU,GAA6C,SAAS,CAAC;IACjE,mBAAmB,GAAqC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;IAE5E,YAAY,KAAe;QACzB,IAAI,CAAC,KAAK,GAAG,KAA0C,CAAC;IAC1D,CAAC;IAEO,kBAAkB,CACxB,CAAgC,EAChC,CAAgC;QAEhC,MAAM,IAAI,GAAG,IAAI,CAAC,YAAsB,CAAC;QACzC,MAAM,MAAM,GAAG,IAAI,CAAC,UAAoB,CAAC;QAEzC,IACE,MAAM,IAAI,IAAI;YACd,CAAC,CAAC,MAAM,CAAC,YAAY,MAAM;YAC3B,CAAC,CAAC,MAAM,CAAC,YAAY,MAAM,EAC3B,CAAC;YACD,MAAM,WAAW,GAAG,CAAC,CAAC,MAAM,CA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AC;IAChC,CAAC;CACF"}
@@ -0,0 +1,435 @@
1
+ import Sorter from './Sorter.js';
2
+ import { TestModel } from './testArtifacts.js';
3
+ describe('Sorter', () => {
4
+ it('sorts entities by a string property', () => {
5
+ const entities = [];
6
+ for (const prop of ['d', 'a', 'C', 'e', 'b']) {
7
+ const entity = new TestModel();
8
+ entity.name = prop;
9
+ entities.push(entity);
10
+ }
11
+ const sorter = new Sorter(entities);
12
+ sorter.sort('name');
13
+ expect(sorter.array.map((entity) => entity.name)).toEqual([
14
+ 'a',
15
+ 'b',
16
+ 'C',
17
+ 'd',
18
+ 'e',
19
+ ]);
20
+ sorter.sort('name', { reverse: true });
21
+ expect(sorter.array.map((entity) => entity.name)).toEqual([
22
+ 'e',
23
+ 'd',
24
+ 'C',
25
+ 'b',
26
+ 'a',
27
+ ]);
28
+ });
29
+ it('sorts entities by a number property', () => {
30
+ const entities = [];
31
+ for (const prop of [4, 300, 5, 1, 2]) {
32
+ const entity = new TestModel();
33
+ entity.number = prop;
34
+ entities.push(entity);
35
+ }
36
+ const sorter = new Sorter(entities);
37
+ sorter.sort('number');
38
+ expect(sorter.array.map((entity) => entity.number)).toEqual([
39
+ 1, 2, 4, 5, 300,
40
+ ]);
41
+ sorter.sort('number', { reverse: true });
42
+ expect(sorter.array.map((entity) => entity.number)).toEqual([
43
+ 300, 5, 4, 2, 1,
44
+ ]);
45
+ });
46
+ it('sorts entities by a number property with an identical resolver', () => {
47
+ const entities = [];
48
+ for (const prop of [4, 300, 5, 1, 2]) {
49
+ const entity = new TestModel();
50
+ entity.number = 50;
51
+ entity.fallback = prop;
52
+ entities.push(entity);
53
+ }
54
+ const sorter = new Sorter(entities);
55
+ sorter.sort('number', {
56
+ identiticalResolver: (a, b) => (a.fallback || 0) - (b.fallback || 0),
57
+ });
58
+ expect(sorter.array.map((entity) => entity.fallback)).toEqual([
59
+ 1, 2, 4, 5, 300,
60
+ ]);
61
+ sorter.sort('number', {
62
+ reverse: true,
63
+ identiticalResolver: (a, b) => (a.fallback || 0) - (b.fallback || 0),
64
+ });
65
+ expect(sorter.array.map((entity) => entity.fallback)).toEqual([
66
+ 300, 5, 4, 2, 1,
67
+ ]);
68
+ });
69
+ it('sorts entities by a string property with an identical resolver', () => {
70
+ const entities = [];
71
+ for (const prop of [4, 300, 5, 1, 2]) {
72
+ const entity = new TestModel();
73
+ entity.name = 'John';
74
+ entity.fallback = prop;
75
+ entities.push(entity);
76
+ }
77
+ const sorter = new Sorter(entities);
78
+ sorter.sort('name', {
79
+ identiticalResolver: (a, b) => (a.fallback || 0) - (b.fallback || 0),
80
+ });
81
+ expect(sorter.array.map((entity) => entity.fallback)).toEqual([
82
+ 1, 2, 4, 5, 300,
83
+ ]);
84
+ sorter.sort('name', {
85
+ reverse: true,
86
+ identiticalResolver: (a, b) => (a.fallback || 0) - (b.fallback || 0),
87
+ });
88
+ expect(sorter.array.map((entity) => entity.fallback)).toEqual([
89
+ 300, 5, 4, 2, 1,
90
+ ]);
91
+ });
92
+ it('sorts entities case sensitively', () => {
93
+ const entities = [];
94
+ for (const prop of ['America', 'america', 'baseball', 'Bernard', 'zebra']) {
95
+ const entity = new TestModel();
96
+ entity.name = prop;
97
+ entities.push(entity);
98
+ }
99
+ const sorter = new Sorter(entities);
100
+ sorter.sort('name');
101
+ expect(sorter.array.map((entity) => entity.name)).toEqual([
102
+ 'America',
103
+ 'america',
104
+ 'baseball',
105
+ 'Bernard',
106
+ 'zebra',
107
+ ]);
108
+ sorter.sort('name', { caseSensitive: true });
109
+ expect(sorter.array.map((entity) => entity.name)).toEqual([
110
+ 'america',
111
+ 'America',
112
+ 'baseball',
113
+ 'Bernard',
114
+ 'zebra',
115
+ ]);
116
+ sorter.sort('name', {
117
+ collatorOptions: {
118
+ sensitivity: 'case',
119
+ caseFirst: 'upper',
120
+ },
121
+ });
122
+ expect(sorter.array.map((entity) => entity.name)).toEqual([
123
+ 'America',
124
+ 'america',
125
+ 'baseball',
126
+ 'Bernard',
127
+ 'zebra',
128
+ ]);
129
+ sorter.sort('name', {
130
+ comparator: (a, b) => (a < b ? -1 : a > b ? 1 : 0),
131
+ });
132
+ expect(sorter.array.map((entity) => entity.name)).toEqual([
133
+ 'America',
134
+ 'Bernard',
135
+ 'america',
136
+ 'baseball',
137
+ 'zebra',
138
+ ]);
139
+ });
140
+ it('sorts entities numerically', () => {
141
+ const entities = [];
142
+ for (const prop of [
143
+ '1: first',
144
+ '10: tenth',
145
+ '40: fourtieth',
146
+ '3: third',
147
+ '2: second',
148
+ ]) {
149
+ const entity = new TestModel();
150
+ entity.name = prop;
151
+ entities.push(entity);
152
+ }
153
+ const sorter = new Sorter(entities);
154
+ sorter.sort('name');
155
+ expect(sorter.array.map((entity) => entity.name)).toEqual([
156
+ '1: first',
157
+ '10: tenth',
158
+ '2: second',
159
+ '3: third',
160
+ '40: fourtieth',
161
+ ]);
162
+ sorter.sort('name', {
163
+ collatorOptions: {
164
+ numeric: true,
165
+ },
166
+ });
167
+ expect(sorter.array.map((entity) => entity.name)).toEqual([
168
+ '1: first',
169
+ '2: second',
170
+ '3: third',
171
+ '10: tenth',
172
+ '40: fourtieth',
173
+ ]);
174
+ });
175
+ it('sorts entities by parent', () => {
176
+ const firstParent = new TestModel();
177
+ firstParent.name = 'Aaron';
178
+ const secondParent = new TestModel();
179
+ secondParent.name = 'Kevin';
180
+ const entities = [
181
+ (() => {
182
+ const entity = new TestModel();
183
+ entity.name = 'Sarah';
184
+ entity.parent = firstParent;
185
+ return entity;
186
+ })(),
187
+ (() => {
188
+ const entity = new TestModel();
189
+ entity.name = 'Jimmy';
190
+ entity.parent = firstParent;
191
+ return entity;
192
+ })(),
193
+ (() => {
194
+ const entity = new TestModel();
195
+ entity.name = 'Mary';
196
+ entity.parent = firstParent;
197
+ return entity;
198
+ })(),
199
+ (() => {
200
+ const entity = new TestModel();
201
+ entity.name = 'Jessica';
202
+ entity.parent = secondParent;
203
+ return entity;
204
+ })(),
205
+ firstParent,
206
+ secondParent,
207
+ ];
208
+ const sorter = new Sorter(entities);
209
+ sorter.psort('name', 'parent');
210
+ expect(sorter.array.map((entity) => entity.name)).toEqual([
211
+ 'Aaron',
212
+ 'Jimmy',
213
+ 'Kevin',
214
+ 'Mary',
215
+ 'Sarah',
216
+ 'Jessica',
217
+ ]);
218
+ });
219
+ it('sorts entities by parent with an identical resolver', () => {
220
+ const firstParent = new TestModel();
221
+ firstParent.name = 'Aaron';
222
+ firstParent.fallback = 1;
223
+ const secondParent = new TestModel();
224
+ secondParent.name = 'Kevin';
225
+ secondParent.fallback = 2;
226
+ const entities = [
227
+ (() => {
228
+ const entity = new TestModel();
229
+ entity.name = 'Steve';
230
+ entity.fallback = 3;
231
+ entity.parent = firstParent;
232
+ return entity;
233
+ })(),
234
+ (() => {
235
+ const entity = new TestModel();
236
+ entity.name = 'Steve';
237
+ entity.fallback = 5;
238
+ entity.parent = firstParent;
239
+ return entity;
240
+ })(),
241
+ (() => {
242
+ const entity = new TestModel();
243
+ entity.name = 'Steve';
244
+ entity.fallback = 4;
245
+ entity.parent = firstParent;
246
+ return entity;
247
+ })(),
248
+ (() => {
249
+ const entity = new TestModel();
250
+ entity.name = 'Steve';
251
+ entity.fallback = 6;
252
+ entity.parent = secondParent;
253
+ return entity;
254
+ })(),
255
+ firstParent,
256
+ secondParent,
257
+ ];
258
+ const sorter = new Sorter(entities);
259
+ sorter.psort('name', 'parent', {
260
+ identiticalResolver: (a, b) => (a.fallback || 0) - (b.fallback || 0),
261
+ });
262
+ expect(sorter.array.map((entity) => entity.fallback)).toEqual([
263
+ 1, 2, 3, 4, 5, 6,
264
+ ]);
265
+ });
266
+ it('sorts entities hierarchically', () => {
267
+ const getEntries = () => {
268
+ const firstParent = new TestModel();
269
+ firstParent.name = 'Herbert';
270
+ const secondParent = new TestModel();
271
+ secondParent.name = 'Anthony';
272
+ const subParent = new TestModel();
273
+ subParent.name = 'Lamar';
274
+ subParent.parent = secondParent;
275
+ return [
276
+ (() => {
277
+ const entity = new TestModel();
278
+ entity.name = 'Jacob';
279
+ entity.parent = firstParent;
280
+ return entity;
281
+ })(),
282
+ (() => {
283
+ const entity = new TestModel();
284
+ entity.name = 'Joshua';
285
+ entity.parent = firstParent;
286
+ return entity;
287
+ })(),
288
+ subParent,
289
+ (() => {
290
+ const entity = new TestModel();
291
+ entity.name = 'Peter';
292
+ entity.parent = secondParent;
293
+ return entity;
294
+ })(),
295
+ (() => {
296
+ const entity = new TestModel();
297
+ entity.name = 'Warren';
298
+ entity.parent = subParent;
299
+ return entity;
300
+ })(),
301
+ (() => {
302
+ const entity = new TestModel();
303
+ entity.name = 'Arthur';
304
+ entity.parent = subParent;
305
+ return entity;
306
+ })(),
307
+ firstParent,
308
+ secondParent,
309
+ ];
310
+ };
311
+ const entities = [];
312
+ for (let i = 0; i < 100; i++) {
313
+ entities.push(...getEntries());
314
+ }
315
+ const sorter = new Sorter(entities);
316
+ sorter.hsort('name', 'parent');
317
+ expect(sorter.array.map((entity) => {
318
+ let output = '';
319
+ let parent = entity.parent;
320
+ while (parent) {
321
+ output += '- ';
322
+ parent = parent.parent;
323
+ }
324
+ return output + entity.name;
325
+ })).toEqual([
326
+ ...[...Array(100)]
327
+ .map(() => [
328
+ 'Anthony',
329
+ '- Lamar',
330
+ '- - Arthur',
331
+ '- - Warren',
332
+ '- Peter',
333
+ ])
334
+ .flat(),
335
+ ...[...Array(100)].map(() => ['Herbert', '- Jacob', '- Joshua']).flat(),
336
+ ]);
337
+ });
338
+ it('sorts entities hierarchically with an identical resolver', () => {
339
+ const firstParent = new TestModel();
340
+ firstParent.name = 'Herbert';
341
+ firstParent.fallback = 1;
342
+ const secondParent = new TestModel();
343
+ secondParent.name = 'Anthony';
344
+ secondParent.fallback = 2;
345
+ const subParent = new TestModel();
346
+ subParent.name = 'Lamar';
347
+ subParent.fallback = 3;
348
+ subParent.parent = secondParent;
349
+ const entities = [
350
+ (() => {
351
+ const entity = new TestModel();
352
+ entity.name = 'Peter';
353
+ entity.fallback = 5;
354
+ entity.parent = firstParent;
355
+ return entity;
356
+ })(),
357
+ (() => {
358
+ const entity = new TestModel();
359
+ entity.name = 'Peter';
360
+ entity.fallback = 4;
361
+ entity.parent = firstParent;
362
+ return entity;
363
+ })(),
364
+ subParent,
365
+ (() => {
366
+ const entity = new TestModel();
367
+ entity.name = 'Peter';
368
+ entity.fallback = 7;
369
+ entity.parent = secondParent;
370
+ return entity;
371
+ })(),
372
+ (() => {
373
+ const entity = new TestModel();
374
+ entity.name = 'Peter';
375
+ entity.fallback = 8;
376
+ entity.parent = subParent;
377
+ return entity;
378
+ })(),
379
+ (() => {
380
+ const entity = new TestModel();
381
+ entity.name = 'Peter';
382
+ entity.fallback = 6;
383
+ entity.parent = subParent;
384
+ return entity;
385
+ })(),
386
+ firstParent,
387
+ secondParent,
388
+ ];
389
+ const sorter = new Sorter(entities);
390
+ sorter.hsort('name', 'parent', {
391
+ identiticalResolver: (a, b) => (a.fallback || 0) - (b.fallback || 0),
392
+ });
393
+ expect(sorter.array.map((entity) => {
394
+ let output = '';
395
+ let parent = entity.parent;
396
+ while (parent) {
397
+ output += '- ';
398
+ parent = parent.parent;
399
+ }
400
+ return output + entity.fallback;
401
+ })).toEqual(['2', '- 3', '- - 6', '- - 8', '- 7', '1', '- 4', '- 5']);
402
+ });
403
+ it('sorts array in place', () => {
404
+ const entities = [];
405
+ for (const prop of ['d', 'a', 'c', 'e', 'b']) {
406
+ const entity = new TestModel();
407
+ entity.name = prop;
408
+ entities.push(entity);
409
+ }
410
+ const sorter = new Sorter(entities);
411
+ const sorted = sorter.sort('name');
412
+ expect(entities.map((entity) => entity.name)).toEqual([
413
+ 'a',
414
+ 'b',
415
+ 'c',
416
+ 'd',
417
+ 'e',
418
+ ]);
419
+ expect(sorter.array.map((entity) => entity.name)).toEqual([
420
+ 'a',
421
+ 'b',
422
+ 'c',
423
+ 'd',
424
+ 'e',
425
+ ]);
426
+ expect(sorted.map((entity) => entity.name)).toEqual([
427
+ 'a',
428
+ 'b',
429
+ 'c',
430
+ 'd',
431
+ 'e',
432
+ ]);
433
+ });
434
+ });
435
+ //# sourceMappingURL=Sorter.test.js.map