Skip to content

Commit

Permalink
fix(core): adjust code smell
Browse files Browse the repository at this point in the history
  • Loading branch information
nartc committed Jan 30, 2021
1 parent cf53a1f commit 736f58d
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 38 deletions.
8 changes: 4 additions & 4 deletions packages/core/src/lib/map/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import type {
Mapping,
MapWithFunction,
MemberMapFunction,
NullSubstitutionFunction,
} from '@automapper/types';
import { MapFnClassId, TransformationType } from '@automapper/types';
import { isEmpty } from '../utils';
Expand Down Expand Up @@ -63,9 +62,10 @@ function mapMember<TSource extends Dictionary<TSource> = unknown>(
break;
case TransformationType.Condition:
case TransformationType.NullSubstitution:
value = (mapFn as ReturnType<
NullSubstitutionFunction | ConditionFunction
>[MapFnClassId.fn])(sourceObj, destinationMemberPath);
value = (mapFn as ReturnType<ConditionFunction>[MapFnClassId.fn])(
sourceObj,
destinationMemberPath
);
break;
case TransformationType.MapDefer:
value = mapMember(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { MapFnClassId, TransformationType } from '@automapper/types';

describe('ConvertUsingFunction', () => {
const dateToStringConverter: Converter<Date, string> = {
convert(source: Date): string {
return source.toDateString();
convert(date: Date): string {
return date.toDateString();
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ describe('MapFromFunction', () => {
});

const resolver: Resolver<typeof source> = {
resolve(source: { foo: string }) {
return source.foo;
resolve(src: { foo: string }) {
return src.foo;
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
});
});
4 changes: 2 additions & 2 deletions packages/core/src/lib/utils/get.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ export function get<T>(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]);
Expand Down

0 comments on commit 736f58d

Please sign in to comment.