Skip to content
This repository has been archived by the owner on Jun 15, 2021. It is now read-only.

Commit

Permalink
fix regex's up and rename sortby method name
Browse files Browse the repository at this point in the history
  • Loading branch information
fat committed May 17, 2011
1 parent dd96ef9 commit d462a53
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions lib/ender.search.js
Expand Up @@ -47,26 +47,31 @@ module.exports = function (terms, options) {
});
}

function rankRelevance(terms, array) {
var ranked = [], priority = ['name', 'keywords', 'description'];
function rankRelevance(args, data) {
var regex
, sorted = []
, priority = [ "name", "keywords", "description" ]
, args = args.map(function (arg) {
return escapeRegExp(arg)
});

//Exact query match against name
var regex = new RegExp('^' + terms.join('\s') + '$');
matchAgainstReg(regex, array, ranked, ['name']);
// args as exact phrase for name
regexp = new RegExp("^" + args.join("\\s") + "$");
sortByRegExp(regexp, data, sorted, ["name"]);

//whole query match against priorities as words
var regex = new RegExp('\\b' + escapeRegExp(terms.join('\s')) + '\\b', 'i');
matchAgainstReg(regex, array, ranked, priority);
// args as phrase anywhere
regexp = new RegExp("\\b" + args.join("\\s") + "\\b", "i");
sortByRegExp(regexp, data, sorted, priority);

//any one query against priorities as word
var regex = new RegExp('\\b' + escapeRegExp(terms.join('\b|\b')) + '\\b', 'i');
matchAgainstReg(regex, array, ranked, priority);
// args as keywords anywhere (ex: useful for case when express matches expresso)
regexp = new RegExp("\\b" + args.join("\\b\|\\b") + "\\b", "i");
sortByRegExp(regexp, data, sorted, priority);

//we don't really care about relevance at this point :P
return ranked.concat(array);
// we don't really care about relevance at this point :P
return sorted.concat(data);
}

function matchAgainstReg(regex, array, ranked, priority) {
function sortByRegExp(regex, array, ranked, priority) {
for (var i = 0; i < priority.length; i++) {
var p = priority[i];
for (var j = 0; j < array.length; j++) {
Expand All @@ -89,8 +94,13 @@ function matchAgainstReg(regex, array, ranked, priority) {
function processItem(item, terms) {
var reg = new RegExp('(' + terms.map(function (item) { return escapeRegExp(item) }).join('|') + ')', 'ig');

var title = item.name, dots = item.description.length > 80 ? '...' : '';
if (item.description) title += ' - ' + item.description.substring(0, 80) + dots;
var title = item.name;

if (item.description) {
var dots = item.description.length > 80 ? '...' : '';
title += ' - ' + item.description.substring(0, 80) + dots;
}

console.log('+ ' + title.replace(reg, '$1'.cyan));

var maintainers = '';
Expand All @@ -115,7 +125,7 @@ function processItem(item, terms) {

function escapeRegExp(string){
// Credit: XRegExp 0.6.1 (c) 2007-2008 Steven Levithan <http://stevenlevithan.com/regex/xregexp/> MIT License
return string.replace(/[-[\]{}()*+?.\\^$|,#\s]/g, function(match){
return '\\' + match;
return string.replace(/[-[\]{}()*+?.\\^$|,#\s]/g, function(match){
return '\\' + match;
});
}

0 comments on commit d462a53

Please sign in to comment.