Skip to content

Commit

Permalink
fix: skip search/sort when search term is empty (#40)
Browse files Browse the repository at this point in the history
Closes #39
  • Loading branch information
hozefaj authored and Kent C. Dodds committed Mar 15, 2018
1 parent 2800b57 commit dcec521
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
7 changes: 5 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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()

Expand Down

0 comments on commit dcec521

Please sign in to comment.