Skip to content

Commit 8c57db3

Browse files
committed
Allow to reverse sort order in Stats by prefixing the sort key with a "!"
1 parent fad4021 commit 8c57db3

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

lib/Stats.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,22 @@ class Stats {
2424
return this.compilation.errors.length > 0;
2525
}
2626

27+
// remove a prefixed "!" that can be specified to reverse sort order
2728
normalizeFieldKey(field) {
2829
if(field[0] === "!") {
2930
return field.substr(1);
3031
}
3132
return field;
3233
}
3334

35+
// if a field is prefixed by a "!" reverse sort order
36+
sortOrderRegular(field) {
37+
if(field[0] === "!") {
38+
return false;
39+
}
40+
return true;
41+
}
42+
3443
toJson(options, forToString) {
3544
if(typeof options === "boolean" || typeof options === "string") {
3645
options = Stats.presetToOptions(options);
@@ -87,18 +96,25 @@ class Stats {
8796
};
8897
};
8998

99+
const sortByFieldAndOrder = (fieldKey, a, b) => {
100+
if(a[fieldKey] === null && b[fieldKey] === null) return 0;
101+
if(a[fieldKey] === null) return 1;
102+
if(b[fieldKey] === null) return -1;
103+
if(a[fieldKey] === b[fieldKey]) return 0;
104+
return a[fieldKey] < b[fieldKey] ? -1 : 1;
105+
};
106+
90107
const sortByField = (field) => (a, b) => {
91108
if(!field) {
92109
return 0;
93110
}
94111

95112
const fieldKey = this.normalizeFieldKey(field);
96113

97-
if(a[fieldKey] === null && b[fieldKey] === null) return 0;
98-
if(a[fieldKey] === null) return 1;
99-
if(b[fieldKey] === null) return -1;
100-
if(a[fieldKey] === b[fieldKey]) return 0;
101-
return a[fieldKey] < b[fieldKey] ? -1 : 1;
114+
// if a field is prefixed with a "!" the sort is reversed!
115+
const sortIsRegular = this.sortOrderRegular(field);
116+
117+
return sortByFieldAndOrder(fieldKey, sortIsRegular ? a : b, sortIsRegular ? b : a);
102118
};
103119

104120
const formatError = (e) => {

0 commit comments

Comments
 (0)