From 736f58d94012d901ccc6980c026e06afe75eba98 Mon Sep 17 00:00:00 2001 From: Chau Tran Date: Fri, 29 Jan 2021 21:26:02 -0600 Subject: [PATCH] fix(core): adjust code smell --- packages/core/src/lib/map/map.ts | 8 +++--- .../specs/convert-using.spec.ts | 4 +-- .../specs/map-from.spec.ts | 4 +-- .../camel-case-naming-convention.spec.ts | 26 +++++++++++------ .../pascal-case-naming-convention.spec.ts | 26 +++++++++++------ .../snake-case-naming-convention.spec.ts | 28 ++++++++++++------- packages/core/src/lib/utils/get.util.ts | 4 +-- 7 files changed, 62 insertions(+), 38 deletions(-) diff --git a/packages/core/src/lib/map/map.ts b/packages/core/src/lib/map/map.ts index 9893e4dd2..447554e90 100644 --- a/packages/core/src/lib/map/map.ts +++ b/packages/core/src/lib/map/map.ts @@ -13,7 +13,6 @@ import type { Mapping, MapWithFunction, MemberMapFunction, - NullSubstitutionFunction, } from '@automapper/types'; import { MapFnClassId, TransformationType } from '@automapper/types'; import { isEmpty } from '../utils'; @@ -63,9 +62,10 @@ function mapMember = unknown>( break; case TransformationType.Condition: case TransformationType.NullSubstitution: - value = (mapFn as ReturnType< - NullSubstitutionFunction | ConditionFunction - >[MapFnClassId.fn])(sourceObj, destinationMemberPath); + value = (mapFn as ReturnType[MapFnClassId.fn])( + sourceObj, + destinationMemberPath + ); break; case TransformationType.MapDefer: value = mapMember( diff --git a/packages/core/src/lib/member-map-functions/specs/convert-using.spec.ts b/packages/core/src/lib/member-map-functions/specs/convert-using.spec.ts index c39dfb8ad..f76aab58b 100644 --- a/packages/core/src/lib/member-map-functions/specs/convert-using.spec.ts +++ b/packages/core/src/lib/member-map-functions/specs/convert-using.spec.ts @@ -4,8 +4,8 @@ import { MapFnClassId, TransformationType } from '@automapper/types'; describe('ConvertUsingFunction', () => { const dateToStringConverter: Converter = { - convert(source: Date): string { - return source.toDateString(); + convert(date: Date): string { + return date.toDateString(); }, }; diff --git a/packages/core/src/lib/member-map-functions/specs/map-from.spec.ts b/packages/core/src/lib/member-map-functions/specs/map-from.spec.ts index 19038f34a..1b89aa71a 100644 --- a/packages/core/src/lib/member-map-functions/specs/map-from.spec.ts +++ b/packages/core/src/lib/member-map-functions/specs/map-from.spec.ts @@ -24,8 +24,8 @@ describe('MapFromFunction', () => { }); const resolver: Resolver = { - resolve(source: { foo: string }) { - return source.foo; + resolve(src: { foo: string }) { + return src.foo; }, }; diff --git a/packages/core/src/lib/naming-conventions/specs/camel-case-naming-convention.spec.ts b/packages/core/src/lib/naming-conventions/specs/camel-case-naming-convention.spec.ts index c823f436c..ec304cf71 100644 --- a/packages/core/src/lib/naming-conventions/specs/camel-case-naming-convention.spec.ts +++ b/packages/core/src/lib/naming-conventions/specs/camel-case-naming-convention.spec.ts @@ -8,32 +8,40 @@ describe('CamelCaseNamingConvention', () => { const snakedTwo = ['address', 'street']; const snakedThree = ['formatted', 'address', 'street']; const toSplit = 'formattedAddressStreet'; - const namingConvention = new CamelCaseNamingConvention(); + const camelCaseNamingConvention = new CamelCaseNamingConvention(); it('should instantiate', () => { - expect(namingConvention).toBeTruthy(); + expect(camelCaseNamingConvention).toBeTruthy(); }); it('should split correctly', () => { const split = toSplit - .split(namingConvention.splittingExpression) + .split(camelCaseNamingConvention.splittingExpression) .filter(Boolean); expect(split).toEqual(['formatted', 'Address', 'Street']); }); it('should convert PascalCase to camelCase', () => { - const convertedOne = namingConvention.transformPropertyName(one); - const convertedTwo = namingConvention.transformPropertyName(two); - const convertedThree = namingConvention.transformPropertyName(three); + const convertedOne = camelCaseNamingConvention.transformPropertyName(one); + const convertedTwo = camelCaseNamingConvention.transformPropertyName(two); + const convertedThree = camelCaseNamingConvention.transformPropertyName( + three + ); expect(convertedOne).toEqual('address'); expect(convertedTwo).toEqual('addressStreet'); expect(convertedThree).toEqual(toSplit); }); it('should convert lowercase (snake_case) to camelCase', () => { - const convertedOne = namingConvention.transformPropertyName(snakedOne); - const convertedTwo = namingConvention.transformPropertyName(snakedTwo); - const convertedThree = namingConvention.transformPropertyName(snakedThree); + const convertedOne = camelCaseNamingConvention.transformPropertyName( + snakedOne + ); + const convertedTwo = camelCaseNamingConvention.transformPropertyName( + snakedTwo + ); + const convertedThree = camelCaseNamingConvention.transformPropertyName( + snakedThree + ); expect(convertedOne).toEqual('address'); expect(convertedTwo).toEqual('addressStreet'); expect(convertedThree).toEqual(toSplit); diff --git a/packages/core/src/lib/naming-conventions/specs/pascal-case-naming-convention.spec.ts b/packages/core/src/lib/naming-conventions/specs/pascal-case-naming-convention.spec.ts index d2293b6b7..d02ac4d4c 100644 --- a/packages/core/src/lib/naming-conventions/specs/pascal-case-naming-convention.spec.ts +++ b/packages/core/src/lib/naming-conventions/specs/pascal-case-naming-convention.spec.ts @@ -8,32 +8,40 @@ describe('PascalCaseNamingConvention', () => { const snakedTwo = ['address', 'street']; const snakedThree = ['formatted', 'address', 'street']; const toSplit = 'FormattedAddressStreet'; - const namingConvention = new PascalCaseNamingConvention(); + const pascalCaseNamingConvention = new PascalCaseNamingConvention(); it('should instantiate', () => { - expect(namingConvention).toBeTruthy(); + expect(pascalCaseNamingConvention).toBeTruthy(); }); it('should split correctly', () => { const split = toSplit - .split(namingConvention.splittingExpression) + .split(pascalCaseNamingConvention.splittingExpression) .filter(Boolean); expect(split).toEqual(['Formatted', 'Address', 'Street']); }); it('should convert camelCase to PascalCase', () => { - const convertedOne = namingConvention.transformPropertyName(one); - const convertedTwo = namingConvention.transformPropertyName(two); - const convertedThree = namingConvention.transformPropertyName(three); + const convertedOne = pascalCaseNamingConvention.transformPropertyName(one); + const convertedTwo = pascalCaseNamingConvention.transformPropertyName(two); + const convertedThree = pascalCaseNamingConvention.transformPropertyName( + three + ); expect(convertedOne).toEqual('Address'); expect(convertedTwo).toEqual('AddressStreet'); expect(convertedThree).toEqual(toSplit); }); it('should convert lowercase (snake_case) to PascalCase', () => { - const convertedOne = namingConvention.transformPropertyName(snakedOne); - const convertedTwo = namingConvention.transformPropertyName(snakedTwo); - const convertedThree = namingConvention.transformPropertyName(snakedThree); + const convertedOne = pascalCaseNamingConvention.transformPropertyName( + snakedOne + ); + const convertedTwo = pascalCaseNamingConvention.transformPropertyName( + snakedTwo + ); + const convertedThree = pascalCaseNamingConvention.transformPropertyName( + snakedThree + ); expect(convertedOne).toEqual('Address'); expect(convertedTwo).toEqual('AddressStreet'); expect(convertedThree).toEqual(toSplit); diff --git a/packages/core/src/lib/naming-conventions/specs/snake-case-naming-convention.spec.ts b/packages/core/src/lib/naming-conventions/specs/snake-case-naming-convention.spec.ts index 831b34764..131aabdd8 100644 --- a/packages/core/src/lib/naming-conventions/specs/snake-case-naming-convention.spec.ts +++ b/packages/core/src/lib/naming-conventions/specs/snake-case-naming-convention.spec.ts @@ -8,39 +8,47 @@ describe('SnakeCaseNamingConvention', () => { const pascalTwo = ['Address', 'Street']; const pascalThree = ['Formatted', 'Address', 'Street']; const toSplit = 'formatted_address_street'; - const namingConvention = new SnakeCaseNamingConvention(); + const snakeCaseNamingConvention = new SnakeCaseNamingConvention(); it('should instantiate', () => { - expect(namingConvention).toBeTruthy(); + expect(snakeCaseNamingConvention).toBeTruthy(); }); it('should split correctly', () => { const split = toSplit - .split(namingConvention.splittingExpression) + .split(snakeCaseNamingConvention.splittingExpression) .filter(Boolean); expect(split).toEqual(['formatted', 'address', 'street']); }); it('should convert camelCase to snake_case', () => { - const convertedOne = namingConvention.transformPropertyName(one); - const convertedTwo = namingConvention.transformPropertyName(two); - const convertedThree = namingConvention.transformPropertyName(three); + const convertedOne = snakeCaseNamingConvention.transformPropertyName(one); + const convertedTwo = snakeCaseNamingConvention.transformPropertyName(two); + const convertedThree = snakeCaseNamingConvention.transformPropertyName( + three + ); expect(convertedOne).toEqual('address'); expect(convertedTwo).toEqual('address_street'); expect(convertedThree).toEqual(toSplit); }); it('should convert PascalCase to snake_case', () => { - const convertedOne = namingConvention.transformPropertyName(pascalOne); - const convertedTwo = namingConvention.transformPropertyName(pascalTwo); - const convertedThree = namingConvention.transformPropertyName(pascalThree); + const convertedOne = snakeCaseNamingConvention.transformPropertyName( + pascalOne + ); + const convertedTwo = snakeCaseNamingConvention.transformPropertyName( + pascalTwo + ); + const convertedThree = snakeCaseNamingConvention.transformPropertyName( + pascalThree + ); expect(convertedOne).toEqual('address'); expect(convertedTwo).toEqual('address_street'); expect(convertedThree).toEqual(toSplit); }); it('should convert to empty string if provide empty string', () => { - const converted = namingConvention.transformPropertyName(['']); + const converted = snakeCaseNamingConvention.transformPropertyName(['']); expect(converted).toEqual(''); }); }); diff --git a/packages/core/src/lib/utils/get.util.ts b/packages/core/src/lib/utils/get.util.ts index ed21b6e06..70abed16e 100644 --- a/packages/core/src/lib/utils/get.util.ts +++ b/packages/core/src/lib/utils/get.util.ts @@ -3,9 +3,9 @@ export function get(object: T, ...paths: string[]): unknown { return; } - function _getInternal(object: T, path: string) { + function _getInternal(innerObject: T, path: string) { const _path = path.split('.').filter(Boolean); - return _path.reduce((obj: unknown, key) => obj && obj[key], object); + return _path.reduce((obj: unknown, key) => obj && obj[key], innerObject); } let val = _getInternal(object, paths[0]);