Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: casting empty numberstring to null #1390

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/TransformOperationExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export class TransformOperationExecutor {
return String(value);
} else if (targetType === Number && !isMap) {
if (value === null || value === undefined) return value;
if (value === '') return null;
return Number(value);
} else if (targetType === Boolean && !isMap) {
if (value === null || value === undefined) return value;
Expand Down
9 changes: 7 additions & 2 deletions test/functional/implicit-type-declarations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ describe('implicit type conversion', () => {

@Expose()
readonly implicitTypeString: string;

@Expose()
readonly implicitTypeEmptyNumber: number;
}

const result1: SimpleExample = plainToInstance(
SimpleExample,
{
implicitTypeNumber: '100',
implicitTypeString: 133123,
implicitTypeEmptyNumber: '',
},
{ enableImplicitConversion: true }
);
Expand All @@ -29,12 +33,13 @@ describe('implicit type conversion', () => {
{
implicitTypeNumber: '100',
implicitTypeString: 133123,
implicitTypeEmptyNumber: '',
},
{ enableImplicitConversion: false }
);

expect(result1).toEqual({ implicitTypeNumber: 100, implicitTypeString: '133123' });
expect(result2).toEqual({ implicitTypeNumber: '100', implicitTypeString: 133123 });
expect(result1).toEqual({ implicitTypeNumber: 100, implicitTypeString: '133123', implicitTypeEmptyNumber: null });
expect(result2).toEqual({ implicitTypeNumber: '100', implicitTypeString: 133123, implicitTypeEmptyNumber: '' });
});
});

Expand Down