forked from btford/write-good
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite-good.js
52 lines (45 loc) · 1.8 KB
/
write-good.js
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
var checks = {
weasel : { fn: require('weasel-words'), explanation: 'is a weasel word' },
illusion : { fn: require('./lib/lexical-illusions'), explanation: 'is repeated' },
so : { fn: require('./lib/starts-with-so'), explanation: 'adds no meaning' },
thereIs : { fn: require('./lib/there-is'), explanation: 'is unnecessary verbiage' },
passive : { fn: require('passive-voice'), explanation: 'may be passive voice' },
adverb : { fn: require('adverb-where'), explanation: 'can weaken meaning'},
tooWordy : { fn: require('too-wordy'), explanation: 'is wordy or unneeded'},
cliches : { fn: require('no-cliches'), explanation: 'is a cliche'},
};
module.exports = function (text, opts) {
opts = opts || {};
var suggestions = [];
Object.keys(checks).forEach(function (checkName) {
if (opts[checkName] !== false) {
suggestions = suggestions.concat(checks[checkName].fn(text).
map(reasonable(checks[checkName].explanation)));
}
});
return dedup(suggestions).sort(function (a, b) {
return a.index < b.index ? -1 : 1;
});
function reasonable (reason) {
return function (suggestion) {
suggestion.reason = '"' +
text.substr(suggestion.index, suggestion.offset) +
'" ' + reason;
return suggestion;
};
}
};
function dedup (suggestions) {
var dupsHash = {};
return suggestions.reduce(function(memo, suggestion) {
var key = suggestion.index + ":" + suggestion.offset;
if (!dupsHash[key]) {
dupsHash[key] = suggestion;
memo.push(suggestion);
} else {
dupsHash[key].reason += " and " + suggestion.reason.substring(suggestion.offset + 3);
}
return memo;
}, []);
}
module.exports.annotate = require('./lib/annotate');