Files
atom/src/stdlib/fuzzy-filter.coffee
Nathan Sobo 0024cf89de Auto-complete matches can be fuzzy-filtered. Typing non-word characters confirms the match automatically.
Extract fuzzyFilter function into a file that is shared between file finder and autocompleter. Fix jQuery.fn.preempt to pass its arguments to the event handler.
2012-04-19 18:17:08 -06:00

17 lines
606 B
CoffeeScript

stringScore = require 'stringscore'
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) }
scoredCandidates.sort (a, b) ->
if a.score > b.score then -1
else if a.score < b.score then 1
else 0
candidates = (scoredCandidate.candidate for scoredCandidate in scoredCandidates when scoredCandidate.score > 0)
candidates = candidates[0...options.maxResults] if options.maxResults?
candidates