fuzzaldrin
Version:
Fuzzy filtering and string scoring
46 lines (40 loc) • 1.29 kB
JavaScript
(function() {
var pluckCandidates, scorer, sortCandidates;
scorer = require('./scorer');
pluckCandidates = function(a) {
return a.candidate;
};
sortCandidates = function(a, b) {
return b.score - a.score;
};
module.exports = function(candidates, query, queryHasSlashes, _arg) {
var candidate, key, maxResults, score, scoredCandidates, string, _i, _len, _ref;
_ref = _arg != null ? _arg : {}, key = _ref.key, maxResults = _ref.maxResults;
if (query) {
scoredCandidates = [];
for (_i = 0, _len = candidates.length; _i < _len; _i++) {
candidate = candidates[_i];
string = key != null ? candidate[key] : candidate;
if (!string) {
continue;
}
score = scorer.score(string, query, queryHasSlashes);
if (!queryHasSlashes) {
score = scorer.basenameScore(string, query, score);
}
if (score > 0) {
scoredCandidates.push({
candidate: candidate,
score: score
});
}
}
scoredCandidates.sort(sortCandidates);
candidates = scoredCandidates.map(pluckCandidates);
}
if (maxResults != null) {
candidates = candidates.slice(0, maxResults);
}
return candidates;
};
}).call(this);