-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(predicates): isBoolean, isNumber and isString predicates
- Loading branch information
Showing
9 changed files
with
173 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -5,6 +5,7 @@ cache: | |
notifications: | ||
email: false | ||
node_js: | ||
- '10' | ||
- '9' | ||
- '8' | ||
- '6' | ||
|
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,34 @@ | ||
import 'mocha'; | ||
import { given } from 'mocha-testdata'; | ||
|
||
import { ensure, isBoolean, TinyType } from '../../src'; | ||
import { expect } from '../expect'; | ||
|
||
describe('predicates', () => { | ||
|
||
/** @test {isBoolean} */ | ||
describe('::isBoolean', () => { | ||
|
||
class MarketingOptIn extends TinyType { | ||
constructor(public readonly value: boolean) { | ||
super(); | ||
|
||
ensure('MarketingOptIn', value, isBoolean()); | ||
} | ||
} | ||
|
||
it('ensures that the argument is a boolean value', () => { | ||
expect(new MarketingOptIn(false)).to.not.throw; // tslint:disable-line:no-unused-expression | ||
}); | ||
|
||
given( | ||
undefined, | ||
null, | ||
{}, | ||
'string', | ||
5, | ||
).it('complains if the value is not a boolean', (value: any) => { | ||
expect(() => new MarketingOptIn(value)).to.throw(`MarketingOptIn should be a 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import 'mocha'; | ||
import { given } from 'mocha-testdata'; | ||
|
||
import { ensure, isNumber, TinyType } from '../../src'; | ||
import { expect } from '../expect'; | ||
|
||
describe('predicates', () => { | ||
|
||
/** @test {isNumber} */ | ||
describe('::isNumber', () => { | ||
class Percentage extends TinyType { | ||
constructor(public readonly value: number) { | ||
super(); | ||
|
||
ensure('Percentage', value, isNumber()); | ||
} | ||
} | ||
|
||
it('ensures that the argument in a number', () => { | ||
expect(new Percentage(42)).to.not.throw; // tslint:disable-line:no-unused-expression | ||
}); | ||
|
||
given( | ||
1 / 3, | ||
0.42, | ||
0o3, | ||
0xB4D455, | ||
NaN, | ||
Infinity, | ||
-Infinity, | ||
).it('works for any type of number', (value: any) => { | ||
expect(() => new Percentage(value)).to.not.throw; // tslint:disable-line:no-unused-expression | ||
}); | ||
|
||
given( | ||
undefined, | ||
null, | ||
{}, | ||
'string', | ||
).it('complains if the value is not a number', (value: any) => { | ||
expect(() => new Percentage(value)).to.throw(`Percentage should be a number`); | ||
}); | ||
}); | ||
}); |
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 'mocha'; | ||
import { given } from 'mocha-testdata'; | ||
|
||
import { ensure, isString, TinyType } from '../../src'; | ||
import { expect } from '../expect'; | ||
|
||
describe('predicates', () => { | ||
|
||
/** @test {isString} */ | ||
describe('::isString', () => { | ||
class FirstName extends TinyType { | ||
constructor(public readonly value: string) { | ||
super(); | ||
|
||
ensure('FirstName', value, isString()); | ||
} | ||
} | ||
|
||
it('ensures that the argument in a string', () => { | ||
expect(new FirstName('Jan')).to.not.throw; // tslint:disable-line:no-unused-expression | ||
}); | ||
|
||
given( | ||
undefined, | ||
null, | ||
{}, | ||
[], | ||
42, | ||
).it('complains if the value is not a string', (value: any) => { | ||
expect(() => new FirstName(value)).to.throw(`FirstName should be a 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
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,19 @@ | ||
import { Predicate } from './Predicate'; | ||
|
||
/** | ||
* @desc Ensures that the `value` is a {@link Boolean} value. | ||
* | ||
* @example | ||
* import { ensure, isBoolean, TinyType } from 'tiny-types'; | ||
* | ||
* class MarketingOptIn extends TinyType { | ||
* constructor(public readonly value: boolean) { | ||
* ensure('MarketingOptIn', value, isBoolean()); | ||
* } | ||
* } | ||
* | ||
* @returns {Predicate<boolean>} | ||
*/ | ||
export function isBoolean(): Predicate<boolean> { | ||
return Predicate.to(`be a boolean value`, (value: boolean) => typeof value === 'boolean'); | ||
} |
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,19 @@ | ||
import { Predicate } from './Predicate'; | ||
|
||
/** | ||
* @desc Ensures that the `value` is a {@link Number}. | ||
* | ||
* @example | ||
* import { ensure, isNumber, TinyType } from 'tiny-types'; | ||
* | ||
* class Percentage extends TinyType { | ||
* constructor(public readonly value: number) { | ||
* ensure('Percentage', value, isNumber()); | ||
* } | ||
* } | ||
* | ||
* @returns {Predicate<number>} | ||
*/ | ||
export function isNumber(): Predicate<number> { | ||
return Predicate.to(`be a number`, (value: number) => typeof value === 'number'); | ||
} |
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,19 @@ | ||
import { Predicate } from './Predicate'; | ||
|
||
/** | ||
* @desc Ensures that the `value` is an integer {@link Number}. | ||
* | ||
* @example | ||
* import { ensure, isString, TinyType } from 'tiny-types'; | ||
* | ||
* class FirstName extends TinyType { | ||
* constructor(public readonly value: string) { | ||
* ensure('FirstName', value, isString()); | ||
* } | ||
* } | ||
* | ||
* @returns {Predicate<string>} | ||
*/ | ||
export function isString(): Predicate<string> { | ||
return Predicate.to(`be a string`, (value: string) => typeof value === 'string'); | ||
} |