Skip to content

Commit

Permalink
fix(sorting): change keyIndex only when ranking changes (#28)
Browse files Browse the repository at this point in the history
Previously, `keyIndex` was getting updated before min and max ranking were checked, so it could change even if the ranking itself didn't.
  • Loading branch information
nfdjps authored and Kent C. Dodds committed Aug 2, 2017
1 parent fd2dd67 commit d621043
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ function getHighestRanking(item, keys, value, options) {
}
const valuesToRank = getAllValuesToRank(item, keys)
return valuesToRank.reduce(({rank, keyIndex}, {itemValue, attributes}, i) => {
const newRank = getMatchRanking(itemValue, value, options)
let newRank = getMatchRanking(itemValue, value, options)
const {minRanking, maxRanking} = attributes
if (newRank < minRanking && newRank >= rankings.MATCHES) {
newRank = minRanking
} else if (newRank > maxRanking) {
newRank = maxRanking
}
if (newRank > rank) {
rank = newRank
keyIndex = i
}
const {minRanking, maxRanking} = attributes
if (rank < minRanking && newRank >= rankings.MATCHES) {
rank = minRanking
} else if (rank > maxRanking) {
rank = maxRanking
}
return {rank, keyIndex}
}, {rank: rankings.NO_MATCH, keyIndex: -1})
}
Expand Down Expand Up @@ -188,11 +188,11 @@ function getClosenessRanking(testString, stringToRank) {
return rankings.NO_MATCH
}
}

const spread = charNumber - firstIndex
return getRanking(spread)
}

/**
* Sorts items that have a rank, index, and keyIndex
* @param {Object} a - the first item to sort
Expand Down

0 comments on commit d621043

Please sign in to comment.