repl.txt (6056B)
1 2 {{alias}}( collection, n, [options,] predicate, done ) 3 Tests whether a collection contains at least `n` elements which pass a test 4 implemented by a predicate function, iterating from right to left. 5 6 When invoked, the predicate function is provided a maximum of four 7 arguments: 8 9 - `value`: collection value 10 - `index`: collection index 11 - `collection`: the input collection 12 - `next`: a callback to be invoked after processing a collection `value` 13 14 The actual number of provided arguments depends on function length. If the 15 predicate function accepts two arguments, the predicate function is 16 provided: 17 18 - `value` 19 - `next` 20 21 If the predicate function accepts three arguments, the predicate function is 22 provided: 23 24 - `value` 25 - `index` 26 - `next` 27 28 For every other predicate function signature, the predicate function is 29 provided all four arguments. 30 31 The `next` callback takes two arguments: 32 33 - `error`: error argument 34 - `result`: test result 35 36 If a provided function calls the `next` callback with a truthy `error` 37 argument, the function suspends execution and immediately calls the `done` 38 callback for subsequent `error` handling. 39 40 The function immediately returns upon receiving `n` non-falsy `result` 41 values and calls the `done` callback with `null` as the first argument and 42 `true` as the second argument. 43 44 If all elements fail, the function calls the `done` callback with `null` 45 as the first argument and `false` as the second argument. 46 47 Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, 48 wrap the `done` callback in a function which either executes at the end of 49 the current stack (e.g., `nextTick`) or during a subsequent turn of the 50 event loop (e.g., `setImmediate`, `setTimeout`). 51 52 The function does not support dynamic collection resizing. 53 54 The function does not skip `undefined` elements. 55 56 Parameters 57 ---------- 58 collection: Array|TypedArray|Object 59 Input collection over which to iterate. If provided an object, the 60 object must be array-like (excluding strings and functions). 61 62 n: number 63 Minimum number of successful elements. 64 65 options: Object (optional) 66 Function options. 67 68 options.limit: integer (optional) 69 Maximum number of pending invocations. Default: Infinity. 70 71 options.series: boolean (optional) 72 Boolean indicating whether to process each collection element 73 sequentially. Default: false. 74 75 options.thisArg: any (optional) 76 Execution context. 77 78 predicate: Function 79 The test function to invoke for each element in a collection. 80 81 done: Function 82 A callback invoked either upon processing all collection elements or 83 upon encountering an error. 84 85 Examples 86 -------- 87 // Basic usage: 88 > function predicate( value, next ) { 89 ... setTimeout( onTimeout, value ); 90 ... function onTimeout() { 91 ... console.log( value ); 92 ... next( null, false ); 93 ... } 94 ... }; 95 > function done( error, bool ) { 96 ... if ( error ) { 97 ... throw error; 98 ... } 99 ... console.log( bool ); 100 ... }; 101 > var arr = [ 1000, 2500, 3000 ]; 102 > {{alias}}( arr, 2, predicate, done ) 103 1000 104 2500 105 3000 106 false 107 108 // Limit number of concurrent invocations: 109 > function predicate( value, next ) { 110 ... setTimeout( onTimeout, value ); 111 ... function onTimeout() { 112 ... console.log( value ); 113 ... next( null, false ); 114 ... } 115 ... }; 116 > function done( error, bool ) { 117 ... if ( error ) { 118 ... throw error; 119 ... } 120 ... console.log( bool ); 121 ... }; 122 > var opts = { 'limit': 2 }; 123 > var arr = [ 1000, 2500, 3000 ]; 124 > {{alias}}( arr, 2, opts, predicate, done ) 125 2500 126 3000 127 1000 128 false 129 130 // Process sequentially: 131 > function predicate( value, next ) { 132 ... setTimeout( onTimeout, value ); 133 ... function onTimeout() { 134 ... console.log( value ); 135 ... next( null, false ); 136 ... } 137 ... }; 138 > function done( error, bool ) { 139 ... if ( error ) { 140 ... throw error; 141 ... } 142 ... console.log( bool ); 143 ... }; 144 > var opts = { 'series': true }; 145 > var arr = [ 1000, 2500, 3000 ]; 146 > {{alias}}( arr, 2, opts, predicate, done ) 147 3000 148 2500 149 1000 150 false 151 152 153 {{alias}}.factory( [options,] predicate ) 154 Returns a function which tests whether a collection contains at least `n` 155 elements which pass a test implemented by a predicate function, iterating 156 from right to left. 157 158 Parameters 159 ---------- 160 options: Object (optional) 161 Function options. 162 163 options.limit: integer (optional) 164 Maximum number of pending invocations. Default: Infinity. 165 166 options.series: boolean (optional) 167 Boolean indicating whether to process each collection element 168 sequentially. Default: false. 169 170 options.thisArg: any (optional) 171 Execution context. 172 173 predicate: Function 174 The test function to invoke for each element in a collection. 175 176 Returns 177 ------- 178 out: Function 179 A function which tests each element in a collection. 180 181 Examples 182 -------- 183 > function predicate( value, next ) { 184 ... setTimeout( onTimeout, value ); 185 ... function onTimeout() { 186 ... console.log( value ); 187 ... next( null, false ); 188 ... } 189 ... }; 190 > var opts = { 'series': true }; 191 > var f = {{alias}}.factory( opts, predicate ); 192 > function done( error, bool ) { 193 ... if ( error ) { 194 ... throw error; 195 ... } 196 ... console.log( bool ); 197 ... }; 198 > var arr = [ 1000, 2500, 3000 ]; 199 > f( arr, 2, done ) 200 3000 201 2500 202 1000 203 false 204 > arr = [ 1000, 1500, 2000 ]; 205 > f( arr, 2, done ) 206 2000 207 1500 208 1000 209 false 210 211 See Also 212 -------- 213