Skip to content

Commit

Permalink
fix(core): clean up optional chaining
Browse files Browse the repository at this point in the history
  • Loading branch information
nartc committed Feb 24, 2021
1 parent 65304a7 commit d223d79
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
8 changes: 6 additions & 2 deletions packages/core/src/lib/create-mapper/create-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ export function createMapper<TKey = unknown>({
const { preMap } = plugin;

// run preMap if available
const [sourceInstance] = preMap?.(source, destination, sourceObj) ?? [];
const [sourceInstance] = preMap
? preMap(source, destination, sourceObj)
: [];

// get mapping between Source and Destination
const mapping = this.getMapping(source, destination);
Expand Down Expand Up @@ -152,7 +154,9 @@ export function createMapper<TKey = unknown>({
);
},
dispose() {
plugin.dispose?.();
if (plugin.dispose) {
plugin.dispose();
}
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ export function createInitialMapping(
undefined,
];

prePropertiesLoop?.(mapping);
if (prePropertiesLoop) {
prePropertiesLoop(mapping);
}

const destinationPaths = getPathRecursive(destinationObj) || [];
const namingConventions = mapping[MappingClassId.namingConventions];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ export function extendMappings(bases: any[], mapping: Mapping) {
]);
}
}
mapping[MappingClassId.bases] ??= [];

if (mapping[MappingClassId.bases] == null) {
mapping[MappingClassId.bases] = [];
}
mapping[MappingClassId.bases]!.push(
mappingToExtend[MappingClassId.mappings]
);
Expand Down
22 changes: 16 additions & 6 deletions packages/core/src/lib/map/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ function map<
// Do not run before map when in Map Array mode
if (!isMapArray) {
const beforeMap = mapBeforeAction ?? mappingBeforeAction;
beforeMap?.(sourceObj, destination);
if (beforeMap) {
beforeMap(sourceObj, destination);
}
}

// map
Expand Down Expand Up @@ -358,7 +360,9 @@ Original error: ${originalError}`;
// Do not map for when in Map Array mode
if (!isMapArray) {
const afterMap = mapAfterAction ?? mappingAfterAction;
afterMap?.(sourceObj, destination);
if (afterMap) {
afterMap(sourceObj, destination);
}
}

// Check unmapped properties
Expand Down Expand Up @@ -394,15 +398,19 @@ export function mapArray<
const { beforeMap, afterMap, extraArguments } = options ?? {};

// run beforeMap for the whole map operation
beforeMap?.(sourceArray, []);
if (beforeMap) {
beforeMap(sourceArray, destinationArray);
}

// loop through each item and run map() for each
for (let i = 0, len = sourceArray.length; i < len; i++) {
const mapping = mapper.getMapping(source, destination);
destinationArray.push(
mapReturn(
sourceArray[i],
mapping as Mapping<TSource, TDestination>,
mapper.getMapping(source, destination) as Mapping<
TSource,
TDestination
>,
{ extraArguments },
mapper,
errorHandler,
Expand All @@ -412,7 +420,9 @@ export function mapArray<
}

// run afterMap for the whole map operation
afterMap?.(sourceArray, destinationArray);
if (afterMap) {
afterMap(sourceArray, destinationArray);
}

return destinationArray;
}
7 changes: 5 additions & 2 deletions packages/core/src/lib/member-map-functions/convert-using.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ export function convertUsing<
return [
TransformationType.ConvertUsing,
(source) => {
const valueToConvert =
value?.(source) ?? ((source as unknown) as TConvertSource);
let valueToConvert = (source as unknown) as TConvertSource;

if (value) {
valueToConvert = value(source);
}
return (converter.convert(valueToConvert) as unknown) as TConvertSource;
},
];
Expand Down

0 comments on commit d223d79

Please sign in to comment.