Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 5, 2019
1 parent 2fef5bb commit 97b6988
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 45 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ function sentenceSpacing(options) {
value = toString(child)

// We only check for white-space that is *just* spaces: it’s OK to add
// newlines if `space` is expected.
// line feeds if `space` is expected.
if (!/^ +$/.test(value)) {
continue
}

size = value.length

// Size is never preferred if we want a newline.
// Size is never preferred if we want a line feed.
if (preferred === 0) {
file.warn(
'Expected a newline between sentences, not `' +
Expand Down
92 changes: 49 additions & 43 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,58 +18,64 @@ test('sentenceSpacing(value[, size])', function(t) {
var two = [2]

one.forEach(function(pref) {
t.deepEqual(
retext()
.use(spacing, {preferred: pref})
.processSync(mixed)
.messages.map(String),
['3:14-3:16: Expected `1` space between sentences, not `2`'],
'should catch double spaces when preferred == ' + pref
)
retext()
.use(spacing, {preferred: pref})
.process(mixed, function(err, file) {
t.deepEqual(
[err].concat(file.messages.map(String)),
[null, '3:14-3:16: Expected `1` space between sentences, not `2`'],
'should catch double spaces when preferred == ' + pref
)
})
})

two.forEach(function(pref) {
t.deepEqual(
retext()
.use(spacing, {preferred: pref})
.processSync(mixed)
.messages.map(String),
['1:14-1:15: Expected `2` spaces between sentences, not `1`'],
'should catch single spaces when preferred == ' + pref
)
retext()
.use(spacing, {preferred: pref})
.process(mixed, function(err, file) {
t.deepEqual(
[err].concat(file.messages.map(String)),
[null, '1:14-1:15: Expected `2` spaces between sentences, not `1`'],
'should catch single spaces when preferred == ' + pref
)
})
})

zero.forEach(function(pref) {
t.deepEqual(
retext()
.use(spacing, {preferred: pref})
.processSync(mixed)
.messages.map(String),
[
'1:14-1:15: Expected a newline between sentences, not `1` space',
'3:14-3:16: Expected a newline between sentences, not `2` spaces'
],
'should catch single spaces when preferred == ' + pref
)
retext()
.use(spacing, {preferred: pref})
.process(mixed, function(err, file) {
t.deepEqual(
[err].concat(file.messages.map(String)),
[
null,
'1:14-1:15: Expected a newline between sentences, not `1` space',
'3:14-3:16: Expected a newline between sentences, not `2` spaces'
],
'should catch spaces when preferred == ' + pref
)
})
})

t.deepEqual(
retext()
.use(spacing)
.processSync('One sentence. Three sentences.')
.messages.map(String),
['1:14-1:17: Expected `1` space between sentences, not `3`'],
'should catch more than two spaces'
)
retext()
.use(spacing)
.process('One sentence. Three sentences.', function(err, file) {
t.deepEqual(
[err].concat(file.messages.map(String)),
[null, '1:14-1:17: Expected `1` space between sentences, not `3`'],
'should catch more than two spaces'
)
})

t.deepEqual(
retext()
.use(spacing)
.processSync('One sentence.\tFour sentences.')
.messages.map(String),
[],
'should not emit messages for non-space white-space'
)
retext()
.use(spacing)
.process('One sentence.\tFour sentences.', function(err, file) {
t.deepEqual(
[err].concat(file.messages.map(String)),
[null],
'should not emit messages for non-space white-space'
)
})

t.throws(
function() {
Expand Down

0 comments on commit 97b6988

Please sign in to comment.