Skip to content

Commit

Permalink
fix: Only execute the filter function if really necessary
Browse files Browse the repository at this point in the history
This makes the query using a falsy value much faster `simpleTextSearchInstance('')`
This will speed up some renderers where it previously executed a JSON.stringify on all elements of a collection
  • Loading branch information
marcbachmann committed Jan 21, 2020
1 parent 0de0ff2 commit 0bdb309
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
module.exports = search
module.exports = prepareSimpleTextSearch

// Usage:
// ```
// var get = search(['foo', 'bar', 'foobar'])
// var get = simpleTextSearch(['foo', 'bar', 'foobar'])
// var results = get('foo')
// // -> returns ['foo', 'foobar']
// ```
//
// Objects in a collection get stringified to search it.
// You can also define a property to search in:
// ```
// var get = serach([{name: 'Zürich'}, {name: 'Marc'}], 'name')
// var get = simpleTextSearch([{name: 'Zürich'}, {name: 'Marc'}], 'name')
// var results = get('zurich')
// // -> returns [{name: 'Marc'}]
// ```
function search (collection, property) {
return function (q) {
if (!collection) return collection
return collection.filter(matches(toQuery(q), property))
function prepareSimpleTextSearch (collection, property) {
return function simpleTextSearch (q) {
if (!collection || !q) return collection
const filter = matches(toQuery(q), property)
const result = []
for (const elem of collection) if (filter(elem)) result.push(elem)
return result
}
}

Expand All @@ -30,7 +33,7 @@ function toQuery (str) {
}

function matches (query, prop) {
return function (val) {
return function filter (val) {
if (typeof prop === 'string') val = val && val[prop]
if (typeof val === 'object') val = JSON.stringify(val)
if (typeof val !== 'string') return false
Expand All @@ -52,21 +55,21 @@ function clean (str) {

function charReplacer () {
var charMap = {
'äàáâäæãåā': 'a',
'çćč': 'c',
'đð': 'd',
'èéêëēėę': 'e',
'îïíīįì': 'i',
'ł': 'l',
'ñńň': 'n',
'ôöòóœøōõ': 'o',
'ř': 'r',
'śš': 's',
'ß': 'ss',
'ť': 't',
'ûüùúūů': 'u',
'ÿý': 'y',
'žżŻź': 'z'
äàáâäæãåā: 'a',
çćč: 'c',
đð: 'd',
èéêëēėę: 'e',
îïíīįì: 'i',
ł: 'l',
ñńň: 'n',
ôöòóœøōõ: 'o',
ř: 'r',
śš: 's',
ß: 'ss',
ť: 't',
ûüùúūů: 'u',
ÿý: 'y',
žżŻź: 'z'
}

Object.keys(charMap).forEach(function (keys) {
Expand Down

0 comments on commit 0bdb309

Please sign in to comment.