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