Skip to content

Commit cb9576b

Browse files
justinsajwilsson
authored andcommitted
Extract utility functions to utils.js (#52)
Extracted util methods: getSeverityCount, isError, isWarning, and pluralize.
1 parent 7271a9a commit cb9576b

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

index.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const PluginError = require('plugin-error');
44
const Lesshint = require('lesshint').Lesshint;
55
const through = require('through2');
66

7-
const { isExcluded } = require('./utils');
7+
const { getSeverityCount, isError, isExcluded, isWarning, pluralize } = require('./utils');
88

99
const lesshintPlugin = (options = {}) => {
1010
const lesshint = new Lesshint();
@@ -33,9 +33,7 @@ const lesshintPlugin = (options = {}) => {
3333
const contents = file.contents.toString();
3434
const results = lesshint.checkString(contents, file.path);
3535

36-
warningCount = results.reduce((sum, result) => {
37-
return sum + (result.severity === 'warning' ? 1 : 0);
38-
}, warningCount);
36+
warningCount += getSeverityCount(results, isWarning);
3937

4038
file.lesshint = {
4139
resultCount: results.length,
@@ -55,11 +53,10 @@ const lesshintPlugin = (options = {}) => {
5553
showStack: false,
5654
}));
5755
} else if (warningCount > maxWarnings) {
58-
const count = (warningCount === 1 ? 'warning' : 'warnings');
59-
const message = `Failed with ${ warningCount } ${ count }. Maximum allowed is ${ options.maxWarnings }.`;
56+
const message = `Failed with ${ warningCount } ${ pluralize('warning', warningCount) }. Maximum allowed is ${ options.maxWarnings }.`;
6057

6158
this.emit('error', new PluginError('gulp-lesshint', message, {
62-
name: 'LesshintError'
59+
name: 'LesshintError',
6360
}));
6461
}
6562

@@ -92,9 +89,7 @@ lesshintPlugin.failOnError = () => {
9289

9390
return through.obj((file, enc, cb) => {
9491
if (file.lesshint) {
95-
errorCount += file.lesshint.results.reduce((count, result) => {
96-
return count + (result.severity === 'error');
97-
}, 0);
92+
errorCount += getSeverityCount(file.lesshint.results, isError);
9893
}
9994

10095
return cb(null, file);
@@ -103,10 +98,10 @@ lesshintPlugin.failOnError = () => {
10398
return cb();
10499
}
105100

106-
const message = `Failed with ${ errorCount } ` + (errorCount === 1 ? 'error' : 'errors');
101+
const message = `Failed with ${ errorCount } ${ pluralize('error', errorCount) }`;
107102

108103
this.emit('error', new PluginError('gulp-lesshint', message, {
109-
name: 'LesshintError'
104+
name: 'LesshintError',
110105
}));
111106

112107
return cb();

utils.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
const minimatch = require('minimatch');
44

55
module.exports = {
6+
getSeverityCount (results, isSeverityFn) {
7+
return results.reduce((sum, result) => {
8+
return sum + (isSeverityFn(result.severity) ? 1 : 0);
9+
}, 0);
10+
},
11+
12+
isError (severity) {
13+
return severity === 'error';
14+
},
15+
616
isExcluded (config, checkPath) {
717
const excludedFiles = config && config.excludedFiles || [];
818

@@ -12,4 +22,12 @@ module.exports = {
1222
});
1323
});
1424
},
25+
26+
isWarning (severity) {
27+
return severity === 'warning';
28+
},
29+
30+
pluralize (singular, count) {
31+
return count === 1 ? singular : `${ singular }s`;
32+
},
1533
};

0 commit comments

Comments
 (0)