-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): adjust public API of core
- Loading branch information
Showing
22 changed files
with
292 additions
and
292 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
57 changes: 57 additions & 0 deletions
57
packages/core/src/lib/create-mapper/create-map-fluent-function.util.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
88
packages/core/src/lib/create-mapper/create-map-for-member.util.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.