Skip to content

Commit

Permalink
feat(predicates): isBoolean, isNumber and isString predicates
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-molak committed Apr 30, 2018
1 parent c54efd4 commit 5676694
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 1 deletion.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ cache:
notifications:
email: false
node_js:
- '10'
- '9'
- '8'
- '6'
Expand Down
2 changes: 1 addition & 1 deletion spec/predicates/isArray.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('predicates', () => {
{},
false,
5,
).it('complains if the value is not an array and provides', (value: any) => {
).it('complains if the value is not an array', (value: any) => {
expect(() => new Strings(value)).to.throw(`Collection should be an array`);
});
});
Expand Down
34 changes: 34 additions & 0 deletions spec/predicates/isBoolean.spec.ts
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`);
});
});
});
44 changes: 44 additions & 0 deletions spec/predicates/isNumber.spec.ts
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`);
});
});
});
33 changes: 33 additions & 0 deletions spec/predicates/isString.spec.ts
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`);
});
});
});
3 changes: 3 additions & 0 deletions src/predicates/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './and';
export * from './hasLengthOf';
export * from './isArray';
export * from './isBoolean';
export * from './isDefined';
export * from './isEqualTo';
export * from './isGreaterThan';
Expand All @@ -9,6 +10,8 @@ export * from './isInRange';
export * from './isInteger';
export * from './isLessThan';
export * from './isLessThanOrEqualTo';
export * from './isNumber';
export * from './isOneOf';
export * from './isString';
export * from './or';
export * from './Predicate';
19 changes: 19 additions & 0 deletions src/predicates/isBoolean.ts
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');
}
19 changes: 19 additions & 0 deletions src/predicates/isNumber.ts
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');
}
19 changes: 19 additions & 0 deletions src/predicates/isString.ts
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');
}

0 comments on commit 5676694

Please sign in to comment.