Skip to content

Commit

Permalink
refactor(core): adjust public API of core
Browse files Browse the repository at this point in the history
  • Loading branch information
nartc committed Jan 9, 2021
1 parent 2c084f1 commit d86af1d
Show file tree
Hide file tree
Showing 22 changed files with 292 additions and 292 deletions.
251 changes: 0 additions & 251 deletions packages/core/src/lib/create-mapper.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import type {
CreateMapFluentFunction,
Dictionary,
MapAction,
Mapping,
MemberMapFunction,
PreConditionFunction,
Selector,
SelectorReturn,
} from '@automapper/types';
import { MappingClassId } from '@automapper/types';
import { createMapForMember } from './create-map-for-member.util';

/**
* Method to create FluentFunction for chaining forMember etc...
*
* @param {Mapping} mapping - Mapping object of source <> destination
*/
export function createMapFluentFunction<
TSource extends Dictionary<TSource> = unknown,
TDestination extends Dictionary<TDestination> = unknown
>(
mapping: Mapping<TSource, TDestination>
): CreateMapFluentFunction<TSource, TDestination> {
// initialize fluentFunction
const fluentFunction: CreateMapFluentFunction<TSource, TDestination> = {
forMember<TMemberType = SelectorReturn<TDestination>>(
selector: Selector<TDestination, TMemberType>,
...functions: [
(
| ReturnType<PreConditionFunction<TSource, TDestination, TMemberType>>
| ReturnType<MemberMapFunction<TSource, TDestination, TMemberType>>
),
ReturnType<MemberMapFunction<TSource, TDestination, TMemberType>>?
]
): CreateMapFluentFunction<TSource, TDestination> {
return createMapForMember<TSource, TDestination>(
mapping,
selector,
functions,
fluentFunction
);
},
beforeMap(mapAction: MapAction<TSource, TDestination>) {
// assign mapAction to mapping
mapping[MappingClassId.actions][0] = mapAction;
return fluentFunction;
},
afterMap(mapAction: MapAction<TSource, TDestination>) {
// assign mapAction to mapping
mapping[MappingClassId.actions][1] = mapAction;
return fluentFunction;
},
};

return fluentFunction;
}
88 changes: 88 additions & 0 deletions packages/core/src/lib/create-mapper/create-map-for-member.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import type {
CreateMapFluentFunction,
Dictionary,
Mapping,
MappingProperty,
MemberMapFunction,
PreConditionFunction,
Selector,
SelectorReturn,
} from '@automapper/types';
import {
MapFnClassId,
MappingClassId,
MappingPropertiesClassId,
TransformationType,
} from '@automapper/types';
import { getMemberPath } from './get-member-path.util';

/**
*
* @param {Mapping} mapping - Mapping between source <> destination
* @param {Selector} selector - the member selector on `forMember(selector)`
* @param preCond
* @param mapMemberFn
* @param fluentFunction
*/
export function createMapForMember<
TSource extends Dictionary<TSource> = unknown,
TDestination extends Dictionary<TDestination> = unknown,
TMemberType = SelectorReturn<TDestination>
>(
mapping: Mapping<TSource, TDestination>,
selector: Selector<TDestination, TMemberType>,
[preCond, mapMemberFn]: [
preCond:
| ReturnType<PreConditionFunction<TSource, TDestination>>
| ReturnType<MemberMapFunction<TSource, TDestination>>,
mapMemberFn?: ReturnType<MemberMapFunction<TSource, TDestination>>
],
fluentFunction: CreateMapFluentFunction<TSource, TDestination>
): CreateMapFluentFunction<TSource, TDestination> {
// get the memberPath from the selector
// eg: `s => s.foo.bar` returns `foo.bar`
const memberPath = getMemberPath(selector);

// reassign mapMemberFn and preCond
if (mapMemberFn == null) {
mapMemberFn = preCond as ReturnType<MemberMapFunction>;
preCond = undefined;
}

// initialize sourcePath
let sourcePath = '';

// if the transformation is MapFrom or MapWith, we have information on the source value selector
if (
mapMemberFn[MapFnClassId.type] === TransformationType.MapFrom ||
mapMemberFn[MapFnClassId.type] === TransformationType.MapWith
) {
sourcePath = getMemberPath(mapMemberFn[MapFnClassId.misc]);
}

// initialize paths tuple
const paths: [member: string, source?: string] = !sourcePath
? [memberPath]
: [memberPath, sourcePath];

// initialize MappingProperty
const mappingProperty: MappingProperty = [
paths,
[mapMemberFn, preCond as ReturnType<PreConditionFunction>],
];

// check existProp on mapping
const existProp = mapping[MappingClassId.properties].find(
([propName]) => propName === memberPath
);

// if exists, overrides
if (existProp != null) {
existProp[MappingPropertiesClassId.property] = mappingProperty;
return fluentFunction;
}

// push MappingProperty to mapping
mapping[MappingClassId.properties].push([memberPath, mappingProperty]);
return fluentFunction;
}
Loading

0 comments on commit d86af1d

Please sign in to comment.