repl.txt (6080B)
1 2 {{alias}}( collection, [options,] indicator, done ) 3 Groups values according to an indicator function and returns group counts. 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.thisArg: any (optional) 71 Execution context. 72 73 indicator: Function 74 Indicator function specifying which group an element in the input 75 collection belongs to. 76 77 done: Function 78 A callback invoked either upon processing all collection elements or 79 upon encountering an error. 80 81 Examples 82 -------- 83 // Basic usage: 84 > function indicator( value, index, next ) { 85 ... setTimeout( onTimeout, value ); 86 ... function onTimeout() { 87 ... console.log( value ); 88 ... next( null, ( index%2 === 0 ) ? 'even': 'odd' ); 89 ... } 90 ... }; 91 > function done( error, result ) { 92 ... if ( error ) { 93 ... throw error; 94 ... } 95 ... console.log( result ); 96 ... }; 97 > var arr = [ 3000, 2500, 1000 ]; 98 > {{alias}}( arr, indicator, done ) 99 1000 100 2500 101 3000 102 { "even": 2, "odd": 1 } 103 104 // Limit number of concurrent invocations: 105 > function indicator( value, index, next ) { 106 ... setTimeout( onTimeout, value ); 107 ... function onTimeout() { 108 ... console.log( value ); 109 ... next( null, ( index%2 === 0 ) ? 'even' : 'odd' ); 110 ... } 111 ... }; 112 > function done( error, result ) { 113 ... if ( error ) { 114 ... throw error; 115 ... } 116 ... console.log( result ); 117 ... }; 118 > var opts = { 'limit': 2 }; 119 > var arr = [ 3000, 2500, 1000 ]; 120 > {{alias}}( arr, opts, indicator, done ) 121 2500 122 3000 123 1000 124 { "even": 2, "odd": 1 } 125 126 // Process sequentially: 127 > function indicator( value, index, next ) { 128 ... setTimeout( onTimeout, value ); 129 ... function onTimeout() { 130 ... console.log( value ); 131 ... next( null, ( index%2 === 0 ) ? 'even' : 'odd' ); 132 ... } 133 ... }; 134 > function done( error, result ) { 135 ... if ( error ) { 136 ... throw error; 137 ... } 138 ... console.log( result ); 139 ... }; 140 > var opts = { 'series': true }; 141 > var arr = [ 3000, 2500, 1000 ]; 142 > {{alias}}( arr, opts, indicator, done ) 143 3000 144 2500 145 1000 146 { "even": 2, "odd": 1 } 147 148 149 {{alias}}.factory( [options,] indicator ) 150 Returns a function which groups values according to an indicator function 151 and returns group counts. 152 153 Parameters 154 ---------- 155 options: Object (optional) 156 Function options. 157 158 options.limit: integer (optional) 159 Maximum number of pending invocations. Default: Infinity. 160 161 options.series: boolean (optional) 162 Boolean indicating whether to process each collection element 163 sequentially. Default: false. 164 165 options.thisArg: any (optional) 166 Execution context. 167 168 indicator: Function 169 Indicator function specifying which group an element in the input 170 collection belongs to. 171 172 Returns 173 ------- 174 out: Function 175 A function which groups values and returns group counts. 176 177 Examples 178 -------- 179 > function indicator( value, index, next ) { 180 ... setTimeout( onTimeout, value ); 181 ... function onTimeout() { 182 ... console.log( value ); 183 ... next( null, ( index%2 === 0 ) ? 'even' : 'odd' ); 184 ... } 185 ... }; 186 > var opts = { 'series': true }; 187 > var f = {{alias}}.factory( opts, indicator ); 188 > function done( error, result ) { 189 ... if ( error ) { 190 ... throw error; 191 ... } 192 ... console.log( result ); 193 ... }; 194 > var arr = [ 3000, 2500, 1000 ]; 195 > f( arr, done ) 196 3000 197 2500 198 1000 199 { "even": 2, "odd": 1 } 200 > arr = [ 2000, 1500, 1000, 500 ]; 201 > f( arr, done ) 202 2000 203 1500 204 1000 205 500 206 { "even": 2, "odd": 2 } 207 208 See Also 209 -------- 210