From a6e165ec11d827fe22550c9af868b29e67b9e5b7 Mon Sep 17 00:00:00 2001 From: Alex Ford Date: Mon, 1 Jun 2015 11:53:49 -0400 Subject: [PATCH] Add _.mapObject --- underscore/underscore-tests.ts | 4 ++++ underscore/underscore.d.ts | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/underscore/underscore-tests.ts b/underscore/underscore-tests.ts index 60c1708d0..d530a4334 100644 --- a/underscore/underscore-tests.ts +++ b/underscore/underscore-tests.ts @@ -182,6 +182,10 @@ _.omit({ name: 'moe', age: 50, userid: 'moe1' }, 'name'); _.omit({ name: 'moe', age: 50, userid: 'moe1' }, 'name', 'age'); _.omit({ name: 'moe', age: 50, userid: 'moe1' }, ['name', 'age']); +_.mapObject({ a: 1, b: 2 }, val => val * 2) === _.mapObject({ a: 2, b: 4 }, _.identity); +_.mapObject({ a: 1, b: 2 }, (val, key, o) => o[key] * 2) === _.mapObject({ a: 2, b: 4}, _.identity); +_.mapObject({ x: "string 1", y: "string 2" }, 'length') === _.mapObject({ x: "string 1", y: "string 2"}, _.property('length')); + var iceCream = { flavor: "chocolate" }; _.defaults(iceCream, { flavor: "vanilla", sprinkles: "lots" }); diff --git a/underscore/underscore.d.ts b/underscore/underscore.d.ts index 03088bffd..653d3ea07 100644 --- a/underscore/underscore.d.ts +++ b/underscore/underscore.d.ts @@ -1170,6 +1170,31 @@ interface UnderscoreStatic { * @return List of all the values on `object`. **/ values(object: any): any[]; + + /** + * Like map, but for objects. Transform the value of each property in turn. + * @param object The object to transform + * @param iteratee The function that transforms property values + * @param context The optional context (value of `this`) to bind to + * @return a new _.Dictionary of property values + */ + mapObject(object: _.Dictionary, iteratee: (val: T, key: string, object: _.Dictionary) => U, context?: any): _.Dictionary; + + /** + * Like map, but for objects. Transform the value of each property in turn. + * @param object The object to transform + * @param iteratee The function that tranforms property values + * @param context The optional context (value of `this`) to bind to + */ + mapObject(object: any, iteratee: (val: any, key: string, object: any) => T, context?: any): _.Dictionary; + + /** + * Like map, but for objects. Retrieves a property from each entry in the object, as if by _.property + * @param object The object to transform + * @param iteratee The property name to retrieve + * @param context The optional context (value of `this`) to bind to + */ + mapObject(object: any, iteratee: string, context?: any): _.Dictionary; /** * Convert an object into a list of [key, value] pairs.