Skip to content

Commit

Permalink
feat(common): Add map-in-place support to map()
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherthielen committed Jan 17, 2018
1 parent 87fa4d4 commit 12bc7d8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
14 changes: 7 additions & 7 deletions src/common/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: <T, U>(collection: { [key: string]: T }, callback: Mapper<T, U>) => { [key: string]: U } = map;
export let mapObj: <T, U>(collection: { [key: string]: T }, callback: Mapper<T, U>, 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<T, U>(collection: T[], callback: Mapper<T, U>): U[];
export function map<T, U>(collection: { [key: string]: T }, callback: Mapper<T, U>): { [key: string]: U };
export function map<T, U>(collection: T[], callback: Mapper<T, U>, target?: typeof collection): U[];
export function map<T, U>(collection: { [key: string]: T }, callback: Mapper<T, U>, 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;
}

/**
Expand Down
38 changes: 37 additions & 1 deletion test/commonSpec.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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 });
});
});
});

0 comments on commit 12bc7d8

Please sign in to comment.