Fuzzy-filter scores basename matches higher.

Idea stolen from github/github https://github.com/github/github/blob/master/app/assets/javascripts/github/tree_finder.js
This commit is contained in:
Corey Johnson
2012-10-24 15:38:18 -07:00
parent f686988e36
commit 831d23d968

View File

@@ -1,10 +1,16 @@
stringScore = require 'stringscore'
module.exports = (candidates, query, options) ->
module.exports = (candidates, query, options={}) ->
if query
scoredCandidates = candidates.map (candidate) ->
string = if options.key? then candidate[options.key] else candidate
{ candidate, score: stringScore(string, query) }
score = stringScore(string, query)
# Basename matches count for more.
unless /\//.test(query)
score += stringScore(candidate.replace(/^.*\//,''), query)
{ candidate, score }
scoredCandidates.sort (a, b) ->
if a.score > b.score then -1
@@ -13,4 +19,4 @@ module.exports = (candidates, query, options) ->
candidates = (scoredCandidate.candidate for scoredCandidate in scoredCandidates when scoredCandidate.score > 0)
candidates = candidates[0...options.maxResults] if options.maxResults?
candidates
candidates