Skip to content

Instantly share code, notes, and snippets.

@wangshijun
Created May 14, 2014 01:22
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 wangshijun/f4a2656e487302d2cad3 to your computer and use it in GitHub Desktop.
Save wangshijun/f4a2656e487302d2cad3 to your computer and use it in GitHub Desktop.
'use strict';
/*
IMPORTANT: requires Underscore.js (http://underscorejs.org)
Usage: ng-repeat="item in items | fuzzyFilter: searchText"
*/
var app = angular.module('app', []);
app.filter('fuzzyFilter', function () {
return function (items, searchText) {
if (!searchText) {
return items;
}
var searchWords = searchText.split(' ');
return _.filter(items, function (item) {
var itemValues = _.values(item);
var itemText = itemValues.join(' ');
var lowerCasedItemText = itemText.toLowerCase();
return _.every(searchWords, function (searchWord) {
var lowerCasedSearchWord = searchWord.toLowerCase();
return lowerCasedItemText.search(lowerCasedSearchWord) !== -1;
});
});
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment