Skip to content

Commit

Permalink
fix: Add a test to make sure we search using a boolean AND operation
Browse files Browse the repository at this point in the history
  • Loading branch information
marcbachmann committed May 9, 2020
1 parent 6b67456 commit 2e86556
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 34 deletions.
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ function prepareSimpleTextSearch (collection, property) {

return function simpleTextSearch (q) {
if (!collection || !q) return collection
const regex = toRegex(q)
const { regex, length } = toRegex(q)
const result = []
for (const { pruned, elem } of cachedPrunedElements || prunedElements()) {
if (regex.test(pruned)) result.push(elem)
const match = pruned.match(regex)
if (match && match.length === length) result.push(elem)
}
return result
}
Expand All @@ -49,7 +50,10 @@ function toRegex (str) {
if (!/\b/.test(token)) continue
content.push(token.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d'))
}
return new RegExp(`(${content.join('|')})`, 'i')
return {
regex: new RegExp(`(${content.join('|')})`, 'ig'),
length: content.length
}
}

var replaceChar = charReplacer()
Expand Down
69 changes: 38 additions & 31 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,53 @@
var assert = require('assert')
var search = require('./')
const assert = require('assert')
const search = require('./')

// Returns matches
var get = search(['foo', 'bar', 'foobar'])
var res = get('foo')
const get = search(['foo', 'bar', 'foobar'])
const res = get('foo')
assert.strictEqual(res[0], 'foo')
assert.strictEqual(res[1], 'foobar')
assert.strictEqual(res.length, 2)

// Matches whole object and returns same object instances
var arr2 = [{ id: 1, name: 'Test' }, { id: 2, name: 'Marc' }]
var get2 = search(arr2)
var res2 = get2('test')
const arr2 = [{ id: 1, name: 'Test' }, { id: 2, name: 'Marc' }]
const get2 = search(arr2)
const res2a = get2('test')

assert.strictEqual(res2[0], arr2[0])
assert.strictEqual(res2.length, 1)
assert.strictEqual(res2a[0], arr2[0])
assert.strictEqual(res2a.length, 1)

res2 = get2(1)
assert.strictEqual(res2[0], arr2[0])
assert.strictEqual(res2.length, 1)
const res2b = get2(1)
assert.strictEqual(res2b[0], arr2[0])
assert.strictEqual(res2b.length, 1)

res2 = get2(2)
assert.strictEqual(res2[0], arr2[1])
assert.strictEqual(res2.length, 1)
const res2c = get2(2)
assert.strictEqual(res2c[0], arr2[1])
assert.strictEqual(res2c.length, 1)

// Allows to restrict search one property
var arr3 = [{ id: 1, name: 'Test' }, { id: 2, name: 'Marc' }]
var get3 = search(arr3, 'name')
var res3 = get3('Marc')
assert.strictEqual(res3[0], arr3[1])
assert.strictEqual(res3.length, 1)
const arr3 = [{ id: 1, name: 'Test' }, { id: 2, name: 'Marc' }]
const get3 = search(arr3, 'name')
const res3a = get3('Marc')
assert.strictEqual(res3a[0], arr3[1])
assert.strictEqual(res3a.length, 1)

res3 = get3(2)
assert.strictEqual(res3.length, 0)
const res3b = get3(2)
assert.strictEqual(res3b.length, 0)

// Search using umlauts is supported
var arr4 = [{ id: 1, name: 'Zürich' }, { id: 2, name: 'Reykjavík' }]
var get4 = search(arr4, 'name')
var res4 = get4('zurich')
assert.strictEqual(res4[0], arr4[0])
assert.strictEqual(res4.length, 1)

res4 = get4('reykjavik')
assert.strictEqual(res4[0], arr4[1])
assert.strictEqual(res4.length, 1)
const arr4 = [{ id: 1, name: 'Zürich' }, { id: 2, name: 'Reykjavík' }]
const get4 = search(arr4, 'name')
const res4a = get4('zurich')
assert.strictEqual(res4a[0], arr4[0])
assert.strictEqual(res4a.length, 1)

const res4b = get4('reykjavik')
assert.strictEqual(res4b[0], arr4[1])
assert.strictEqual(res4b.length, 1)

const res4c = get4('zu rich')
assert.strictEqual(res4c[0], arr4[0])
assert.strictEqual(res4c.length, 1)

const res4d = get4('zu reyk')
assert.strictEqual(res4d.length, 0)

0 comments on commit 2e86556

Please sign in to comment.