Last active
August 29, 2015 14:11
-
-
Save marclundgren/5e9ba077c437813f8e38 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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