Skip to content

Commit

Permalink
feat(core): add mapWithArguments
Browse files Browse the repository at this point in the history
  • Loading branch information
nartc committed Feb 14, 2021
1 parent 018600c commit 2732300
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/core/src/lib/create-mapper/create-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ export function createMapper<TKey = unknown>({
if (
(destinationObjOrOptions &&
('beforeMap' in destinationObjOrOptions ||
'afterMap' in destinationObjOrOptions)) ||
'afterMap' in destinationObjOrOptions ||
'extraArguments' in destinationObjOrOptions)) ||
destinationObjOrOptions == null
) {
return mapReturn(
Expand Down
18 changes: 16 additions & 2 deletions packages/core/src/lib/map/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
MapOptions,
Mapper,
Mapping,
MapWithArgumentsReturn,
MapWithReturn,
MemberMapReturn,
} from '@automapper/types';
Expand All @@ -25,13 +26,15 @@ import { set, setMutate } from './set.util';
* @param {TSource} sourceObj - The sourceObject being used to map to destination
* @param destination - destination meta key
* @param {string} destinationMemberPath - the property path on the destination
* @param {Record<string, any>} extraArguments
* @param {Mapper} mapper - the mapper instance
*/
function mapMember<TSource extends Dictionary<TSource> = any>(
transformationMapFn: MemberMapReturn,
sourceObj: TSource,
destination: unknown,
destinationMemberPath: string,
extraArguments: Record<string, unknown>,
mapper: Mapper
) {
let value: unknown;
Expand Down Expand Up @@ -59,6 +62,12 @@ function mapMember<TSource extends Dictionary<TSource> = any>(
destinationMemberPath
);
break;
case TransformationType.MapWithArguments:
value = (mapFn as MapWithArgumentsReturn[MapFnClassId.fn])(
sourceObj,
extraArguments
);
break;
case TransformationType.MapDefer:
value = mapMember(
(mapFn as MapDeferReturn[MapFnClassId.fn])(
Expand All @@ -67,6 +76,7 @@ function mapMember<TSource extends Dictionary<TSource> = any>(
sourceObj,
destination,
destinationMemberPath,
extraArguments,
mapper
);
break;
Expand Down Expand Up @@ -195,8 +205,11 @@ function map<
const configuredKeys: string[] = [];

// deconstruct MapOptions
const { beforeMap: mapBeforeAction, afterMap: mapAfterAction } =
options ?? {};
const {
beforeMap: mapBeforeAction,
afterMap: mapAfterAction,
extraArguments,
} = options ?? {};

// Before Map
// Do not run before map when in Map Array mode
Expand Down Expand Up @@ -333,6 +346,7 @@ Original error: ${originalError}`;
sourceObj,
destination,
destinationMemberPath,
extraArguments,
mapper
)
);
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/lib/member-map-functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './ignore';
export * from './null-substitution';
export * from './pre-condition';
export * from './map-defer';
export * from './map-with-arguments';
19 changes: 19 additions & 0 deletions packages/core/src/lib/member-map-functions/map-with-arguments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type {
Dictionary,
MapWithArgumentsReturn,
SelectorReturn,
} from '@automapper/types';
import { TransformationType } from '@automapper/types';

export function mapWithArguments<
TSource extends Dictionary<TSource> = any,
TDestination extends Dictionary<TDestination> = any,
TSelectorReturn = SelectorReturn<TDestination>
>(
withArgumentsResolver: (
source: TSource,
extraArguments: Record<string, unknown>
) => TSelectorReturn
): MapWithArgumentsReturn<TSource, TDestination, TSelectorReturn> {
return [TransformationType.MapWithArguments, withArgumentsResolver];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { mapWithArguments } from '@automapper/core';
import { MapFnClassId, TransformationType } from '@automapper/types';

describe('MapWithArgumentsFunction', () => {
const withArgumentsResolver = (
source: any,
extraArguments: { foo: string }
) => source[extraArguments.foo];

it('should return correctly', () => {
const mapWithArgumentsFn = mapWithArguments(withArgumentsResolver);
expect(mapWithArgumentsFn).toBeTruthy();
expect(mapWithArgumentsFn[MapFnClassId.type]).toEqual(
TransformationType.MapWithArguments
);
expect(mapWithArgumentsFn[MapFnClassId.fn]).toBe(withArgumentsResolver);
});

it('should map correctly', () => {
const mapWithArgumentsFn = mapWithArguments(withArgumentsResolver);
const result = mapWithArgumentsFn[MapFnClassId.fn](
{ foo: 'this is awesome' },
{ foo: 'foo' }
);
expect(result).toEqual('this is awesome');
});
});

0 comments on commit 2732300

Please sign in to comment.