This library aims to perform various checks in the javascript environment.
checkif.js
is distributed as a npm package.
npm install checkif.js
The library is constituted of a set of checkers to perform different verifications.
import { is } from 'checkif.js';
This group of methods allows to verify if a given value is from a specific type.
Check if a given value is null
.
Check if a given value is undefined
.
Check if a given value is NaN
. Same as Number.isNaN
.
is.nan(NaN); // true
is.nan(Number.NaN); // true
Check if a given value is an array. This method is the same as Array.isArray
, if available.
is.array([]); // true
is.array(new Array(0)); // true
Check if a given value is a boolean.
is.boolean(true); // true
is.boolean(new Boolean(0)); // true
Check if a given value is a string.
is.string(''); // true
is.string(String('')); // true
is.string(new String('')); // true
Check if a given value is a char.
is.char(' '); // true
is.char('1'); // true
is.char(1); // false
Check if a given value is a date.
is.date(new Date('November 23, 1998 03:24:00')); // true, my birthdate btw ;)
Check if a given value is a number.
is.number(Number(1)); // true
is.number(new Number(1)); // true
is.number(1); // false
Check if a given value is a regular expression.
is.regexp(\a\); // true
is.regexp(RegExp()); // true
is.regexp(new RegExp()); // true
Check if a given value is an object.
is.object({}); // true
is.object(String(1)); // true
is.object('1'); // false
Check if a given value is a pure JSON object.
is.pureObject({}); // true
is.pureObject({ value: 1 }); // true
is.pureObject(new Date()); // false
Check if a given value is a function.
is.function(function () { }); // true
is.function(x => x); // true
is.function(new Function('x', 'return x')); // true
Check if a given value is an error.
is.error(Error('Fatal error')); // true
is.error(new Error('Nothing works anymore')); // true
Check if a given value is a DOM node.
// Browser
is.domNode(window.document.body); // true
// Node.js
let dom = new JSDOM(`<html !DOCTYPE><body></body></html>`);
is.domNode(dom.window.document.body); // true
Check if a given value is a window object.
// Browser
is.windowObject(window); // true
// Node.js
let dom = new JSDOM(`<html !DOCTYPE></html>`)
is.windowObject(dom.window); // true
This group of methods allows to verify if a given string is from a specific known type of value. To be implemented
This group of methods allows to verify if a given string is a from a specific format. To be implemented
This group of methods allows to verify if a given number is a from a specific class of numbers. To be implemented
This group of methods allows to verify if the current environment is a specific environment. To be implemented
This group of methods allows to verify if a date is a specific kind. To be implemented
import { all } from 'checkif.js';
This method verifies if all elements in a given enumerable (array or object) match a specific value or function.
all([0, 0, 0, 0], 0); // true
all([2, 4, 6, 8], x => x%2 === 0); // true
all([0, 1, 2, 3], 1); // false
all({ x: 1, y: 1 }, 1); // true
let a = {};
all({ x: a, y: a }, x => x === a); // true
all({ x: 0, y: new Number(0) }, function(x){ return x === 0; }); // false
The second parameter of all()
is automatically resolved as a matcher function when it's a function. But you may want to check if the values are exactly equal to the function (as a value). In this case, you should use the strict mode by setting the 3rd parameter to true
(default false
).
import { all } from 'checkif.js';
let _function = function (x) { ... };
all({ x: _function, y: _function }, _function, true); // true
Feel free to build complex logic for your checks.
import { all, is } from 'checkif.js';
all({ x: ['a', 'b'], y: ['a', 'c'] }, x => all(x, is.char)); // true
import { all } from 'checkif.js';
This method verifies if any element in a given enumerable (array or object) match a specific value or function.
any([0,1,2,3], 2); // true
any([0,1,2,3], x => x === 0); // true
all([0, 1, 2, 3], 1); // false
any([0,1,2,3], x => x === 5); //false
any({ x : 1, y : 1}, 1); // true
any({ x : 1, y : 1}, x => x === 1); // true
any({ x : 1, y : 1}, 2); // false
any({ x : 1, y : 1}, x => x === 2); // false
The second parameter of any()
is automatically resolved as a matcher function when it's a function. But you may want to check if the values are exactly equal to the function (as a value). In this case, you should use the strict mode by setting the 3rd parameter to true
(default false
).
import { any } from 'checkif.js';
let _function = function (x) { ... };
any({ x: 0, y: _function }, _function, true); // true
Any help is wanted and welcome. You can check out our github issues and projects or our slack page.
Please follow our contributing guidelines.