From d223d791a5ed3de75e47eae87a596ffde77efe82 Mon Sep 17 00:00:00 2001 From: Chau Tran Date: Wed, 24 Feb 2021 13:27:33 -0600 Subject: [PATCH] fix(core): clean up optional chaining --- .../src/lib/create-mapper/create-mapper.ts | 8 +++++-- .../create-initial-mapping.util.ts | 4 +++- .../initialize-utils/extend-mappings.util.ts | 5 ++++- packages/core/src/lib/map/map.ts | 22 ++++++++++++++----- .../lib/member-map-functions/convert-using.ts | 7 ++++-- 5 files changed, 34 insertions(+), 12 deletions(-) diff --git a/packages/core/src/lib/create-mapper/create-mapper.ts b/packages/core/src/lib/create-mapper/create-mapper.ts index dc3b5910b..78ea87b9d 100644 --- a/packages/core/src/lib/create-mapper/create-mapper.ts +++ b/packages/core/src/lib/create-mapper/create-mapper.ts @@ -60,7 +60,9 @@ export function createMapper({ 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); @@ -152,7 +154,9 @@ export function createMapper({ ); }, dispose() { - plugin.dispose?.(); + if (plugin.dispose) { + plugin.dispose(); + } }, }; } diff --git a/packages/core/src/lib/initialize-utils/create-initial-mapping.util.ts b/packages/core/src/lib/initialize-utils/create-initial-mapping.util.ts index e79d51756..4587dae3a 100644 --- a/packages/core/src/lib/initialize-utils/create-initial-mapping.util.ts +++ b/packages/core/src/lib/initialize-utils/create-initial-mapping.util.ts @@ -66,7 +66,9 @@ export function createInitialMapping( undefined, ]; - prePropertiesLoop?.(mapping); + if (prePropertiesLoop) { + prePropertiesLoop(mapping); + } const destinationPaths = getPathRecursive(destinationObj) || []; const namingConventions = mapping[MappingClassId.namingConventions]; diff --git a/packages/core/src/lib/initialize-utils/extend-mappings.util.ts b/packages/core/src/lib/initialize-utils/extend-mappings.util.ts index f4913b690..d6591396b 100644 --- a/packages/core/src/lib/initialize-utils/extend-mappings.util.ts +++ b/packages/core/src/lib/initialize-utils/extend-mappings.util.ts @@ -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] ); diff --git a/packages/core/src/lib/map/map.ts b/packages/core/src/lib/map/map.ts index 2207387e5..286846e3e 100644 --- a/packages/core/src/lib/map/map.ts +++ b/packages/core/src/lib/map/map.ts @@ -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 @@ -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 @@ -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, + mapper.getMapping(source, destination) as Mapping< + TSource, + TDestination + >, { extraArguments }, mapper, errorHandler, @@ -412,7 +420,9 @@ export function mapArray< } // run afterMap for the whole map operation - afterMap?.(sourceArray, destinationArray); + if (afterMap) { + afterMap(sourceArray, destinationArray); + } return destinationArray; } diff --git a/packages/core/src/lib/member-map-functions/convert-using.ts b/packages/core/src/lib/member-map-functions/convert-using.ts index 4bacf50fa..de6f59f26 100644 --- a/packages/core/src/lib/member-map-functions/convert-using.ts +++ b/packages/core/src/lib/member-map-functions/convert-using.ts @@ -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; }, ];