Skip to content

Commit 7b97a49

Browse files
committed
Merge pull request #165 from toddbranch/filter-by-strict-mode
feat(filter-by): add strict mode; fixes #163
2 parents f51ce3c + 94d3ee4 commit 7b97a49

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

src/_filter/collection/filter-by.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99
angular.module('a8m.filter-by', [])
1010
.filter('filterBy', ['$parse', function( $parse ) {
11-
return function(collection, properties, search) {
11+
return function(collection, properties, search, strict) {
1212
var comparator;
1313

1414
search = (isString(search) || isNumber(search)) ?
@@ -32,16 +32,19 @@ angular.module('a8m.filter-by', [])
3232
if(!~prop.indexOf('+')) {
3333
comparator = $parse(prop)(elm)
3434
} else {
35-
var propList = prop.replace(new RegExp('\\s', 'g'), '').split('+');
36-
comparator = propList.reduce(function(prev, cur, index) {
37-
return (index === 1) ? $parse(prev)(elm) + ' ' + $parse(cur)(elm) :
38-
prev + ' ' + $parse(cur)(elm);
39-
});
35+
var propList = prop.replace(/\s+/g, '').split('+');
36+
comparator = propList
37+
.map(function(prop) { return $parse(prop)(elm); })
38+
.join(' ');
4039
}
4140

42-
return (isString(comparator) || isNumber(comparator))
43-
? String(comparator).toLowerCase().contains(search)
44-
: false;
41+
if (!isString(comparator) && !isNumber(comparator)) {
42+
return false;
43+
}
44+
45+
comparator = String(comparator).toLowerCase();
46+
47+
return strict ? comparator === search : comparator.contains(search);
4548
});
4649
});
4750
}

test/spec/filter/collection/filter-by.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,41 @@ describe('filterByFilter', function() {
9292

9393
});
9494

95+
it('should not coerce non-string/number properties', function() {
96+
var users = [
97+
{ id: [1, 2], user: { first_name: 'foo', last_name: 'bar', mobile: 4444 } }
98+
];
99+
100+
expect(filter(users, ['id'], 1)).toEqual([]);
101+
});
102+
103+
describe('strict mode', function() {
104+
105+
var users = [
106+
{ id: 1, user: { first_name: 'foo', last_name: 'bar', mobile: 4444 } }
107+
];
108+
109+
it('should only return exact matches', function() {
110+
111+
expect(filter(users, ['user.first_name'], 'fo', true)).toEqual([]);
112+
expect(filter(users, ['user.first_name'], 'foo', true)).toEqual(users);
113+
114+
});
115+
116+
it('should handle multiple properties', function() {
117+
118+
expect(filter(users, ['user.first_name', 'user.last_name'], 'ba', true)).toEqual([]);
119+
expect(filter(users, ['user.first_name', 'user.last_name'], 'bar', true)).toEqual(users);
120+
121+
});
122+
123+
it('should handle concatenation', function() {
124+
125+
expect(filter(users, ['user.first_name + user.last_name'], 'foo ba', true)).toEqual([]);
126+
expect(filter(users, ['user.first_name + user.last_name'], 'foo bar', true)).toEqual(users);
127+
128+
});
129+
130+
});
131+
95132
});

0 commit comments

Comments
 (0)