Browse Source

Add symbol, undefined and null predicates (#28)

iss58
Sam Verschueren 7 years ago
committed by Sindre Sorhus
parent
commit
20d3ba7176
  1. 21
      source/index.ts
  2. 2
      source/lib/predicates/predicate.ts
  3. 11
      source/test/null.ts
  4. 8
      source/test/symbol.ts
  5. 13
      source/test/undefined.ts

21
source/index.ts

@ -20,6 +20,18 @@ export interface Ow {
* Test the value to be a boolean. * Test the value to be a boolean.
*/ */
boolean: BooleanPredicate; boolean: BooleanPredicate;
/**
* Test the value to be undefined.
*/
undefined: Predicate<undefined>;
/**
* Test the value to be null.
*/
null: Predicate<null>;
/**
* Test the value to be a Symbol.
*/
symbol: Predicate<Symbol>;
/** /**
* Test the value to be an array. * Test the value to be an array.
*/ */
@ -49,6 +61,15 @@ Object.defineProperties(main, {
boolean: { boolean: {
get: () => new BooleanPredicate() get: () => new BooleanPredicate()
}, },
undefined: {
get: () => new Predicate('undefined')
},
null: {
get: () => new Predicate('null')
},
symbol: {
get: () => new Predicate('symbol')
},
array: { array: {
get: () => new ArrayPredicate() get: () => new ArrayPredicate()
}, },

2
source/lib/predicates/predicate.ts

@ -12,7 +12,7 @@ export interface Context {
export const validatorSymbol = Symbol('validators'); export const validatorSymbol = Symbol('validators');
export abstract class Predicate<T = any> { export class Predicate<T = any> {
constructor( constructor(
type: string, type: string,
private context: Context = { private context: Context = {

11
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`');
});

8
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`');
});

13
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`');
});
Loading…
Cancel
Save