From 066622746cd4247335d74664f15645e8166ccf89 Mon Sep 17 00:00:00 2001 From: hozefaj Date: Wed, 14 Mar 2018 21:30:33 -0700 Subject: [PATCH] skip search/sort when search term is empty --- src/__tests__/index.js | 16 ++++++++++++++++ src/index.js | 7 +++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/__tests__/index.js b/src/__tests__/index.js index 05f0654..7172dbe 100644 --- a/src/__tests__/index.js +++ b/src/__tests__/index.js @@ -344,6 +344,22 @@ const tests = { 'superduperfile', ], }, + 'skip matching when no search value is absent': { + input: [ + [ + {tea: 'Milk', alias: 'moo'}, + {tea: 'Oolong', alias: 'B'}, + {tea: 'Green', alias: 'C'}, + ], + '', + {keys: ['tea']}, + ], + output: [ + {tea: 'Milk', alias: 'moo'}, + {tea: 'Oolong', alias: 'B'}, + {tea: 'Green', alias: 'C'}, + ], + }, } Object.keys(tests).forEach(title => { diff --git a/src/index.js b/src/index.js index ea7911e..0fda04c 100644 --- a/src/index.js +++ b/src/index.js @@ -38,6 +38,9 @@ matchSorter.caseRankings = caseRankings * @return {Array} - the new sorted array */ function matchSorter(items, value, options = {}) { + // not performing any search/sort if value(search term) is empty + if (!value) return items + const {keys, threshold = rankings.MATCHES} = options const matchedItems = items.reduce(reduceItemsToRanked, []) return matchedItems.sort(sortRankedItems).map(({item}) => item) @@ -91,7 +94,7 @@ function getHighestRanking(item, keys, value, options) { * @returns {Number} the ranking for how well stringToRank matches testString */ function getMatchRanking(testString, stringToRank, options) { - /* eslint complexity:[2, 11] */ + /* eslint complexity:[2, 12] */ testString = prepareValueForComparison(testString, options) stringToRank = prepareValueForComparison(stringToRank, options) @@ -109,7 +112,7 @@ function getMatchRanking(testString, stringToRank, options) { const isPartial = isPartialOfCase(testString, stringToRank, caseRank) const isCasedAcronym = isCaseAcronym(testString, stringToRank, caseRank) - // Lowercasing before further comparison + // Lower casing before further comparison testString = testString.toLowerCase() stringToRank = stringToRank.toLowerCase()