Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions src/_filter/collection/filter-by.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
angular.module('a8m.filter-by', [])
.filter('filterBy', ['$parse', function( $parse ) {
return function(collection, properties, search) {
return function(collection, properties, search, strict) {
var comparator;

search = (isString(search) || isNumber(search)) ?
Expand All @@ -32,16 +32,19 @@ angular.module('a8m.filter-by', [])
if(!~prop.indexOf('+')) {
comparator = $parse(prop)(elm)
} else {
var propList = prop.replace(new RegExp('\\s', 'g'), '').split('+');
comparator = propList.reduce(function(prev, cur, index) {
return (index === 1) ? $parse(prev)(elm) + ' ' + $parse(cur)(elm) :
prev + ' ' + $parse(cur)(elm);
});
var propList = prop.replace(/\s+/g, '').split('+');
comparator = propList
.map(function(prop) { return $parse(prop)(elm); })
.join(' ');
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small refactor here.

}

return (isString(comparator) || isNumber(comparator))
? String(comparator).toLowerCase().contains(search)
: false;
if (!isString(comparator) && !isNumber(comparator)) {
return false;
}

comparator = String(comparator).toLowerCase();

return strict ? comparator === search : comparator.contains(search);
});
});
}
Expand Down
37 changes: 37 additions & 0 deletions test/spec/filter/collection/filter-by.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,41 @@ describe('filterByFilter', function() {

});

it('should not coerce non-string/number properties', function() {
var users = [
{ id: [1, 2], user: { first_name: 'foo', last_name: 'bar', mobile: 4444 } }
];

expect(filter(users, ['id'], 1)).toEqual([]);
});
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test for the isString / isNumber check


describe('strict mode', function() {

var users = [
{ id: 1, user: { first_name: 'foo', last_name: 'bar', mobile: 4444 } }
];

it('should only return exact matches', function() {

expect(filter(users, ['user.first_name'], 'fo', true)).toEqual([]);
expect(filter(users, ['user.first_name'], 'foo', true)).toEqual(users);

});

it('should handle multiple properties', function() {

expect(filter(users, ['user.first_name', 'user.last_name'], 'ba', true)).toEqual([]);
expect(filter(users, ['user.first_name', 'user.last_name'], 'bar', true)).toEqual(users);

});

it('should handle concatenation', function() {

expect(filter(users, ['user.first_name + user.last_name'], 'foo ba', true)).toEqual([]);
expect(filter(users, ['user.first_name + user.last_name'], 'foo bar', true)).toEqual(users);

});

});

});