repl.txt (5387B)
1 2 {{alias}}( obj, [options,] transform, done ) 3 Maps keys from one object to a new object having the same values. 4 5 When invoked, `transform` is provided a maximum of four arguments: 6 7 - `key`: object key 8 - `value`: object value corresponding to `key` 9 - `obj`: the input object 10 - `next`: a callback to be invoked after processing an object `key` 11 12 The actual number of provided arguments depends on function length. If 13 `transform` accepts two arguments, `transform` is provided: 14 15 - `key` 16 - `next` 17 18 If `transform` accepts three arguments, `transform` is provided: 19 20 - `key` 21 - `value` 22 - `next` 23 24 For every other `transform` signature, `transform` is provided all four 25 arguments. 26 27 The `next` callback accepts two arguments: 28 29 - `error`: error argument 30 - `key`: transformed key 31 32 If a `transform` function calls the `next` callback with a truthy `error` 33 argument, the function suspends execution and immediately calls the `done` 34 callback for subsequent `error` handling. 35 36 The key returned by a transform function should be a value which can be 37 serialized as an object key. 38 39 Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, 40 wrap the `done` callback in a function which either executes at the end of 41 the current stack (e.g., `nextTick`) or during a subsequent turn of the 42 event loop (e.g., `setImmediate`, `setTimeout`). 43 44 The function only maps own properties. Hence, the function does not map 45 inherited properties. 46 47 The function shallow copies key values. 48 49 Key iteration and insertion order are *not* guaranteed. 50 51 Parameters 52 ---------- 53 obj: Object 54 Source object. 55 56 options: Object (optional) 57 Function options. 58 59 options.limit: integer (optional) 60 Maximum number of pending invocations. Default: Infinity. 61 62 options.series: boolean (optional) 63 Boolean indicating whether to process each property sequentially. 64 Default: false. 65 66 options.thisArg: any (optional) 67 Execution context. 68 69 transform: Function 70 Transform function. Returned values specify the keys of the output 71 object. 72 73 done: Function 74 A callback invoked either upon processing all own properties or upon 75 encountering an error. 76 77 Examples 78 -------- 79 // Basic usage: 80 > function transform( key, value, next ) { 81 ... setTimeout( onTimeout, value ); 82 ... function onTimeout() { 83 ... next( null, key+':'+value ); 84 ... } 85 ... }; 86 > function done( error, out ) { 87 ... if ( error ) { 88 ... throw error; 89 ... } 90 ... console.log( out ); 91 ... }; 92 > var obj = { 'a': 1, 'b': 2 }; 93 > {{alias}}( obj, transform, done ) 94 { 'a:1': 1, 'b:2': 2 } 95 96 // Limit number of concurrent invocations: 97 > function transform( key, value, next ) { 98 ... setTimeout( onTimeout, value ); 99 ... function onTimeout() { 100 ... next( null, key+':'+value ); 101 ... } 102 ... }; 103 > function done( error, out ) { 104 ... if ( error ) { 105 ... throw error; 106 ... } 107 ... console.log( out ); 108 ... }; 109 > var opts = { 'limit': 2 }; 110 > var obj = { 'a': 1, 'b': 2, 'c': 3 }; 111 > {{alias}}( obj, opts, transform, done ) 112 { 'a:1': 1, 'b:2': 2, 'c:3': 3 } 113 114 // Process sequentially: 115 > function transform( key, value, next ) { 116 ... setTimeout( onTimeout, value ); 117 ... function onTimeout() { 118 ... next( null, key+':'+value ); 119 ... } 120 ... }; 121 > function done( error, out ) { 122 ... if ( error ) { 123 ... throw error; 124 ... } 125 ... console.log( out ); 126 ... }; 127 > var opts = { 'series': true }; 128 > var obj = { 'a': 1, 'b': 2, 'c': 3 }; 129 > {{alias}}( obj, opts, transform, done ) 130 { 'a:1': 1, 'b:2': 2, 'c:3': 3 } 131 132 133 {{alias}}.factory( [options,] transform ) 134 Returns a function which maps keys from one object to a new object having 135 the same values. 136 137 Parameters 138 ---------- 139 options: Object (optional) 140 Function options. 141 142 options.limit: integer (optional) 143 Maximum number of pending invocations. Default: Infinity. 144 145 options.series: boolean (optional) 146 Boolean indicating whether to process each property sequentially. 147 Default: false. 148 149 options.thisArg: any (optional) 150 Execution context. 151 152 transform: Function 153 Transform function. Returned values specify the keys of the output 154 object. 155 156 Returns 157 ------- 158 out: Function 159 A function which maps keys from one object to a new object having the 160 same values. 161 162 Examples 163 -------- 164 > function transform( key, value, next ) { 165 ... setTimeout( onTimeout, value ); 166 ... function onTimeout() { 167 ... next( null, key+':'+value ); 168 ... } 169 ... }; 170 > var opts = { 'series': true }; 171 > var f = {{alias}}.factory( opts, transform ); 172 > function done( error, out ) { 173 ... if ( error ) { 174 ... throw error; 175 ... } 176 ... console.log( out ); 177 ... }; 178 > var obj = { 'a': 1, 'b': 2, 'c': 3 }; 179 > f( obj, done ) 180 { 'a:1': 1, 'b:2': 2, 'c:3': 3 } 181 > obj = { 'beep': 'boop' }; 182 > f( obj, done ) 183 { 'beep:boop': 'beep' } 184 185 See Also 186 -------- 187