Browse Source

Add dataView, NaN, and nullOrUndefined predicates (#38)

iss58
Sam Verschueren 7 years ago
committed by Sindre Sorhus
parent
commit
a7b775471b
  1. 2
      package.json
  2. 21
      source/index.ts
  3. 8
      source/test/data-view.ts
  4. 10
      source/test/nan.ts
  5. 13
      source/test/null-or-undefined.ts

2
package.json

@ -47,7 +47,7 @@
"object"
],
"devDependencies": {
"@sindresorhus/is": "^0.6.0",
"@sindresorhus/is": "^0.7.0",
"@types/highlight.js": "^9.12.2",
"@types/node": "^8.0.31",
"add-module-exports-webpack-plugin": "^0.1.0",

21
source/index.ts

@ -36,6 +36,14 @@ export interface Ow {
* Test the value to be null.
*/
readonly null: Predicate<null>;
/**
* Test the value to be null or undefined.
*/
readonly nullOrUndefined: Predicate<null | undefined>;
/**
* Test the value to be not a number.
*/
readonly nan: Predicate<number>;
/**
* Test the value to be a Symbol.
*/
@ -120,6 +128,10 @@ export interface Ow {
* Test the value to be a ArrayBuffer.
*/
readonly arrayBuffer: Predicate<ArrayBuffer>;
/**
* Test the value to be a DataView.
*/
readonly dataView: Predicate<DataView>;
/**
* Test the value to be Iterable.
*/
@ -151,6 +163,12 @@ Object.defineProperties(main, {
null: {
get: () => new Predicate('null')
},
nullOrUndefined: {
get: () => new Predicate('nullOrUndefined')
},
nan: {
get: () => new Predicate('nan')
},
symbol: {
get: () => new Predicate('symbol')
},
@ -214,6 +232,9 @@ Object.defineProperties(main, {
arrayBuffer: {
get: () => new Predicate('arrayBuffer')
},
dataView: {
get: () => new Predicate('dataView')
},
iterable: {
get: () => new Predicate('iterable')
}

8
source/test/data-view.ts

@ -0,0 +1,8 @@
import test from 'ava';
import m from '..';
test('dataView', t => {
t.notThrows(() => m(new DataView(new ArrayBuffer(1)), m.dataView));
t.throws(() => m(new ArrayBuffer(1) as any, m.dataView), 'Expected argument to be of type `dataView` but received type `ArrayBuffer`');
t.throws(() => m(12 as any, m.dataView), 'Expected argument to be of type `dataView` but received type `number`');
});

10
source/test/nan.ts

@ -0,0 +1,10 @@
import test from 'ava';
import m from '..';
test('nan', t => {
t.notThrows(() => m(NaN, m.nan));
t.notThrows(() => m(Number.NaN, m.nan));
t.notThrows(() => m(0 / 0, m.nan));
t.throws(() => m(12, m.nan), 'Expected argument to be of type `nan` but received type `number`');
t.throws(() => m('12' as any, m.nan), 'Expected argument to be of type `nan` but received type `string`');
});

13
source/test/null-or-undefined.ts

@ -0,0 +1,13 @@
import test from 'ava';
import m from '..';
test('nullOrUndefined', t => {
const x = null;
const y = undefined;
t.notThrows(() => m(null, m.nullOrUndefined));
t.notThrows(() => m(undefined, m.nullOrUndefined));
t.notThrows(() => m(x, m.nullOrUndefined));
t.notThrows(() => m(y, m.nullOrUndefined));
t.throws(() => m('foo' as any, m.nullOrUndefined), 'Expected argument to be of type `nullOrUndefined` but received type `string`');
});
Loading…
Cancel
Save