Skip to content

Commit

Permalink
feat(core): MapCallback is now called with extraArguments
Browse files Browse the repository at this point in the history
  • Loading branch information
nartc committed Jul 5, 2022
1 parent 73d5936 commit 60a1da8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
9 changes: 6 additions & 3 deletions packages/core/src/lib/mappings/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,16 @@ export function map<
destinationIdentifier
);

// get extraArguments
const extraArguments = extraArgs?.(mapping, destination);

// initialize an array of keys that have already been configured
const configuredKeys: string[] = [];

if (!isMapArray) {
const beforeMap = mapBeforeCallback ?? mappingBeforeCallback;
if (beforeMap) {
beforeMap(sourceObject, destination);
beforeMap(sourceObject, destination, extraArguments);
}
}

Expand Down Expand Up @@ -335,7 +338,7 @@ Original error: ${originalError}`;
sourceObject,
destination,
destinationMemberPath,
extraArgs?.(mapping, destination),
extraArguments,
mapper,
sourceMemberIdentifier,
destinationMemberIdentifier
Expand All @@ -346,7 +349,7 @@ Original error: ${originalError}`;
if (!isMapArray) {
const afterMap = mapAfterCallback ?? mappingAfterCallback;
if (afterMap) {
afterMap(sourceObject, destination);
afterMap(sourceObject, destination, extraArguments);
}
}

Expand Down
13 changes: 9 additions & 4 deletions packages/core/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,21 @@ export interface Converter<

export type MapCallback<
TSource extends Dictionary<TSource>,
TDestination extends Dictionary<TDestination>
> = (source: TSource, destination: TDestination) => void;
TDestination extends Dictionary<TDestination>,
TExtraArgs extends Record<string, any> = Record<string, any>
> = (
source: TSource,
destination: TDestination,
extraArguments?: TExtraArgs
) => void;

export interface MapOptions<
TSource extends Dictionary<TSource>,
TDestination extends Dictionary<TDestination>,
TExtraArgs extends Record<string, any> = Record<string, any>
> {
beforeMap?: MapCallback<TSource, TDestination>;
afterMap?: MapCallback<TSource, TDestination>;
beforeMap?: MapCallback<TSource, TDestination, TExtraArgs>;
afterMap?: MapCallback<TSource, TDestination, TExtraArgs>;
destinationConstructor?: DestinationConstructor<TSource, TDestination>;
extraArgs?: (
mapping: Mapping<TSource, TDestination>,
Expand Down

0 comments on commit 60a1da8

Please sign in to comment.