aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorRosina Bignall2013-01-23 18:18:18 -0600
committerMisko Hevery2013-02-14 14:43:55 -0800
commitace54ff08c4593195b49eadb04d258e6409d969e (patch)
tree60fdeef31aba4aed772821bc029b7a623830b272 /test
parentf5835963d5982003a713dd354eefd376ed39ac02 (diff)
downloadangular.js-ace54ff08c4593195b49eadb04d258e6409d969e.tar.bz2
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.
Diffstat (limited to 'test')
-rw-r--r--test/ng/filter/filterSpec.js53
1 files changed, 53 insertions, 0 deletions
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]]);
+
+ });
+
+
+ });
+
});