Skip to content

Instantly share code, notes, and snippets.

@marclundgren
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marclundgren/5e9ba077c437813f8e38 to your computer and use it in GitHub Desktop.
Save marclundgren/5e9ba077c437813f8e38 to your computer and use it in GitHub Desktop.
/* jshint strict:false, asi:true */
var fuzzy = function(items, key) {
return function(query) {
var words = query.toLowerCase().split(' ');
return items.filter(function(item) {
var normalizedTerm = item[key].toLowerCase();
return words.every(function(word) {
return (normalizedTerm.indexOf(word) > -1);
});
});
};
};
var articles = [{
title: '2014 Javascript MVC Frameworks Comparison',
author: 'Guybrush Treepwood'
}, {
title: 'Javascript in the year 2014',
author: 'Herman Toothrot'
},
{
title: 'Javascript in the year 2013',
author: 'Rapp Scallion'
}];
var searchByTitle = fuzzy(articles, 'title');
searchByTitle('javascript 2014')
// returns the 1st and 2nd items
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment