-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
65 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
packages/core/src/lib/member-map-functions/map-with-arguments.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
27 changes: 27 additions & 0 deletions
27
packages/core/src/lib/member-map-functions/specs/map-with-arguments.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |