Skip to content

Commit

Permalink
Compact after mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
skonves committed Oct 26, 2024
1 parent 629bb5e commit 2b932f3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/mapper-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ export class ExpressMapperFactory extends BaseFactory {
): Iterable<string> {
const paramName = mode === 'input' ? 'dto' : 'obj';
yield `${this.buildSignature(type.name.value, paramName, mode)} {`;
yield `return {`;
this.touchCompact();
yield `return compact({`;
for (const prop of type.properties) {
yield* this.buildProperty(prop, paramName, mode);
}
yield `};`;
yield `});`;
yield `}`;
yield '';
}
Expand Down
34 changes: 22 additions & 12 deletions src/snapshot/v1/express/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,51 @@ import type * as types from '../types';

import type * as dtos from './dtos';

function compact<T extends object>(obj: T): T {
return Object.keys(obj).reduce((acc, key) => {
const value = obj[key as keyof T];
if (value !== undefined) {
acc[key as keyof T] = value;
}
return acc;
}, {} as T);
}

export function mapFromCreateWidgetBodyDto(
dto: dtos.CreateWidgetBodyDto,
): types.CreateWidgetBody {
return {
return compact({
name: dto.name,
};
});
}

export function mapFromExhaustiveParamsBodyDto(
dto: dtos.ExhaustiveParamsBodyDto,
): types.ExhaustiveParamsBody {
return {
return compact({
bar: dto.bar,
foo: dto.foo,
};
});
}

export function mapToGizmoDto(obj: types.Gizmo): dtos.GizmoDto {
return {
return compact({
id: obj.id,
name: obj.name,
size: obj.size,
};
});
}

export function mapToGizmosResponseDto(
obj: types.GizmosResponse,
): dtos.GizmosResponseDto {
return {
return compact({
data: obj.data?.map(mapToGizmoDto),
};
});
}

export function mapToWidgetDto(obj: types.Widget): dtos.WidgetDto {
return {
return compact({
buzz: obj.buzz,
fiz: obj.fiz,
fizbuzz: obj.fizbuzz,
Expand All @@ -59,12 +69,12 @@ export function mapToWidgetDto(obj: types.Widget): dtos.WidgetDto {
id: obj.id,
name: obj.name,
size: obj.size,
};
});
}

export function mapToWidgetFooDto(obj: types.WidgetFoo): dtos.WidgetFooDto {
return {
return compact({
buzz: obj.buzz,
fiz: obj.fiz,
};
});
}

0 comments on commit 2b932f3

Please sign in to comment.