Untitled diff
30 lines
immutable = require 'immutable'
scorer = require './scorer'
scorer = require './scorer'
defaultPathSeparator = scorer.pathSeparator
defaultPathSeparator = scorer.pathSeparator
pluckCandidates = (a) -> a.candidate
pluckCandidates = (a) -> a.candidate
sortCandidates = (a, b) -> b.score - a.score
sortCandidates = (a, b) ->
b.score - a.score
module.exports = (candidates, query, options={}) ->
module.exports = (candidates, query, options={}) ->
scoredCandidates = []
scoredCandidates = immutable.List()
#See also option parsing on main module for default
#See also option parsing on main module for default
{key, maxResults, maxInners, allowErrors, isPath, useExtensionBonus, optCharRegEx, pathSeparator } = options
{key, maxResults, maxInners, allowErrors, isPath, useExtensionBonus, optCharRegEx, pathSeparator } = options
spotLeft = if maxInners? and maxInners > 0 then maxInners else candidates.length
spotLeft = if maxInners? and maxInners > 0 then maxInners else candidates.size
bKey = key?
bKey = key?
prepQuery = scorer.prepQuery(query, options)
prepQuery = scorer.prepQuery(query, options)
for candidate in candidates
candidates.forEach (candidate) ->
string = if bKey then candidate[key] else candidate
string = if bKey then candidate.get(key) else candidate
continue unless string
return unless string
score = scorer.score(string, query, prepQuery, allowErrors, isPath, useExtensionBonus, pathSeparator)
score = scorer.score(string, query, prepQuery, allowErrors, isPath, useExtensionBonus, pathSeparator)
if score > 0
if score > 0
scoredCandidates.push({candidate, score})
scoredCandidates = scoredCandidates.push({candidate, score})
break unless --spotLeft
return unless --spotLeft
# Sort scores in descending order
# Sort scores in descending order
scoredCandidates.sort(sortCandidates)
scoredCandidates.sort(sortCandidates)
candidates = scoredCandidates.map(pluckCandidates)
candidates = scoredCandidates.map(pluckCandidates)
candidates = candidates[0...maxResults] if maxResults?
candidates = candidates.take(maxResults) if maxResults?
candidates
candidates