@@ -41,6 +41,49 @@ describe("filter", function () {
41
41
} ) ;
42
42
} ) ;
43
43
44
+ it ( 'filter collection' , function ( done ) {
45
+ var a = { a : 3 , b : 1 , c : 2 } ;
46
+ async . filter ( a , function ( x , callback ) {
47
+ callback ( null , x % 2 ) ;
48
+ } , function ( err , results ) {
49
+ expect ( err ) . to . equal ( null ) ;
50
+ expect ( results ) . to . eql ( [ 3 , 1 ] ) ;
51
+ expect ( a ) . to . eql ( { a : 3 , b : 1 , c : 2 } ) ;
52
+ done ( ) ;
53
+ } ) ;
54
+ } ) ;
55
+
56
+ if ( typeof Symbol === 'function' && Symbol . iterator ) {
57
+ function makeIterator ( array ) {
58
+ var nextIndex ;
59
+ let iterator = {
60
+ next : function ( ) {
61
+ return nextIndex < array . length ?
62
+ { value : array [ nextIndex ++ ] , done : false } :
63
+ { done : true } ;
64
+ }
65
+ } ;
66
+ iterator [ Symbol . iterator ] = function ( ) {
67
+ nextIndex = 0 ; // reset iterator
68
+ return iterator ;
69
+ } ;
70
+ return iterator ;
71
+ }
72
+
73
+ it ( 'filter iterator' , function ( done ) {
74
+ var a = makeIterator ( [ 500 , 20 , 100 ] ) ;
75
+ async . filter ( a , function ( x , callback ) {
76
+ setTimeout ( function ( ) {
77
+ callback ( null , x > 20 ) ;
78
+ } , x ) ;
79
+ } , function ( err , results ) {
80
+ expect ( err ) . to . equal ( null ) ;
81
+ expect ( results ) . to . eql ( [ 500 , 100 ] ) ;
82
+ done ( ) ;
83
+ } ) ;
84
+ } ) ;
85
+ }
86
+
44
87
it ( 'filter error' , function ( done ) {
45
88
async . filter ( [ 3 , 1 , 2 ] , function ( x , callback ) {
46
89
callback ( 'error' ) ;
0 commit comments