From 1e0fa62b67400609a79290a860c84ffd9c74c0ee Mon Sep 17 00:00:00 2001 From: Chau Tran Date: Wed, 6 Apr 2022 22:44:48 -0500 Subject: [PATCH] feat(core): allow preMap and postMap to also be called with the Mapping --- packages/core/src/lib/core.ts | 28 +++++++++++++------ .../src/lib/default-serializer-options.ts | 5 +++- packages/core/src/lib/types.ts | 24 ++++++++++++---- 3 files changed, 43 insertions(+), 14 deletions(-) diff --git a/packages/core/src/lib/core.ts b/packages/core/src/lib/core.ts index 9d70e95e5..19336b659 100644 --- a/packages/core/src/lib/core.ts +++ b/packages/core/src/lib/core.ts @@ -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, @@ -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 ); }; } @@ -209,7 +213,8 @@ export function createMapper({ ) { let sourceObject = sourceArray[i]; sourceObject = strategy.preMap( - Object.freeze(sourceObject) + Object.freeze(sourceObject), + mapping ); const destination = mapReturn( @@ -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 ); } @@ -283,7 +289,10 @@ export function createMapper({ destinationIdentifier ); - sourceObject = strategy.preMap(Object.freeze(sourceObject)); + sourceObject = strategy.preMap( + Object.freeze(sourceObject), + mapping + ); mapMutate( mapping, @@ -294,7 +303,8 @@ export function createMapper({ strategy.postMap( Object.freeze(sourceObject), - destinationObject + destinationObject, + mapping ); }; } @@ -356,7 +366,8 @@ export function createMapper({ let sourceObject = sourceArray[i]; sourceObject = strategy.preMap( - Object.freeze(sourceObject) + Object.freeze(sourceObject), + mapping ); mapMutate( @@ -374,7 +385,8 @@ export function createMapper({ strategy.postMap( Object.freeze(sourceObject), - destinationArray[i] + destinationArray[i], + mapping ); } diff --git a/packages/core/src/lib/default-serializer-options.ts b/packages/core/src/lib/default-serializer-options.ts index bd1b3f02d..aa8d48862 100644 --- a/packages/core/src/lib/default-serializer-options.ts +++ b/packages/core/src/lib/default-serializer-options.ts @@ -3,7 +3,10 @@ import type { Dictionary, MappingStrategyInitializerOptions } from './types'; export const defaultSerializerOptions = { applyMetadata: defaultApplyMetadata, - preMap>(source: TSource): TSource { + preMap< + TSource extends Dictionary, + TDestination extends Dictionary + >(source: TSource): TSource { return source; }, postMap< diff --git a/packages/core/src/lib/types.ts b/packages/core/src/lib/types.ts index 6c2bcb573..9c36eed7e 100644 --- a/packages/core/src/lib/types.ts +++ b/packages/core/src/lib/types.ts @@ -1,4 +1,4 @@ -import { +import type { ERROR_HANDLER, MAPPINGS, METADATA_MAP, @@ -544,13 +544,20 @@ export interface MappingStrategy { retrieveMetadata( ...identifiers: TIdentifier[] ): Map; - preMap>(source: TSource): TSource; + preMap< + TSource extends Dictionary, + TDestination extends Dictionary + >( + source: TSource, + mapping: Mapping + ): TSource; postMap< TSource extends Dictionary, TDestination extends Dictionary >( source: TSource, - destination: TDestination + destination: TDestination, + mapping: Mapping ): TDestination | undefined; } @@ -560,12 +567,19 @@ export type MappingStrategyInitializer = export interface MappingStrategyInitializerOptions { applyMetadata?: ApplyMetadata; destinationConstructor?: DestinationConstructor; - preMap?>(source: TSource): TSource; + preMap?< + TSource extends Dictionary, + TDestination extends Dictionary + >( + source: TSource, + mapping: Mapping + ): TSource; postMap?< TSource extends Dictionary, TDestination extends Dictionary >( source: TSource, - destination: TDestination + destination: TDestination, + mapping: Mapping ): TDestination; }