Skip to content

Commit bafa019

Browse files
committed
Add support for threshold as a setting
Closes GH-1.
1 parent 5fd99b8 commit bafa019

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,12 @@ function smogToAge(value) {
8989
*
9090
* @param {File} file - Virtual file.
9191
* @param {Node} node - NLCST node.
92+
* @param {number} threshold - Target threshold.
9293
* @param {number} target - Target age.
9394
* @param {Array.<number>} results - Reading-level in age
9495
* for `node` according to several algorithms.
9596
*/
96-
function report(file, node, target, results) {
97+
function report(file, node, threshold, target, results) {
9798
var length = results.length;
9899
var result = 0;
99100
var index = -1;
@@ -107,7 +108,7 @@ function report(file, node, target, results) {
107108

108109
result /= length;
109110

110-
if (result >= SURENESS_THRESHOLD) {
111+
if (result >= threshold) {
111112
if (result >= SURENESS_THRESHOLD_DEFINITELY) {
112113
level = 'Definitely';
113114
} else if (result >= SURENESS_THRESHOLD_VERY) {
@@ -128,7 +129,9 @@ function report(file, node, target, results) {
128129
* @return {Function} - `transformer`.
129130
*/
130131
function attacher(processor, options) {
131-
var targetAge = (options || {}).age || DEFAULT_TARGET_AGE;
132+
var settings = options || {};
133+
var targetAge = settings.age || DEFAULT_TARGET_AGE;
134+
var threshold = settings.threshold || SURENESS_THRESHOLD;
132135

133136
return function (tree, file) {
134137
/**
@@ -216,7 +219,7 @@ function attacher(processor, options) {
216219
'letter': letters
217220
};
218221

219-
report(file, sentence, targetAge, [
222+
report(file, sentence, threshold, targetAge, [
220223
gradeToAge(daleChallFormula.gradeLevel(
221224
daleChallFormula(counts)
222225
)[1]),

readme.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ Detect possibly hard to read sentences.
8787
provide varying results, so your milage might vary with
8888
people actually that age. :wink:
8989

90+
* `threshold` (`number`, default: `4 / 7`)
91+
— By default, 4 out of the 7 algorithms need to agree that
92+
a sentence is higher that the target age and whether it should
93+
be warned about. This can be modified by passing in a new
94+
threshold.
95+
9096
## License
9197

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

test.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ test('readability', function (t) {
5454

5555
retext()
5656
.use(readability, {
57-
'age': 18
57+
'threshold': 5 / 7
5858
})
5959
.process([
6060
'Oberon, also designated Uranus IV, is the outermost ',
@@ -64,6 +64,25 @@ test('readability', function (t) {
6464
].join('\n'), function (err, file) {
6565
t.ifError(err, 'should not fail (#3)');
6666

67+
t.deepEqual(
68+
file.messages.map(String),
69+
[],
70+
'should support a threshold'
71+
);
72+
});
73+
74+
retext()
75+
.use(readability, {
76+
'age': 18
77+
})
78+
.process([
79+
'Oberon, also designated Uranus IV, is the outermost ',
80+
'major moon of the planet Uranus and quite large',
81+
'and massive for a Uranian moon.',
82+
''
83+
].join('\n'), function (err, file) {
84+
t.ifError(err, 'should not fail (#4)');
85+
6786
t.deepEqual(
6887
file.messages.map(String),
6988
[],
@@ -81,7 +100,7 @@ test('readability', function (t) {
81100
'and massive for a Uranian moon.',
82101
''
83102
].join('\n'), function (err, file) {
84-
t.ifError(err, 'should not fail (#4)');
103+
t.ifError(err, 'should not fail (#5)');
85104

86105
t.deepEqual(
87106
file.messages.map(String),
@@ -98,7 +117,7 @@ test('readability', function (t) {
98117
'and second most massive of the Uranian moons.',
99118
''
100119
].join('\n'), function (err, file) {
101-
t.ifError(err, 'should not fail (#5)');
120+
t.ifError(err, 'should not fail (#6)');
102121

103122
t.deepEqual(
104123
file.messages.map(String),
@@ -118,7 +137,7 @@ test('readability', function (t) {
118137
'ninth most massive moon in the Solar System.',
119138
''
120139
].join('\n'), function (err, file) {
121-
t.ifError(err, 'should not fail (#6)');
140+
t.ifError(err, 'should not fail (#7)');
122141

123142
t.deepEqual(
124143
file.messages.map(String),

0 commit comments

Comments
 (0)