From 7dd66ab7970bd6468c8de6428589e0e6e22bb8c7 Mon Sep 17 00:00:00 2001 From: Chau Tran Date: Sat, 28 Mar 2020 14:51:20 -0500 Subject: [PATCH] fix(automapper.ts): fix misc bugs --- src/core/create-map-for-member.ts | 21 ++++++++++++++++--- src/core/create-mapping-object.ts | 4 ++-- .../create-reverse-map-fluent-function.ts | 2 -- src/core/create-reverse-mapping-object.ts | 4 ++-- src/core/initialize-reverse-mapping-props.ts | 2 +- src/explorers/metadata.explorer.ts | 8 +------ src/storages/mapping.storage.ts | 2 +- src/utils/getPathFromSelector.ts | 11 +++++++--- 8 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/core/create-map-for-member.ts b/src/core/create-map-for-member.ts index 25f4737fa..78824ff71 100644 --- a/src/core/create-map-for-member.ts +++ b/src/core/create-map-for-member.ts @@ -1,6 +1,7 @@ import { BaseOf, Dict, + MapFromFunction, Mapping, MappingClassId, MappingProperty, @@ -8,8 +9,9 @@ import { MemberMapFunctionReturnClassId, PreConditionFunction, Selector, + TransformationType, } from '../types'; -import { getMemberPath } from '../utils'; +import { getMemberPath, isThisMemberMap } from '../utils'; export function createMapForMember< TSource extends Dict = any, @@ -34,12 +36,25 @@ export function createMapForMember< preCond = undefined as any; } + let sourcePath: string = ''; + if ( + isThisMemberMap(mapMemberFn, TransformationType.MapFrom) + ) { + sourcePath = getMemberPath( + mapMemberFn[MemberMapFunctionReturnClassId.misc] + ); + } + + const paths: [string, string?] = !!sourcePath + ? [memberPath, sourcePath] + : [memberPath]; + const mappingProperty: MappingProperty< TSource, TDestination, ReturnType > = Object.seal({ - paths: [memberPath], + paths, transformation: { mapFn: mapMemberFn, type: mapMemberFn[MemberMapFunctionReturnClassId.type], @@ -61,7 +76,7 @@ export function createMapForMember< mapping[MappingClassId.props].push([ memberPath, Object.seal({ - paths: [memberPath], + paths, transformation: { mapFn: mapMemberFn, type: mapMemberFn[MemberMapFunctionReturnClassId.type], diff --git a/src/core/create-mapping-object.ts b/src/core/create-mapping-object.ts index e0d8b7f59..c592d14e2 100644 --- a/src/core/create-mapping-object.ts +++ b/src/core/create-mapping-object.ts @@ -34,14 +34,14 @@ export function createMappingObject< TDestination, TBaseSource, TBaseDestination - > = Object.seal([ + > = [ [source, destination], [ options.sourceMemberNamingConvention, options.destinationMemberNamingConvention, ] as Mapping[MappingClassId.conventions], [], - ]); + ]; mappingStorage.set(source, destination, mapping); return mapping; } diff --git a/src/core/create-reverse-map-fluent-function.ts b/src/core/create-reverse-map-fluent-function.ts index 404990baa..a16ebb6ab 100644 --- a/src/core/create-reverse-map-fluent-function.ts +++ b/src/core/create-reverse-map-fluent-function.ts @@ -41,12 +41,10 @@ export function createReverseMapFluentFunction< reversedMapFluentFunction ), beforeMap: action => { - mapping[MappingClassId.actions] = mapping[MappingClassId.actions] || []; (mapping[MappingClassId.actions] as any)[0] = action; return reversedMapFluentFunction; }, afterMap: action => { - mapping[MappingClassId.actions] = mapping[MappingClassId.actions] || []; (mapping[MappingClassId.actions] as any)[1] = action; return reversedMapFluentFunction; }, diff --git a/src/core/create-reverse-mapping-object.ts b/src/core/create-reverse-mapping-object.ts index f46b575ec..66e62156f 100644 --- a/src/core/create-reverse-mapping-object.ts +++ b/src/core/create-reverse-mapping-object.ts @@ -23,13 +23,13 @@ export function createReverseMappingObject< return reversedMapping; } - reversedMapping = Object.seal([ + reversedMapping = [ [destination, source], [destinationConvention, sourceConvention], initializeReverseMappingProps(mapping), [], bases?.slice().reverse() as [Constructible, Constructible], - ]); + ]; if (reversedMapping[MappingClassId.bases]) { const reversedBaseMapping = getMappingForDestination( diff --git a/src/core/initialize-reverse-mapping-props.ts b/src/core/initialize-reverse-mapping-props.ts index 5adb3f96b..1307c7613 100644 --- a/src/core/initialize-reverse-mapping-props.ts +++ b/src/core/initialize-reverse-mapping-props.ts @@ -76,7 +76,7 @@ export function initializeReverseMappingProps< transformation: { type: TransformationType.MapInitialize, preCond: undefined, - mapFn: mapInitialize(path), + mapFn: mapInitialize(destPath), }, }), ]); diff --git a/src/explorers/metadata.explorer.ts b/src/explorers/metadata.explorer.ts index 3bc5826f2..36569c745 100644 --- a/src/explorers/metadata.explorer.ts +++ b/src/explorers/metadata.explorer.ts @@ -19,13 +19,7 @@ export class MetadataExplorer { } private static exploreInternal(model: Constructible): void { - const modelProto = model.prototype || null; - - if (!modelProto) { - return; - } - - if (this.metadataTrackMap.has(model)) { + if (!model.prototype || this.metadataTrackMap.has(model)) { return; } diff --git a/src/storages/mapping.storage.ts b/src/storages/mapping.storage.ts index 809ba776b..7dd49361b 100644 --- a/src/storages/mapping.storage.ts +++ b/src/storages/mapping.storage.ts @@ -4,7 +4,7 @@ import { Constructible, Mapping } from '../types'; * Internal MappingStorage class * @private */ -export class MappingStorage { +class MappingStorage { private mappings: WeakMap>; constructor() { diff --git a/src/utils/getPathFromSelector.ts b/src/utils/getPathFromSelector.ts index 04f3e6432..282af6c09 100644 --- a/src/utils/getPathFromSelector.ts +++ b/src/utils/getPathFromSelector.ts @@ -1,7 +1,12 @@ export function getPathFromSelector(fnParts: string[]): string { - return fnParts + const [, ...parts] = fnParts .join('') .split(new RegExp(`${fnParts[0]}\\.{1}`, 'g')) - .filter(Boolean) - .pop() as string; + .filter(Boolean); + + if (parts.length === 1) { + return parts.pop() as string; + } + + return ''; }