Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

skip search/sort when search term is empty #40

Merged
merged 1 commit into from
Mar 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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