Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 46343c6

Browse files
colincaseybtford
authored andcommitted
feat(filterFilter): pass index to function predicate
Closes #654
1 parent 3b5d75c commit 46343c6

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/ng/filter/filter.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
* property of the object. That's equivalent to the simple substring match with a `string`
2626
* as described above.
2727
*
28-
* - `function(value)`: A predicate function can be used to write arbitrary filters. The function is
29-
* called for each element of `array`. The final result is an array of those elements that
30-
* the predicate returned true for.
28+
* - `function(value, index)`: A predicate function can be used to write arbitrary filters. The
29+
* function is called for each element of `array`. The final result is an array of those
30+
* elements that the predicate returned true for.
3131
*
3232
* @param {function(actual, expected)|true|undefined} comparator Comparator which is used in
3333
* determining if the expected value (from the filter expression) and actual value (from
@@ -120,9 +120,9 @@ function filterFilter() {
120120
var comparatorType = typeof(comparator),
121121
predicates = [];
122122

123-
predicates.check = function(value) {
123+
predicates.check = function(value, index) {
124124
for (var j = 0; j < predicates.length; j++) {
125-
if(!predicates[j](value)) {
125+
if(!predicates[j](value, index)) {
126126
return false;
127127
}
128128
}
@@ -211,7 +211,7 @@ function filterFilter() {
211211
var filtered = [];
212212
for ( var j = 0; j < array.length; j++) {
213213
var value = array[j];
214-
if (predicates.check(value)) {
214+
if (predicates.check(value, j)) {
215215
filtered.push(value);
216216
}
217217
}

test/ng/filter/filterSpec.js

+10
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ describe('Filter: filter', function() {
4949
expect(filter(items, function(i) {return i.done;}).length).toBe(1);
5050
});
5151

52+
it('should pass the index to a function predicate', function() {
53+
var items = [0, 1, 2, 3];
54+
55+
var result = filter(items, function(value, index) {
56+
return index % 2 === 0;
57+
});
58+
59+
expect(result).toEqual([0, 2]);
60+
});
61+
5262
it('should take object as predicate', function() {
5363
var items = [{first: 'misko', last: 'hevery'},
5464
{first: 'adam', last: 'abrons'}];

0 commit comments

Comments
 (0)