From 94d3ee4c3d69a722a7fc3ad6cc0f4a2da2d42bb9 Mon Sep 17 00:00:00 2001 From: Todd Branchflower Date: Mon, 16 Nov 2015 18:33:36 -0700 Subject: [PATCH] feat(filter-by): add strict mode; fixes #163 --- src/_filter/collection/filter-by.js | 21 ++++++++------ test/spec/filter/collection/filter-by.js | 37 ++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/src/_filter/collection/filter-by.js b/src/_filter/collection/filter-by.js index 8b6ace1..17a69a0 100644 --- a/src/_filter/collection/filter-by.js +++ b/src/_filter/collection/filter-by.js @@ -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)) ? @@ -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(' '); } - 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); }); }); } diff --git a/test/spec/filter/collection/filter-by.js b/test/spec/filter/collection/filter-by.js index c177d83..581abb3 100644 --- a/test/spec/filter/collection/filter-by.js +++ b/test/spec/filter/collection/filter-by.js @@ -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([]); + }); + + 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); + + }); + + }); + });