Skip to content

Commit

Permalink
fix: Normalize multiple umlauts provided in the query
Browse files Browse the repository at this point in the history
  • Loading branch information
marcbachmann committed Jun 4, 2020
1 parent 2ffaffc commit 412baa3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
19 changes: 5 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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',
Expand All @@ -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()
}
16 changes: 16 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

0 comments on commit 412baa3

Please sign in to comment.