Skip to content

Commit bd67462

Browse files
committed
Add support for minWords as a setting
1 parent cab8ece commit bd67462

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

Diff for: index.js

+10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ var spacheFormula = require('spache-formula');
3232
*/
3333

3434
var DEFAULT_TARGET_AGE = 16;
35+
var WORDYNESS_THRESHOLD = 5;
3536
var SURENESS_THRESHOLD = 4 / 7;
3637
var SURENESS_THRESHOLD_VERY = 5 / 7;
3738
var SURENESS_THRESHOLD_DEFINITELY = 6 / 7;
@@ -132,6 +133,11 @@ function attacher(processor, options) {
132133
var settings = options || {};
133134
var targetAge = settings.age || DEFAULT_TARGET_AGE;
134135
var threshold = settings.threshold || SURENESS_THRESHOLD;
136+
var minWords = settings.minWords;
137+
138+
if (minWords === null || minWords === undefined) {
139+
minWords = WORDYNESS_THRESHOLD;
140+
}
135141

136142
return function (tree, file) {
137143
/**
@@ -207,6 +213,10 @@ function attacher(processor, options) {
207213
}
208214
});
209215

216+
if (wordCount < minWords) {
217+
return;
218+
}
219+
210220
counts = {
211221
'complexPolysillabicWord': complexPolysillabicWord,
212222
'polysillabicWord': polysillabicWord,

Diff for: readme.md

+8
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ Detect possibly hard to read sentences.
9393
be warned about. This can be modified by passing in a new
9494
threshold.
9595

96+
* `minWords` (`number`, default: `5`)
97+
— Minimum number of words a sentence should have when warning.
98+
Most algorithms are designed to take a large sample of
99+
sentences to detect the body’s reading level. This plug-in,
100+
however, works on a per-sentence basis. This makes the results
101+
quite skewered when said sentence has, for example, a few long
102+
words or some unknown ones.
103+
96104
## License
97105

98106
[MIT][license] © [Titus Wormer][author]

Diff for: test.js

+26
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,31 @@ test('readability', function (t) {
146146
);
147147
});
148148

149+
retext()
150+
.use(readability)
151+
.process('Honorificabilitudinitatibus.', function (err, file) {
152+
t.ifError(err, 'should not fail (#8)');
153+
154+
t.deepEqual(
155+
file.messages.map(String),
156+
[],
157+
'should support minWords (default)'
158+
);
159+
});
160+
161+
retext()
162+
.use(readability, {
163+
'minWords': 0
164+
})
165+
.process('Honorificabilitudinitatibus.', function (err, file) {
166+
t.ifError(err, 'should not fail (#8)');
167+
168+
t.deepEqual(
169+
file.messages.map(String),
170+
['1:1-1:29: Quite hard to read sentence'],
171+
'should support `minWords` (config)'
172+
);
173+
});
174+
149175
t.end();
150176
});

0 commit comments

Comments
 (0)