Skip to content

Commit 12bc7d8

Browse files
feat(common): Add map-in-place support to map()
1 parent 87fa4d4 commit 12bc7d8

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

src/common/common.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -292,15 +292,15 @@ export function find(collection: any, callback: any) {
292292
}
293293

294294
/** Given an object, returns a new object, where each property is transformed by the callback function */
295-
export let mapObj: <T, U>(collection: { [key: string]: T }, callback: Mapper<T, U>) => { [key: string]: U } = map;
295+
export let mapObj: <T, U>(collection: { [key: string]: T }, callback: Mapper<T, U>, target?: typeof collection) => { [key: string]: U } = map;
296296
/** Given an array, returns a new array, where each element is transformed by the callback function */
297-
export function map<T, U>(collection: T[], callback: Mapper<T, U>): U[];
298-
export function map<T, U>(collection: { [key: string]: T }, callback: Mapper<T, U>): { [key: string]: U };
297+
export function map<T, U>(collection: T[], callback: Mapper<T, U>, target?: typeof collection): U[];
298+
export function map<T, U>(collection: { [key: string]: T }, callback: Mapper<T, U>, target: typeof collection): { [key: string]: U };
299299
/** Maps an array or object properties using a callback function */
300-
export function map(collection: any, callback: any): any {
301-
const result = isArray(collection) ? [] : {};
302-
forEach(collection, (item, i) => result[i] = callback(item, i));
303-
return result;
300+
export function map(collection: any, callback: any, target: typeof collection): any {
301+
target = target || (isArray(collection) ? [] : {});
302+
forEach(collection, (item, i) => target[i] = callback(item, i));
303+
return target;
304304
}
305305

306306
/**

test/commonSpec.ts

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
defaults, filter, is, eq, not, pattern, val, isInjectable
33
} from "../src/index";
4-
import { pick } from '../src/common/common';
4+
import { map, mapObj, pick } from '../src/common/common';
55

66
describe('common', function() {
77
describe('filter', function() {
@@ -126,4 +126,40 @@ describe('common', function() {
126126
expect(pick(obj, ['baz'])).toEqual({ });
127127
});
128128
});
129+
130+
describe('map', () => {
131+
it('should map arrays', () => {
132+
const src = [1, 2, 3, 4];
133+
const dest = map(src, x => x * 2);
134+
135+
expect(src).toEqual([1, 2, 3, 4]);
136+
expect(dest).toEqual([2, 4, 6, 8]);
137+
});
138+
139+
it('should map arrays in place when target === src', () => {
140+
const src = [1, 2, 3, 4];
141+
const dest = map(src, x => x * 2, src);
142+
143+
expect(src).toEqual([2, 4, 6, 8]);
144+
expect(dest).toEqual([2, 4, 6, 8]);
145+
});
146+
});
147+
148+
describe('mapObj', () => {
149+
it('should map objects', () => {
150+
const src = { foo: 1, bar: 2, baz: 3 };
151+
const dest = mapObj(src, x => x * 2);
152+
153+
expect(src).toEqual({ foo: 1, bar: 2, baz: 3 });
154+
expect(dest).toEqual({ foo: 2, bar: 4, baz: 6 });
155+
});
156+
157+
it('should map objects in place when target === src', () => {
158+
const src = { foo: 1, bar: 2, baz: 3 };
159+
const dest = mapObj(src, x => x * 2, src);
160+
161+
expect(src).toEqual({ foo: 2, bar: 4, baz: 6 });
162+
expect(dest).toEqual({ foo: 2, bar: 4, baz: 6 });
163+
});
164+
});
129165
});

0 commit comments

Comments
 (0)