diff --git a/src/common/common.ts b/src/common/common.ts index bdebac37..2fb52fa6 100644 --- a/src/common/common.ts +++ b/src/common/common.ts @@ -292,15 +292,15 @@ export function find(collection: any, callback: any) { } /** Given an object, returns a new object, where each property is transformed by the callback function */ -export let mapObj: (collection: { [key: string]: T }, callback: Mapper) => { [key: string]: U } = map; +export let mapObj: (collection: { [key: string]: T }, callback: Mapper, target?: typeof collection) => { [key: string]: U } = map; /** Given an array, returns a new array, where each element is transformed by the callback function */ -export function map(collection: T[], callback: Mapper): U[]; -export function map(collection: { [key: string]: T }, callback: Mapper): { [key: string]: U }; +export function map(collection: T[], callback: Mapper, target?: typeof collection): U[]; +export function map(collection: { [key: string]: T }, callback: Mapper, target: typeof collection): { [key: string]: U }; /** Maps an array or object properties using a callback function */ -export function map(collection: any, callback: any): any { - const result = isArray(collection) ? [] : {}; - forEach(collection, (item, i) => result[i] = callback(item, i)); - return result; +export function map(collection: any, callback: any, target: typeof collection): any { + target = target || (isArray(collection) ? [] : {}); + forEach(collection, (item, i) => target[i] = callback(item, i)); + return target; } /** diff --git a/test/commonSpec.ts b/test/commonSpec.ts index f9dc15ea..984773a7 100644 --- a/test/commonSpec.ts +++ b/test/commonSpec.ts @@ -1,7 +1,7 @@ import { defaults, filter, is, eq, not, pattern, val, isInjectable } from "../src/index"; -import { pick } from '../src/common/common'; +import { map, mapObj, pick } from '../src/common/common'; describe('common', function() { describe('filter', function() { @@ -126,4 +126,40 @@ describe('common', function() { expect(pick(obj, ['baz'])).toEqual({ }); }); }); + + describe('map', () => { + it('should map arrays', () => { + const src = [1, 2, 3, 4]; + const dest = map(src, x => x * 2); + + expect(src).toEqual([1, 2, 3, 4]); + expect(dest).toEqual([2, 4, 6, 8]); + }); + + it('should map arrays in place when target === src', () => { + const src = [1, 2, 3, 4]; + const dest = map(src, x => x * 2, src); + + expect(src).toEqual([2, 4, 6, 8]); + expect(dest).toEqual([2, 4, 6, 8]); + }); + }); + + describe('mapObj', () => { + it('should map objects', () => { + const src = { foo: 1, bar: 2, baz: 3 }; + const dest = mapObj(src, x => x * 2); + + expect(src).toEqual({ foo: 1, bar: 2, baz: 3 }); + expect(dest).toEqual({ foo: 2, bar: 4, baz: 6 }); + }); + + it('should map objects in place when target === src', () => { + const src = { foo: 1, bar: 2, baz: 3 }; + const dest = mapObj(src, x => x * 2, src); + + expect(src).toEqual({ foo: 2, bar: 4, baz: 6 }); + expect(dest).toEqual({ foo: 2, bar: 4, baz: 6 }); + }); + }); });