Skip to content

Commit

Permalink
fix(automapper.ts): fix misc bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
nartc committed Mar 28, 2020
1 parent dd079ac commit 7dd66ab
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 21 deletions.
21 changes: 18 additions & 3 deletions src/core/create-map-for-member.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import {
BaseOf,
Dict,
MapFromFunction,
Mapping,
MappingClassId,
MappingProperty,
MemberMapFunction,
MemberMapFunctionReturnClassId,
PreConditionFunction,
Selector,
TransformationType,
} from '../types';
import { getMemberPath } from '../utils';
import { getMemberPath, isThisMemberMap } from '../utils';

export function createMapForMember<
TSource extends Dict<TSource> = any,
Expand All @@ -34,12 +36,25 @@ export function createMapForMember<
preCond = undefined as any;
}

let sourcePath: string = '';
if (
isThisMemberMap<MapFromFunction>(mapMemberFn, TransformationType.MapFrom)
) {
sourcePath = getMemberPath(
mapMemberFn[MemberMapFunctionReturnClassId.misc]
);
}

const paths: [string, string?] = !!sourcePath
? [memberPath, sourcePath]
: [memberPath];

const mappingProperty: MappingProperty<
TSource,
TDestination,
ReturnType<TSelector>
> = Object.seal({
paths: [memberPath],
paths,
transformation: {
mapFn: mapMemberFn,
type: mapMemberFn[MemberMapFunctionReturnClassId.type],
Expand All @@ -61,7 +76,7 @@ export function createMapForMember<
mapping[MappingClassId.props].push([
memberPath,
Object.seal({
paths: [memberPath],
paths,
transformation: {
mapFn: mapMemberFn,
type: mapMemberFn[MemberMapFunctionReturnClassId.type],
Expand Down
4 changes: 2 additions & 2 deletions src/core/create-mapping-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 0 additions & 2 deletions src/core/create-reverse-map-fluent-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand Down
4 changes: 2 additions & 2 deletions src/core/create-reverse-mapping-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/core/initialize-reverse-mapping-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function initializeReverseMappingProps<
transformation: {
type: TransformationType.MapInitialize,
preCond: undefined,
mapFn: mapInitialize(path),
mapFn: mapInitialize(destPath),
},
}),
]);
Expand Down
8 changes: 1 addition & 7 deletions src/explorers/metadata.explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/storages/mapping.storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Constructible, Mapping } from '../types';
* Internal MappingStorage class
* @private
*/
export class MappingStorage {
class MappingStorage {
private mappings: WeakMap<Constructible, WeakMap<Constructible, Mapping>>;

constructor() {
Expand Down
11 changes: 8 additions & 3 deletions src/utils/getPathFromSelector.ts
Original file line number Diff line number Diff line change
@@ -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 '';
}

0 comments on commit 7dd66ab

Please sign in to comment.