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