Skip to content

Commit

Permalink
refactor(core): reformat map
Browse files Browse the repository at this point in the history
  • Loading branch information
Chau Tran authored and Chau Tran committed Aug 1, 2021
1 parent cdcc39a commit 81992a1
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions packages/core/src/lib/map/map.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable prefer-const */
import type {
ConditionReturn,
ConvertUsingReturn,
Expand All @@ -17,7 +16,7 @@ import type {
MemberMapReturn,
} from '@automapper/types';
import { MapFnClassId, TransformationType } from '@automapper/types';
import { isEmpty, set, setMutate, get } from '../utils';
import { get, isEmpty, set, setMutate } from '../utils';

/**
* Instruction on how to map a particular member on the destination
Expand Down Expand Up @@ -168,22 +167,26 @@ export function mapMutate<
mapper,
errorHandler,
setMemberFn: setMemberMutateFn(destinationObj),
getMemberFn: getMemberMutateFn(destinationObj)
getMemberFn: getMemberMutateFn(destinationObj),
});
}

interface MapParameter<TSource extends Dictionary<TSource> = any,
TDestination extends Dictionary<TDestination> = any> {
interface MapParameter<
TSource extends Dictionary<TSource> = any,
TDestination extends Dictionary<TDestination> = any
> {
sourceObj: TSource;
mapping: Mapping<TSource, TDestination>;
options: MapOptions<TSource, TDestination>;
mapper: Mapper;
errorHandler: ErrorHandler;
setMemberFn: (
destinationMemberPath: string[],
destination?: TDestination,
destination?: TDestination
) => (value: unknown) => void;
getMemberFn?: (destinationMemberPath: string[] | undefined) => Record<string, unknown>;
getMemberFn?: (
destinationMemberPath: string[] | undefined
) => Record<string, unknown>;
isMapArray?: boolean;
}

Expand All @@ -198,8 +201,10 @@ interface MapParameter<TSource extends Dictionary<TSource> = any,
* @param {Function} getMemberFn
* @param {boolean} [isMapArray = false] - whether the map operation is in Array mode
*/
function map<TSource extends Dictionary<TSource> = any,
TDestination extends Dictionary<TDestination> = any>({
function map<
TSource extends Dictionary<TSource> = any,
TDestination extends Dictionary<TDestination> = any
>({
sourceObj,
mapping,
options,
Expand All @@ -210,8 +215,11 @@ function map<TSource extends Dictionary<TSource> = any,
isMapArray = false,
}: MapParameter) {
// destructure the mapping
let [[, destination], propsToMap, [mappingBeforeAction, mappingAfterAction]] =
mapping;
const [
[, destination],
propsToMap,
[mappingBeforeAction, mappingAfterAction],
] = mapping;

// initialize an array of keys that have already been configured
const configuredKeys: string[] = [];
Expand Down Expand Up @@ -346,7 +354,7 @@ Original error: ${originalError}`;
mapper,
errorHandler,
setMemberFn: setMemberMutateFn(destinationMemberValue),
getMemberFn: getMemberMutateFn(destinationMemberValue)
getMemberFn: getMemberMutateFn(destinationMemberValue),
});
continue;
}
Expand All @@ -360,7 +368,7 @@ Original error: ${originalError}`;
mapper,
errorHandler,
setMemberFn: setMemberReturnFn,
}),
})
);
continue;
}
Expand All @@ -383,7 +391,7 @@ Original error: ${originalError}`;
}

// After map
// Do not map for when in Map Array mode
// Do not run for when in Map Array mode
if (!isMapArray) {
const afterMap = mapAfterAction ?? mappingAfterAction;
if (afterMap) {
Expand Down Expand Up @@ -418,7 +426,7 @@ export function mapArray<
errorHandler: ErrorHandler
) {
// initialize an empty array
let destinationArray: TDestination[] = [];
const destinationArray: TDestination[] = [];

// destructure mapOptions
const { beforeMap, afterMap, extraArguments } = options ?? {};
Expand Down Expand Up @@ -462,7 +470,8 @@ function setMemberMutateFn(destinationObj: Record<string, unknown>) {
}

function getMemberMutateFn(destinationObj: Record<string, unknown>) {
return (memberPath: string[] | undefined) => get(destinationObj, memberPath) as Record<string, unknown>;
return (memberPath: string[] | undefined) =>
get(destinationObj, memberPath) as Record<string, unknown>;
}

function setMemberReturnFn<TDestination extends Dictionary<TDestination> = any>(
Expand Down

0 comments on commit 81992a1

Please sign in to comment.