From afcaeba60efcafcf3d12d5267c1a2911f072266b Mon Sep 17 00:00:00 2001 From: Yousef Soliman Date: Mon, 8 Oct 2018 14:23:12 +0100 Subject: [PATCH 1/2] feat(TinyType): Added new predicate to check instance types --- spec/predicates/isInstanceOf.spec.ts | 33 ++++++++++++++++++++++++++++ src/predicates/index.ts | 1 + src/predicates/isInstanceOf.ts | 21 ++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 spec/predicates/isInstanceOf.spec.ts create mode 100644 src/predicates/isInstanceOf.ts diff --git a/spec/predicates/isInstanceOf.spec.ts b/spec/predicates/isInstanceOf.spec.ts new file mode 100644 index 00000000..887ec600 --- /dev/null +++ b/spec/predicates/isInstanceOf.spec.ts @@ -0,0 +1,33 @@ +import 'mocha'; +import { given } from 'mocha-testdata'; + +import { ensure, isInstanceOf, TinyType } from '../../src'; +import { expect } from '../expect'; + +describe('predicates', () => { + + /** @test {isInstanceOf} */ + describe('::isInstanceOf', () => { + class Birthday extends TinyType { + constructor(public readonly value: Date) { + super(); + + ensure('Birthday', value, isInstanceOf(Date)); + } + } + + it('ensures that the argument is an instance of Date', () => { + expect(() => new Birthday(new Date())).to.not.throw; // tslint:disable-line:no-unused-expression + }); + + given( + '2018-10-10', + undefined, + null, + {}, + 'string', + ).it('complains if the value does not meet the predicate', (value: any) => { + expect(() => new Birthday(value)).to.throw(`Birthday should be instance of Date`); + }); + }); +}); diff --git a/src/predicates/index.ts b/src/predicates/index.ts index 6a137b88..03f4434f 100644 --- a/src/predicates/index.ts +++ b/src/predicates/index.ts @@ -13,6 +13,7 @@ export * from './isLessThanOrEqualTo'; export * from './isNumber'; export * from './isOneOf'; export * from './isString'; +export * from './isInstanceOf'; export * from './or'; export * from './Predicate'; export * from './property'; diff --git a/src/predicates/isInstanceOf.ts b/src/predicates/isInstanceOf.ts new file mode 100644 index 00000000..e56c1e61 --- /dev/null +++ b/src/predicates/isInstanceOf.ts @@ -0,0 +1,21 @@ +import { Predicate } from './Predicate'; + +/** + * @desc Ensures that the `value` is an instance of `type` + * + * @example + * import { ensure, isInstanceOf, TinyType } from 'tiny-types'; + * + * class Birthday extends TinyType { + * constructor(public readonly value: Date) { + * ensure('Date', value, isInstanceOf(Date)); + * } + * } + * + * @param {T} value + * @param {new (...args: any[]): T} type + * @returns {Predicate} + */ +export function isInstanceOf(type: { new (...args: any[]): T }): Predicate { + return Predicate.to(`be instance of ${type.name}`, (value: T) => value instanceof type); +} From 4cee120bc94f09f8894c3db30b3c628aa0114ba6 Mon Sep 17 00:00:00 2001 From: Jan Molak Date: Mon, 8 Oct 2018 15:41:55 +0100 Subject: [PATCH 2/2] docs(predicates): Corrected the jsdoc for isInstance so that esdoc doesn't complain --- src/predicates/isInstanceOf.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/predicates/isInstanceOf.ts b/src/predicates/isInstanceOf.ts index e56c1e61..4b293603 100644 --- a/src/predicates/isInstanceOf.ts +++ b/src/predicates/isInstanceOf.ts @@ -12,8 +12,7 @@ import { Predicate } from './Predicate'; * } * } * - * @param {T} value - * @param {new (...args: any[]): T} type + * @param {Constructor} type * @returns {Predicate} */ export function isInstanceOf(type: { new (...args: any[]): T }): Predicate {