added _.transform definition

This commit is contained in:
Brian Zengel
2013-10-20 17:39:55 -04:00
parent 50121d7a01
commit c36fca3f91
2 changed files with 29 additions and 1 deletions
+12
View File
@@ -699,6 +699,18 @@ result = <any>_.pick({ 'name': 'moe', '_userid': 'moe1' }, function(value, key)
return key.charAt(0) != '_';
});
result = <any>_.transform([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], function(result, num) {
num *= num;
if (num % 2) {
return result.push(num) < 3;
}
});
// → [1, 9, 25]
result = <any>_.transform({ 'a': 1, 'b': 2, 'c': 3 }, function(result, num, key) {
result[key] = num * 3;
});
////////////////////////////////////////////////////////////////////////////////////////
//WHAT'S LEFT
////////////////////////////////////////////////////////////////////////////////////////
+17 -1
View File
@@ -2678,7 +2678,23 @@ declare module _ {
callback: ObjectIterator<any, boolean>,
thisArg?: any): any;
/**
* An alternative to _.reduce this method transforms object to a new accumulator object which is
* the result of running each of its elements through a callback, with each callback execution
* potentially mutating the accumulator object. The callback is bound to thisArg and invoked with
* four arguments; (accumulator, value, key, object). Callbacks may exit iteration early by
* explicitly returning false.
* @param collection The collection to iterate over.
* @param callback The function called per iteration.
* @param accumulator The custom accumulator value.
* @param thisArg The this binding of callback.
* @return The accumulated value.
**/
export function transform<T>(
collection: Collection<T>,
callback?: MemoIterator<T, any>,
accumulator?: any,
thisArg?: any): any