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