Skip to content

Commit 8e119ec

Browse files
committed
Skip tokens when pipeline function returns null
Previously only undefined and empty string return values would skip the token, although the documentation specifically mentioned returning null. This makes sure that null and undefined and empty string all have the same behaviour and updates the documentation accordingly.
1 parent 532d990 commit 8e119ec

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

lib/pipeline.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ lunr.Pipeline.registeredFunctions = Object.create(null)
4444
* or mutate (or add) metadata for a given token.
4545
*
4646
* A pipeline function can indicate that the passed token should be discarded by returning
47-
* null. This token will not be passed to any downstream pipeline functions and will not be
48-
* added to the index.
47+
* null, undefined or an empty string. This token will not be passed to any downstream pipeline
48+
* functions and will not be added to the index.
4949
*
5050
* Multiple tokens can be returned by returning an array of tokens. Each token will be passed
5151
* to any downstream pipeline functions and all will returned tokens will be added to the index.
@@ -208,7 +208,7 @@ lunr.Pipeline.prototype.run = function (tokens) {
208208
for (var j = 0; j < tokens.length; j++) {
209209
var result = fn(tokens[j], j, tokens)
210210

211-
if (result === void 0 || result === '') continue
211+
if (result === null || result === void 0 || result === '') continue
212212

213213
if (Array.isArray(result)) {
214214
for (var k = 0; k < result.length; k++) {

test/pipeline_test.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,23 @@ suite('lunr.Pipeline', function () {
142142
assert.deepEqual(['FOO'], this.pipeline.run(['foo']))
143143
})
144144

145-
test('filters out undefined values', function () {
145+
test('filters out null, undefined and empty string values', function () {
146146
var tokens = [],
147147
output
148148

149149
// only pass on tokens for even token indexes
150+
// return null for 'foo'
151+
// return undefined for 'bar'
152+
// return '' for 'baz'
150153
this.pipeline.add(function (t, i) {
151-
if (i % 2) {
154+
if (i == 4) {
155+
return null
156+
} else if (i == 5) {
157+
return ''
158+
} if (i % 2) {
152159
return t
160+
} else {
161+
return undefined
153162
}
154163
})
155164

@@ -158,7 +167,7 @@ suite('lunr.Pipeline', function () {
158167
return t
159168
})
160169

161-
output = this.pipeline.run(['a', 'b', 'c', 'd'])
170+
output = this.pipeline.run(['a', 'b', 'c', 'd', 'foo', 'bar', 'baz'])
162171

163172
assert.sameMembers(['b', 'd'], tokens)
164173
assert.sameMembers(['b', 'd'], output)

0 commit comments

Comments
 (0)