diff --git a/source/index.ts b/source/index.ts index fe055c6..a84a969 100644 --- a/source/index.ts +++ b/source/index.ts @@ -7,6 +7,8 @@ import {ArrayPredicate} from './lib/predicates/array'; import {DatePredicate} from './lib/predicates/date'; import {ErrorPredicate} from './lib/predicates/error'; +export type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array; + export interface Ow { (value: any, predicate: Predicate): void; /** @@ -45,6 +47,70 @@ export interface Ow { * Test the value to be an Error. */ error: ErrorPredicate; + /** + * Test the value to be a Function. + */ + function: Predicate; + /** + * Test the value to be a Buffer. + */ + buffer: Predicate; + /** + * Test the value to be a RegExp. + */ + regExp: Predicate; + /** + * Test the value to be a Promise. + */ + promise: Predicate>; + /** + * Test the value to be a typed array. + */ + typedArray: Predicate; + /** + * Test the value to be a Int8Array. + */ + int8Array: Predicate; + /** + * Test the value to be a Uint8Array. + */ + uint8Array: Predicate; + /** + * Test the value to be a Uint8ClampedArray. + */ + uint8ClampedArray: Predicate; + /** + * Test the value to be a Int16Array. + */ + int16Array: Predicate; + /** + * Test the value to be a Uint16Array. + */ + uint16Array: Predicate; + /** + * Test the value to be a Int32Array. + */ + int32Array: Predicate; + /** + * Test the value to be a Uint32Array. + */ + uint32Array: Predicate; + /** + * Test the value to be a Int32Array. + */ + float32Array: Predicate; + /** + * Test the value to be a Uint64Array. + */ + float64Array: Predicate; + /** + * Test the value to be a ArrayBuffer. + */ + arrayBuffer: Predicate; + /** + * Test the value to be Iterable. + */ + iterable: Predicate>; } const main = (value: any, predicate: Predicate) => { @@ -83,6 +149,54 @@ Object.defineProperties(main, { }, error: { get: () => new ErrorPredicate() + }, + function: { + get: () => new Predicate('function') + }, + buffer: { + get: () => new Predicate('buffer') + }, + regExp: { + get: () => new Predicate('regExp') + }, + promise: { + get: () => new Predicate('promise') + }, + typedArray: { + get: () => new Predicate('typedArray') + }, + int8Array: { + get: () => new Predicate('int8Array') + }, + uint8Array: { + get: () => new Predicate('uint8Array') + }, + uint8ClampedArray: { + get: () => new Predicate('uint8ClampedArray') + }, + int16Array: { + get: () => new Predicate('int16Array') + }, + uint16Array: { + get: () => new Predicate('uint16Array') + }, + int32Array: { + get: () => new Predicate('int32Array') + }, + uint32Array: { + get: () => new Predicate('uint32Array') + }, + float32Array: { + get: () => new Predicate('float32Array') + }, + float64Array: { + get: () => new Predicate('float64Array') + }, + arrayBuffer: { + get: () => new Predicate('arrayBuffer') + }, + iterable: { + get: () => new Predicate('iterable') } }); diff --git a/source/test/array-buffer.ts b/source/test/array-buffer.ts new file mode 100644 index 0000000..b524293 --- /dev/null +++ b/source/test/array-buffer.ts @@ -0,0 +1,8 @@ +import test from 'ava'; +import m from '..'; + +test('arrayBuffer', t => { + t.notThrows(() => m(new ArrayBuffer(1), m.arrayBuffer)); + t.throws(() => m('foo', m.arrayBuffer), 'Expected argument to be of type `arrayBuffer` but received type `string`'); + t.throws(() => m(12, m.arrayBuffer), 'Expected argument to be of type `arrayBuffer` but received type `number`'); +}); diff --git a/source/test/buffer.ts b/source/test/buffer.ts new file mode 100644 index 0000000..b165eda --- /dev/null +++ b/source/test/buffer.ts @@ -0,0 +1,9 @@ +import test from 'ava'; +import m from '..'; + +test('buffer', t => { + t.notThrows(() => m(Buffer.alloc(2), m.buffer)); + t.notThrows(() => m(Buffer.from('f'), m.buffer)); + t.throws(() => m('foo', m.buffer), 'Expected argument to be of type `buffer` but received type `string`'); + t.throws(() => m(12, m.buffer), 'Expected argument to be of type `buffer` but received type `number`'); +}); diff --git a/source/test/function.ts b/source/test/function.ts new file mode 100644 index 0000000..e66f2c3 --- /dev/null +++ b/source/test/function.ts @@ -0,0 +1,8 @@ +import test from 'ava'; +import m from '..'; + +test('function', t => { + t.notThrows(() => m(() => {}, m.function)); // tslint:disable-line:no-empty + t.throws(() => m('foo', m.function), 'Expected argument to be of type `function` but received type `string`'); + t.throws(() => m(12, m.function), 'Expected argument to be of type `function` but received type `number`'); +}); diff --git a/source/test/iterable.ts b/source/test/iterable.ts new file mode 100644 index 0000000..e48b448 --- /dev/null +++ b/source/test/iterable.ts @@ -0,0 +1,9 @@ +import test from 'ava'; +import m from '..'; + +test('iterable', t => { + t.notThrows(() => m([], m.iterable)); + t.notThrows(() => m('foo', m.iterable)); + t.notThrows(() => m(new Map(), m.iterable)); + t.throws(() => m(12, m.iterable), 'Expected argument to be of type `iterable` but received type `number`'); +}); diff --git a/source/test/promise.ts b/source/test/promise.ts new file mode 100644 index 0000000..9de3440 --- /dev/null +++ b/source/test/promise.ts @@ -0,0 +1,9 @@ +import test from 'ava'; +import m from '..'; + +test('promise', t => { + t.notThrows(() => m(Promise.resolve(), m.promise)); + t.notThrows(() => m(new Promise(resolve => resolve()), m.promise)); + t.throws(() => m('foo', m.promise), 'Expected argument to be of type `promise` but received type `string`'); + t.throws(() => m(12, m.promise), 'Expected argument to be of type `promise` but received type `number`'); +}); diff --git a/source/test/regexp.ts b/source/test/regexp.ts new file mode 100644 index 0000000..afec2ec --- /dev/null +++ b/source/test/regexp.ts @@ -0,0 +1,9 @@ +import test from 'ava'; +import m from '..'; + +test('regExp', t => { + t.notThrows(() => m(/\d/, m.regExp)); + t.notThrows(() => m(new RegExp('\d'), m.regExp)); + t.throws(() => m('foo', m.regExp), 'Expected argument to be of type `regExp` but received type `string`'); + t.throws(() => m(12, m.regExp), 'Expected argument to be of type `regExp` but received type `number`'); +}); diff --git a/source/test/typed-array.ts b/source/test/typed-array.ts new file mode 100644 index 0000000..0dd5178 --- /dev/null +++ b/source/test/typed-array.ts @@ -0,0 +1,65 @@ +import test from 'ava'; +import m from '..'; + +test('typedArray', t => { + t.notThrows(() => m(new Int8Array(2), m.typedArray)); + t.notThrows(() => m(new Uint8Array(2), m.typedArray)); + t.notThrows(() => m(new Int32Array(2), m.typedArray)); + t.notThrows(() => m(new Float64Array(2), m.typedArray)); + t.throws(() => m('foo', m.typedArray), 'Expected argument to be of type `typedArray` but received type `string`'); + t.throws(() => m(12, m.typedArray), 'Expected argument to be of type `typedArray` but received type `number`'); +}); + +test('int8Array', t => { + t.notThrows(() => m(new Int8Array(2), m.int8Array)); + t.throws(() => m('foo', m.int8Array), 'Expected argument to be of type `int8Array` but received type `string`'); + t.throws(() => m(12, m.int8Array), 'Expected argument to be of type `int8Array` but received type `number`'); +}); + +test('uint8Array', t => { + t.notThrows(() => m(new Uint8Array(2), m.uint8Array)); + t.throws(() => m('foo', m.uint8Array), 'Expected argument to be of type `uint8Array` but received type `string`'); + t.throws(() => m(12, m.uint8Array), 'Expected argument to be of type `uint8Array` but received type `number`'); +}); + +test('uint8ClampedArray', t => { + t.notThrows(() => m(new Uint8ClampedArray(2), m.uint8ClampedArray)); + t.throws(() => m('foo', m.uint8ClampedArray), 'Expected argument to be of type `uint8ClampedArray` but received type `string`'); + t.throws(() => m(12, m.uint8ClampedArray), 'Expected argument to be of type `uint8ClampedArray` but received type `number`'); +}); + +test('int16Array', t => { + t.notThrows(() => m(new Int16Array(2), m.int16Array)); + t.throws(() => m('foo', m.int16Array), 'Expected argument to be of type `int16Array` but received type `string`'); + t.throws(() => m(12, m.int16Array), 'Expected argument to be of type `int16Array` but received type `number`'); +}); + +test('uint16Array', t => { + t.notThrows(() => m(new Uint16Array(2), m.uint16Array)); + t.throws(() => m('foo', m.uint16Array), 'Expected argument to be of type `uint16Array` but received type `string`'); + t.throws(() => m(12, m.uint16Array), 'Expected argument to be of type `uint16Array` but received type `number`'); +}); + +test('int32Array', t => { + t.notThrows(() => m(new Int32Array(2), m.int32Array)); + t.throws(() => m('foo', m.int32Array), 'Expected argument to be of type `int32Array` but received type `string`'); + t.throws(() => m(12, m.int32Array), 'Expected argument to be of type `int32Array` but received type `number`'); +}); + +test('uint32Array', t => { + t.notThrows(() => m(new Uint32Array(2), m.uint32Array)); + t.throws(() => m('foo', m.uint32Array), 'Expected argument to be of type `uint32Array` but received type `string`'); + t.throws(() => m(12, m.uint32Array), 'Expected argument to be of type `uint32Array` but received type `number`'); +}); + +test('float32Array', t => { + t.notThrows(() => m(new Float32Array(2), m.float32Array)); + t.throws(() => m('foo', m.float32Array), 'Expected argument to be of type `float32Array` but received type `string`'); + t.throws(() => m(12, m.float32Array), 'Expected argument to be of type `float32Array` but received type `number`'); +}); + +test('float64Array', t => { + t.notThrows(() => m(new Float64Array(2), m.float64Array)); + t.throws(() => m('foo', m.float64Array), 'Expected argument to be of type `float64Array` but received type `string`'); + t.throws(() => m(12, m.float64Array), 'Expected argument to be of type `float64Array` but received type `number`'); +});