diff --git a/source/index.ts b/source/index.ts index 748f6db..8fd6fc1 100644 --- a/source/index.ts +++ b/source/index.ts @@ -20,6 +20,18 @@ export interface Ow { * Test the value to be a boolean. */ boolean: BooleanPredicate; + /** + * Test the value to be undefined. + */ + undefined: Predicate; + /** + * Test the value to be null. + */ + null: Predicate; + /** + * Test the value to be a Symbol. + */ + symbol: Predicate; /** * Test the value to be an array. */ @@ -49,6 +61,15 @@ Object.defineProperties(main, { boolean: { get: () => new BooleanPredicate() }, + undefined: { + get: () => new Predicate('undefined') + }, + null: { + get: () => new Predicate('null') + }, + symbol: { + get: () => new Predicate('symbol') + }, array: { get: () => new ArrayPredicate() }, diff --git a/source/lib/predicates/predicate.ts b/source/lib/predicates/predicate.ts index 2aafcec..8712284 100644 --- a/source/lib/predicates/predicate.ts +++ b/source/lib/predicates/predicate.ts @@ -12,7 +12,7 @@ export interface Context { export const validatorSymbol = Symbol('validators'); -export abstract class Predicate { +export class Predicate { constructor( type: string, private context: Context = { diff --git a/source/test/null.ts b/source/test/null.ts new file mode 100644 index 0000000..eb0cc01 --- /dev/null +++ b/source/test/null.ts @@ -0,0 +1,11 @@ +import test from 'ava'; +import m from '..'; + +test('undefined', t => { + const x = null; + + t.notThrows(() => m(null, m.null)); + t.notThrows(() => m(x, m.null)); + t.throws(() => m(undefined, m.null), 'Expected argument to be of type `null` but received type `undefined`'); + t.throws(() => m('foo', m.null), 'Expected argument to be of type `null` but received type `string`'); +}); diff --git a/source/test/symbol.ts b/source/test/symbol.ts new file mode 100644 index 0000000..ada7164 --- /dev/null +++ b/source/test/symbol.ts @@ -0,0 +1,8 @@ +import test from 'ava'; +import m from '..'; + +test('symbol', t => { + t.notThrows(() => m(Symbol.iterator, m.symbol)); + t.notThrows(() => m(Symbol('foo'), m.symbol)); + t.throws(() => m(12, m.symbol), 'Expected argument to be of type `symbol` but received type `number`'); +}); diff --git a/source/test/undefined.ts b/source/test/undefined.ts new file mode 100644 index 0000000..9b99812 --- /dev/null +++ b/source/test/undefined.ts @@ -0,0 +1,13 @@ +import test from 'ava'; +import m from '..'; + +test('undefined', t => { + let x; // tslint:disable-line:prefer-const + const y = 12; + + t.notThrows(() => m(undefined, m.undefined)); + t.notThrows(() => m(x, m.undefined)); + t.throws(() => m(y, m.undefined), 'Expected argument to be of type `undefined` but received type `number`'); + t.throws(() => m(null, m.undefined), 'Expected argument to be of type `undefined` but received type `null`'); + t.throws(() => m('foo', m.undefined), 'Expected argument to be of type `undefined` but received type `string`'); +});