Skip to content

Commit

Permalink
refactor(core): clean up misc. in core
Browse files Browse the repository at this point in the history
  • Loading branch information
nartc committed Feb 14, 2021
1 parent 997528b commit 61f4f65
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 34 deletions.
15 changes: 5 additions & 10 deletions packages/core/src/lib/create-mapper/create-map-for-member.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function createMapForMember<
}

// initialize sourcePath
let sourcePath = '';
let sourcePath: string;

// if the transformation is MapFrom or MapWith, we have information on the source value selector
if (
Expand All @@ -61,14 +61,9 @@ export function createMapForMember<
sourcePath = getMemberPath(mapMemberFn[MapFnClassId.misc]!);
}

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

// initialize MappingProperty
const mappingProperty: MappingProperty = [
paths,
[memberPath, sourcePath],
[mapMemberFn, preCond as PreConditionReturn],
];

Expand All @@ -80,10 +75,10 @@ export function createMapForMember<
// if exists, overrides
if (existProp != null) {
existProp[MappingPropertiesClassId.property] = mappingProperty;
return fluentFunction;
} else {
// push MappingProperty to mapping
mapping[MappingClassId.properties].push([memberPath, mappingProperty]);
}

// push MappingProperty to mapping
mapping[MappingClassId.properties].push([memberPath, mappingProperty]);
return fluentFunction;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import type {
CreateMapOptions,
Mapping,
MappingProperty,
} from '@automapper/types';
import type { CreateMapOptions, Mapping } from '@automapper/types';
import { MappingClassId } from '@automapper/types';
import { isDefined } from '../utils';
import { extendMappings } from './extend-mappings.util';
Expand Down Expand Up @@ -109,10 +105,7 @@ export function createInitialMapping(

mapping[MappingClassId.properties].push([
destinationPath,
Object.freeze(([
[destinationPath],
[mapInitialize(...sourcePaths!)],
] as unknown) as MappingProperty),
[[destinationPath], [mapInitialize(...sourcePaths!)]],
destinationNestedMetadataAtPath,
]);
continue;
Expand All @@ -125,10 +118,7 @@ export function createInitialMapping(

mapping[MappingClassId.properties].push([
destinationPath,
Object.freeze(([
[destinationPath, sourcePath],
[mapInitialize(sourcePath)],
] as unknown) as MappingProperty),
[[destinationPath, sourcePath], [mapInitialize(sourcePath)]],
destinationNestedMetadataAtPath,
]);
}
Expand Down
10 changes: 4 additions & 6 deletions packages/core/src/lib/initialize-utils/extend-mappings.util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Mapping, MappingProperty } from '@automapper/types';
import type { Mapping } from '@automapper/types';
import { MappingClassId, MappingPropertiesClassId } from '@automapper/types';

export function extendMappings(bases: unknown[], mapping: Mapping) {
Expand All @@ -11,18 +11,16 @@ export function extendMappings(bases: unknown[], mapping: Mapping) {
);
if (existProp) {
existProp[MappingPropertiesClassId.path] = propToExtendKey;
existProp[MappingPropertiesClassId.property] = Object.freeze(
propToExtendMappingProp
) as MappingProperty;
existProp[MappingPropertiesClassId.property] = propToExtendMappingProp;
} else {
mapping[MappingClassId.properties].push([
propToExtendKey,
Object.freeze(propToExtendMappingProp) as MappingProperty,
propToExtendMappingProp,
]);
}
}
mapping[MappingClassId.bases] ??= [];
mapping[MappingClassId.bases]?.push(
mapping[MappingClassId.bases]!.push(
mappingToExtend[MappingClassId.mappings]
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import type { NamingConvention } from '@automapper/types';

/**
* Convert namingConventions params to [convention, convention] format
*
* @param namingConventions
*/
export function getNamingConventionsFromOptions(
namingConventions?:
| NamingConvention
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* Loop through an object and recursively get paths of each property
*
* @param node
* @param prefix
* @param prev
*/
export function getPathRecursive(
node: unknown,
prefix = '',
Expand All @@ -17,10 +24,7 @@ export function getPathRecursive(

const child = node[key];
if (typeof child === 'object') {
let queue = [child];
if (Array.isArray(child)) {
queue = child;
}
const queue = Array.isArray(child) ? child : [child];

for (const childNode of queue) {
const childPaths = getPathRecursive(childNode, path + '.');
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/lib/utils/get.util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export function get<T>(object: T, ...paths: string[]): unknown {
if (!paths?.length) {
if (!paths.length) {
return;
}

Expand Down

0 comments on commit 61f4f65

Please sign in to comment.