-
Notifications
You must be signed in to change notification settings - Fork 2
/
ng-property-filters.js
124 lines (98 loc) · 3.22 KB
/
ng-property-filters.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
(function() {
var getAllStringProperties = function(obj) {
var strings = [];
for (var prop in obj) {
if (obj.hasOwnProperty(prop) && typeof obj[prop] === 'string' && prop !== '$$hashKey') { // only get strings and ignore the $$hashKey property added by angularjs
strings.push(obj[prop]);
}
}
return strings;
}
var getProperties = function(obj, properties) {
var strings = [];
for (var i=0, len=properties.length; i<len; i++) {
strings.push(obj[properties[i]]);
}
return strings;
}
function MatchFilter() {
return function(haystack, options) {
var needle = options.needle;
var allProperties = options.allProperties || false;
var properties = options.properties || false;
var results = [];
for (var i=0, len=haystack.length; i<len; i++) {
var testObj = haystack[i];
var searchStrings = [];
if (properties) {
searchStrings = getProperties(testObj, properties);
}
else if (allProperties) {
searchStrings = getAllStringProperties(testObj);
}
else {
searchStrings = [testObj];
}
for (var j=0, slen=searchStrings.length; j<slen; j++) {
if(searchStrings[j].toLowerCase().indexOf(needle.toLowerCase()) !== -1) {
results.push(testObj);
break;
}
}
}
return results;
}
}
function FuzzyFilter() {
// use underscore's memoize, if available - otherwise, uses the same implementation of _.memoize and _.has as in underscore 1.7.0 (http://underscorejs.org/)
// to help make fuzzy regex generator faster on large data sets
var memoize = (typeof _ !== 'undefined') ? _.memoize : function(func, hasher) {
var memoize = function(key) {
var cache = memoize.cache;
var address = hasher ? hasher.apply(this, arguments) : key;
//if (!has(cache, address)) cache[address] = func.apply(this, arguments);
if (cache === null || !hasOwnProperty.call(cache, address)) cache[address] = func.apply(this, arguments);
return cache[address];
}
memoize.cache = {};
return memoize;
};
// cache the regex pattern creator function
var searchCache = memoize(function(pattern){
return new RegExp(pattern.split('').reduce(function(a,b){
return a+'[^'+b+']*'+b;
}), 'i');
});
// the actual filter function - haystack comes from angular, options specified as an object literal in directive
return function(haystack, options) {
var needle = options.needle;
if (typeof needle === 'undefined' || needle == '') return haystack;
var allProperties = options.allProperties || false;
var properties = options.properties || false;
var results = [];
for (var i=0, len=haystack.length; i<len; i++) {
var testObj = haystack[i];
var searchStrings = [];
if (properties) {
searchStrings = getProperties(testObj, properties);
}
else if (allProperties) {
searchStrings = getAllStringProperties(testObj);
}
else {
searchStrings = [testObj];
}
for (var j=0, slen=searchStrings.length; j<slen; j++) {
if(searchCache(needle).test(searchStrings[j])) {
results.push(testObj);
break;
}
}
}
return results;
}
}
angular.module('propFilters', [])
.filter('matchProp', MatchFilter)
.filter('fuzzyProp', FuzzyFilter);
})();