This repository was archived by the owner on Apr 12, 2024. It is now read-only.
File tree 2 files changed +16
-6
lines changed
2 files changed +16
-6
lines changed Original file line number Diff line number Diff line change 25
25
* property of the object. That's equivalent to the simple substring match with a `string`
26
26
* as described above.
27
27
*
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.
31
31
*
32
32
* @param {function(actual, expected)|true|undefined } comparator Comparator which is used in
33
33
* determining if the expected value (from the filter expression) and actual value (from
@@ -120,9 +120,9 @@ function filterFilter() {
120
120
var comparatorType = typeof ( comparator ) ,
121
121
predicates = [ ] ;
122
122
123
- predicates . check = function ( value ) {
123
+ predicates . check = function ( value , index ) {
124
124
for ( var j = 0 ; j < predicates . length ; j ++ ) {
125
- if ( ! predicates [ j ] ( value ) ) {
125
+ if ( ! predicates [ j ] ( value , index ) ) {
126
126
return false ;
127
127
}
128
128
}
@@ -211,7 +211,7 @@ function filterFilter() {
211
211
var filtered = [ ] ;
212
212
for ( var j = 0 ; j < array . length ; j ++ ) {
213
213
var value = array [ j ] ;
214
- if ( predicates . check ( value ) ) {
214
+ if ( predicates . check ( value , j ) ) {
215
215
filtered . push ( value ) ;
216
216
}
217
217
}
Original file line number Diff line number Diff line change @@ -49,6 +49,16 @@ describe('Filter: filter', function() {
49
49
expect ( filter ( items , function ( i ) { return i . done ; } ) . length ) . toBe ( 1 ) ;
50
50
} ) ;
51
51
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
+
52
62
it ( 'should take object as predicate' , function ( ) {
53
63
var items = [ { first : 'misko' , last : 'hevery' } ,
54
64
{ first : 'adam' , last : 'abrons' } ] ;
You can’t perform that action at this time.
0 commit comments