-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
61 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "valid-objects-ts", | ||
"version": "1.0.10", | ||
"version": "1.0.11", | ||
"main": ".dist/index.js", | ||
"types": ".dist/index.d.ts", | ||
"repository": "[email protected]:Travelport-Czech/valid-objects-ts.git", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { ValidObjectError } from '@/errors/ValidObjectError' | ||
|
||
export class InvalidBooleanError extends ValidObjectError { | ||
constructor(value: string) { | ||
super("Invalid boolean '" + value + "'.") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { ValidBoolean } from 'src' | ||
import { expect } from 'chai' | ||
|
||
describe('ValidBoolean', () => { | ||
it('constructor', () => { | ||
const testError = (val: string, err: string) => { | ||
expect(() => new ValidBoolean(val)).to.throw(err) | ||
} | ||
|
||
expect(new ValidBoolean(true).value).to.eq(true) | ||
expect(new ValidBoolean(true).toString()).to.eq('true') | ||
expect(new ValidBoolean(false).value).to.eq(false) | ||
|
||
testError('true', `Invalid boolean '"true" is type string'.`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { InvalidBooleanError } from '@/errors/InvalidBooleanError' | ||
|
||
const validate = (val: unknown): boolean => { | ||
if (typeof val !== 'boolean') { | ||
throw new InvalidBooleanError(JSON.stringify(val) + ' is type ' + typeof val) | ||
} | ||
|
||
return val | ||
} | ||
|
||
export class ValidBoolean { | ||
private readonly val: boolean | ||
|
||
constructor(val: unknown) { | ||
this.val = validate(val) | ||
} | ||
|
||
get value(): boolean { | ||
return this.val | ||
} | ||
|
||
public toString() { | ||
return this.val.toString() | ||
} | ||
|
||
get [Symbol.toStringTag]() { | ||
return this.toString() | ||
} | ||
|
||
public toJSON() { | ||
return this.toString() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters