Skip to content

Add new decorator for casting to Boolean #363

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

Closed
wants to merge 7 commits into from
Closed
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- stable
- 10
- 8

after_success:
Expand Down
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,33 @@ import {deserializeArray} from "class-transformer";
let photos = deserializeArray(Photo, photos);
```

## Type casting

By default typescript emits types metadata and we can infer how to cast them. But `Boolean` works not as expected


```typescript
Boolean('false') === true
Boolean(undefined) === false
```
So there is an opportunity to cast boolean as expected:

```typescript
class Dto {
@ToBoolean()
hasFlag: boolean;
}

plainToClass(Dto, {hasFlag: 'false'})
// { hasFlag: false }
plainToClass(Dto, {hasFlag: 0})
// { hasFlag: false }
plainToClass(Dto, {hasFlag: 1})
// { hasFlag: true }
```



## Enforcing type-safe instance

The default behaviour of the `plainToClass` method is to set *all* properties from the plain object,
Expand Down
18 changes: 18 additions & 0 deletions src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@ export function Transform(transformFn: (value: any, obj: any, transformationType
};
}


/**
* Cast value to boolean type
*/
export function ToBoolean() {
return Transform((value) => {
if (value === "true" || value === "1" || value === 1) {
return true;
}

if (value === "false" || value === 0 || value === "0") {
return false;
}

return value;
});
}

/**
* Specifies a type of the property.
* The given TypeFunction can return a constructor. A discriminator can be given in the options.
Expand Down
58 changes: 58 additions & 0 deletions test/functional/to-boolean-decorator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {plainToClass, ToBoolean} from "../../src";
import { expect } from "chai";

describe("Cast to boolean", () => {
class Dto {
@ToBoolean()
hasFlag: boolean;
}

it("Should cast false value as false", () => {

const result = plainToClass(Dto, { hasFlag: "false" });
expect(result.hasFlag).to.be.false;
});

it("Should cast true value as true", () => {
const result = plainToClass(Dto, { hasFlag: "true" });
expect(result.hasFlag).to.be.true;
});

it("Should not cast undefined as bool", () => {
const result = plainToClass(Dto, { hasFlag: undefined });
expect(result.hasFlag).to.not.be.equals(true);
expect(result.hasFlag).to.not.be.equals(false);
});

it("Should not cast null as bool", () => {
const result = plainToClass(Dto, { hasFlag: null });
expect(result.hasFlag).to.not.be.equals(true);
expect(result.hasFlag).to.not.be.equals(false);
});

it("Should cast 0 as false", () => {
const result = plainToClass(Dto, { hasFlag: 0 });
expect(result.hasFlag).to.be.equals(false);
});

it("Should cast 1 as true", () => {
const result = plainToClass(Dto, { hasFlag: 1 });
expect(result.hasFlag).to.be.equals(true);
});

it("Should cast '0' as false", () => {
const result = plainToClass(Dto, { hasFlag: "0" });
expect(result.hasFlag).to.be.equals(false);
});

it("Should not cast '1' as true", () => {
const result = plainToClass(Dto, { hasFlag: "1" });
expect(result.hasFlag).to.be.equals(true);
});

it("Should remain real bool", () => {
const result = plainToClass(Dto, { hasFlag: true });
expect(result.hasFlag).to.be.equals(true);
});

});