Skip to content

Commit 6a93e02

Browse files
author
Lior Amsalem
committed
Refactor MultiStats to es2015
1 parent 5abfeea commit 6a93e02

File tree

2 files changed

+99
-61
lines changed

2 files changed

+99
-61
lines changed

lib/MultiStats.js

Lines changed: 66 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,70 +2,78 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
var Stats = require("./Stats");
5+
"use strict";
66

7-
function MultiStats(stats) {
8-
this.stats = stats;
9-
this.hash = stats.map(function(stat) {
10-
return stat.hash;
11-
}).join("");
12-
}
7+
const Stats = require("./Stats");
138

14-
MultiStats.prototype.hasErrors = function() {
15-
return this.stats.map(function(stat) {
16-
return stat.hasErrors();
17-
}).reduce(function(a, b) {
18-
return a || b;
19-
}, false);
20-
};
9+
const optionOrFallback = (optionValue, fallbackValue) => optionValue !== undefined ? optionValue : fallbackValue;
2110

22-
MultiStats.prototype.hasWarnings = function() {
23-
return this.stats.map(function(stat) {
24-
return stat.hasWarnings();
25-
}).reduce(function(a, b) {
26-
return a || b;
27-
}, false);
28-
};
11+
class MultiStats {
12+
constructor(stats) {
13+
this.stats = stats;
14+
this.hash = stats.map((stat) => stat.hash).join("");
15+
}
2916

30-
MultiStats.prototype.toJson = function(options, forToString) {
31-
if(typeof options === "boolean" || typeof options === "string") {
32-
options = Stats.presetToOptions(options);
33-
} else if(!options) {
34-
options = {};
17+
hasErrors() {
18+
return this.stats.map((stat) => stat.hasErrors()).reduce((a, b) => a || b, false);
3519
}
36-
var jsons = this.stats.map((stat, idx) => {
37-
var childOptions = Stats.getChildOptions(options, idx);
38-
var obj = stat.toJson(childOptions, forToString);
39-
obj.name = stat.compilation && stat.compilation.name;
40-
return obj;
41-
});
42-
var showVersion = typeof options.version === "undefined" ? jsons.every(j => j.version) : options.version !== false;
43-
var showHash = typeof options.hash === "undefined" ? jsons.every(j => j.hash) : options.hash !== false;
44-
jsons.forEach(j => {
20+
21+
hasWarnings() {
22+
return this.stats.map((stat) => stat.hasWarnings()).reduce((a, b) => a || b, false);
23+
}
24+
25+
toJson(options, forToString) {
26+
if(typeof options === "boolean" || typeof options === "string") {
27+
options = Stats.presetToOptions(options);
28+
} else if(!options) {
29+
options = {};
30+
}
31+
const jsons = this.stats.map((stat, idx) => {
32+
const childOptions = Stats.getChildOptions(options, idx);
33+
const obj = stat.toJson(childOptions, forToString);
34+
obj.name = stat.compilation && stat.compilation.name;
35+
return obj;
36+
});
37+
const showVersion = typeof options.version === "undefined" ? jsons.every(j => j.version) : options.version !== false;
38+
const showHash = typeof options.hash === "undefined" ? jsons.every(j => j.hash) : options.hash !== false;
39+
jsons.forEach(j => {
40+
if(showVersion)
41+
delete j.version;
42+
});
43+
const obj = {
44+
errors: jsons.reduce((arr, j) => {
45+
return arr.concat(j.errors.map((msg) => {
46+
return `(${j.name}) ${msg}`;
47+
}));
48+
}, []),
49+
warnings: jsons.reduce((arr, j) => {
50+
return arr.concat(j.warnings.map((msg) => {
51+
return `(${j.name}) ${msg}`;
52+
}));
53+
}, [])
54+
};
4555
if(showVersion)
46-
delete j.version;
47-
});
48-
var obj = {
49-
errors: jsons.reduce(function(arr, j) {
50-
return arr.concat(j.errors.map(function(msg) {
51-
return "(" + j.name + ") " + msg;
52-
}));
53-
}, []),
54-
warnings: jsons.reduce(function(arr, j) {
55-
return arr.concat(j.warnings.map(function(msg) {
56-
return "(" + j.name + ") " + msg;
57-
}));
58-
}, [])
59-
};
60-
if(showVersion)
61-
obj.version = require("../package.json").version;
62-
if(showHash)
63-
obj.hash = this.hash;
64-
if(options.children !== false)
65-
obj.children = jsons;
66-
return obj;
67-
};
56+
obj.version = require("../package.json").version;
57+
if(showHash)
58+
obj.hash = this.hash;
59+
if(options.children !== false)
60+
obj.children = jsons;
61+
return obj;
62+
}
63+
64+
toString(options) {
65+
if(typeof options === "boolean" || typeof options === "string") {
66+
options = Stats.presetToOptions(options);
67+
} else if(!options) {
68+
options = {};
69+
}
70+
71+
const useColors = optionOrFallback(options.colors, false);
6872

69-
MultiStats.prototype.toString = Stats.prototype.toString;
73+
const obj = this.toJson(options, true);
74+
75+
return Stats.jsonToString(obj, useColors);
76+
}
77+
}
7078

7179
module.exports = MultiStats;

test/MultiStats.test.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,14 @@ describe("MultiStats", () => {
160160
})
161161
})
162162
];
163+
});
164+
165+
it("returns plain object representation", () => {
163166
myMultiStats = new MultiStats(stats);
164167
result = myMultiStats.toJson({
165168
version: false,
166169
hash: false
167170
});
168-
});
169-
170-
it("returns plain object representation", () => {
171171
result.should.deepEqual({
172172
errors: [
173173
"(abc123-compilation) abc123-error"
@@ -197,6 +197,36 @@ describe("MultiStats", () => {
197197
]
198198
});
199199
});
200+
201+
it("returns plain object representation with json set to true", () => {
202+
myMultiStats = new MultiStats(stats);
203+
result = myMultiStats.toJson(true);
204+
result.should.deepEqual({
205+
errors: [
206+
"(abc123-compilation) abc123-error"
207+
],
208+
warnings: [
209+
"(abc123-compilation) abc123-warning",
210+
"(xyz890-compilation) xyz890-warning-1",
211+
"(xyz890-compilation) xyz890-warning-2"
212+
],
213+
hash: "abc123xyz890",
214+
children: [{
215+
warnings: ["abc123-warning"],
216+
errors: ["abc123-error"],
217+
name: "abc123-compilation"
218+
},
219+
{
220+
warnings: [
221+
"xyz890-warning-1",
222+
"xyz890-warning-2"
223+
],
224+
errors: [],
225+
name: "xyz890-compilation"
226+
}
227+
]
228+
});
229+
});
200230
});
201231

202232
describe("toString", () => {

0 commit comments

Comments
 (0)