diff --git a/index.js b/index.js index de3bcb3..35db213 100644 --- a/index.js +++ b/index.js @@ -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 } @@ -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() diff --git a/test.js b/test.js index a8c0a49..8ff6a80 100644 --- a/test.js +++ b/test.js @@ -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)