Skip to content

Commit

Permalink
feat(core): allow preMap and postMap to also be called with the Mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
nartc committed Apr 7, 2022
1 parent e462b96 commit 1e0fa62
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
28 changes: 20 additions & 8 deletions packages/core/src/lib/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ export function createMapper({
destinationIdentifier
);

sourceObject = strategy.preMap(Object.freeze(sourceObject));
sourceObject = strategy.preMap(
Object.freeze(sourceObject),
mapping
);

const destination = mapReturn(
mapping,
Expand All @@ -149,7 +152,8 @@ export function createMapper({
Object.freeze(sourceObject),
// seal destination so that consumers cannot add properties to it
// or change the property descriptors. but they can still modify it
Object.seal(destination)
Object.seal(destination),
mapping
);
};
}
Expand Down Expand Up @@ -209,7 +213,8 @@ export function createMapper({
) {
let sourceObject = sourceArray[i];
sourceObject = strategy.preMap(
Object.freeze(sourceObject)
Object.freeze(sourceObject),
mapping
);

const destination = mapReturn(
Expand All @@ -229,7 +234,8 @@ export function createMapper({
Object.freeze(sourceObject),
// seal destination so that consumers cannot add properties to it
// or change the property descriptors. but they can still modify it
Object.seal(destination)
Object.seal(destination),
mapping
) as TDestination
);
}
Expand Down Expand Up @@ -283,7 +289,10 @@ export function createMapper({
destinationIdentifier
);

sourceObject = strategy.preMap(Object.freeze(sourceObject));
sourceObject = strategy.preMap(
Object.freeze(sourceObject),
mapping
);

mapMutate(
mapping,
Expand All @@ -294,7 +303,8 @@ export function createMapper({

strategy.postMap(
Object.freeze(sourceObject),
destinationObject
destinationObject,
mapping
);
};
}
Expand Down Expand Up @@ -356,7 +366,8 @@ export function createMapper({
let sourceObject = sourceArray[i];

sourceObject = strategy.preMap(
Object.freeze(sourceObject)
Object.freeze(sourceObject),
mapping
);

mapMutate(
Expand All @@ -374,7 +385,8 @@ export function createMapper({

strategy.postMap(
Object.freeze(sourceObject),
destinationArray[i]
destinationArray[i],
mapping
);
}

Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/lib/default-serializer-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import type { Dictionary, MappingStrategyInitializerOptions } from './types';

export const defaultSerializerOptions = {
applyMetadata: defaultApplyMetadata,
preMap<TSource extends Dictionary<TSource>>(source: TSource): TSource {
preMap<
TSource extends Dictionary<TSource>,
TDestination extends Dictionary<TDestination>
>(source: TSource): TSource {
return source;
},
postMap<
Expand Down
24 changes: 19 additions & 5 deletions packages/core/src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import type {
ERROR_HANDLER,
MAPPINGS,
METADATA_MAP,
Expand Down Expand Up @@ -544,13 +544,20 @@ export interface MappingStrategy<TIdentifier extends MetadataIdentifier> {
retrieveMetadata(
...identifiers: TIdentifier[]
): Map<TIdentifier, MetadataList>;
preMap<TSource extends Dictionary<TSource>>(source: TSource): TSource;
preMap<
TSource extends Dictionary<TSource>,
TDestination extends Dictionary<TDestination>
>(
source: TSource,
mapping: Mapping<TSource, TDestination>
): TSource;
postMap<
TSource extends Dictionary<TSource>,
TDestination extends Dictionary<TDestination>
>(
source: TSource,
destination: TDestination
destination: TDestination,
mapping: Mapping<TSource, TDestination>
): TDestination | undefined;
}

Expand All @@ -560,12 +567,19 @@ export type MappingStrategyInitializer<TIdentifier extends MetadataIdentifier> =
export interface MappingStrategyInitializerOptions {
applyMetadata?: ApplyMetadata;
destinationConstructor?: DestinationConstructor;
preMap?<TSource extends Dictionary<TSource>>(source: TSource): TSource;
preMap?<
TSource extends Dictionary<TSource>,
TDestination extends Dictionary<TDestination>
>(
source: TSource,
mapping: Mapping<TSource, TDestination>
): TSource;
postMap?<
TSource extends Dictionary<TSource>,
TDestination extends Dictionary<TDestination>
>(
source: TSource,
destination: TDestination
destination: TDestination,
mapping: Mapping<TSource, TDestination>
): TDestination;
}

0 comments on commit 1e0fa62

Please sign in to comment.