diff --git a/index.js b/index.js index 540018b..9d2772e 100644 --- a/index.js +++ b/index.js @@ -34,7 +34,7 @@ function prepareSimpleTextSearch (collection, property) { return function simpleTextSearch (q) { if (!collection || !q) return collection - const tokens = toQueryTokens(q) + const tokens = clean(q).split(/\W/) const result = [] // eslint-disable-next-line no-labels @@ -48,15 +48,6 @@ function prepareSimpleTextSearch (collection, property) { } } -function toQueryTokens (str) { - const content = [] - for (const token of clean(str).split(/\b/)) { - if (!/\b/.test(token)) continue - content.push(token.trim()) - } - return content -} - const specialCharMap = { äàáâäæãåā: 'a', çćč: 'c', @@ -75,16 +66,16 @@ const specialCharMap = { žżŻź: 'z' } -const charMap = { '\\W+': ' ' } +const charMap = {} for (const keys of Object.keys(specialCharMap)) { for (const char of keys) { charMap[char] = specialCharMap[keys] } } -const toReplace = new RegExp('(' + Object.keys(charMap).join('|') + ')', 'g') -function replacer (char) { return charMap[char] || char } +const toReplace = new RegExp('[' + Object.keys(charMap).join('') + ']|\\W+', 'g') +function replacer (char) { return charMap[char] || ' ' } function clean (str) { - return String(str).toLowerCase().replace(toReplace, replacer) + return String(str).toLowerCase().replace(toReplace, replacer).trim() } diff --git a/test.js b/test.js index 93037b9..01efbd2 100644 --- a/test.js +++ b/test.js @@ -61,3 +61,19 @@ assert.strictEqual(res5a[1], arr5[1]) const res5b = get5('Sidra') assert.strictEqual(res5b.length, arr5.length) + +// Normalize multiple umlauts +const get6 = search(['Es Füür']) +const res6 = get6('Fuur') +assert.strictEqual(res6.length, 1) + +// Supports numbers in filters +const get7 = search(['Hello 1', 'Hello 2', 'Test 1']) +const res7a = get7('1') +assert.strictEqual(res7a.length, 2) +assert.strictEqual(res7a[0], 'Hello 1') +assert.strictEqual(res7a[1], 'Test 1') + +const res7b = get7('2') +assert.strictEqual(res7b.length, 1) +assert.strictEqual(res7b[0], 'Hello 2')