Created
May 14, 2014 01:22
-
-
Save wangshijun/f4a2656e487302d2cad3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
'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