From ace54ff08c4593195b49eadb04d258e6409d969e Mon Sep 17 00:00:00 2001 From: Rosina Bignall Date: Wed, 23 Jan 2013 18:18:18 -0600 Subject: feat(filter): Add comparison function to filter Add optional comparator function argument to $filter('filter')(array, expression, comparator) such that the comparator function is used to compare the values and predicates. When true, defaults to equality. When missing defaults to substring matching. --- test/ng/filter/filterSpec.js | 53 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'test') diff --git a/test/ng/filter/filterSpec.js b/test/ng/filter/filterSpec.js index a33358d0..4f357371 100644 --- a/test/ng/filter/filterSpec.js +++ b/test/ng/filter/filterSpec.js @@ -66,4 +66,57 @@ describe('Filter: filter', function() { expect(filter(items, '!isk').length).toBe(1); expect(filter(items, '!isk')[0]).toEqual(items[1]); }); + + describe('should support comparator', function() { + + it('as equality when true', function() { + var items = ['misko', 'adam', 'adamson']; + var expr = 'adam'; + expect(filter(items, expr, true)).toEqual([items[1]]); + expect(filter(items, expr, false)).toEqual([items[1], items[2]]); + + var items = [ + {key: 'value1', nonkey: 1}, + {key: 'value2', nonkey: 2}, + {key: 'value12', nonkey: 3}, + {key: 'value1', nonkey:4}, + {key: 'Value1', nonkey:5} + ]; + var expr = {key: 'value1'}; + expect(filter(items, expr, true)).toEqual([items[0], items[3]]); + + var items = [ + {key: 1, nonkey: 1}, + {key: 2, nonkey: 2}, + {key: 12, nonkey: 3}, + {key: 1, nonkey:4} + ]; + var expr = { key: 1 }; + expect(filter(items, expr, true)).toEqual([items[0], items[3]]); + + var expr = 12; + expect(filter(items, expr, true)).toEqual([items[2]]); + }); + + it('and use the function given to compare values', function() { + var items = [ + {key: 1, nonkey: 1}, + {key: 2, nonkey: 2}, + {key: 12, nonkey: 3}, + {key: 1, nonkey:14} + ]; + var expr = {key: 10}; + var comparator = function (obj,value) { + return obj > value; + } + expect(filter(items, expr, comparator)).toEqual([items[2]]); + + expr = 10; + expect(filter(items, expr, comparator)).toEqual([items[2], items[3]]); + + }); + + + }); + }); -- cgit v1.2.3