This library aims to perform various checks in the javascript environment.
checkif.js is distributed as a npm package.
npm install checkif.jsThe 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.
Checks if a given value is null.
Alias und(value)
Checks if a given value is undefined.
Checks if a given value is null or undefined.
Checks if a given value is NaN. Same as Number.isNaN.
is.nan(NaN); // true
is.nan(Number.NaN); // trueAlias arr(value)
Checks 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)); // trueAlias bool(value)
Checks if a given value is a boolean.
is.boolean(true); // true
is.boolean(new Boolean(0)); // trueAlias str(value)
Checks if a given value is a string.
is.string(''); // true
is.string(String('')); // true
is.string(new String('')); // trueChecks if a given value is a char.
is.char(' '); // true
is.char('1'); // true
is.char(1); // falseChecks if a given value is a date.
is.date(new Date('November 23, 1998 03:24:00')); // true, my birthdate btw ;)Alias num(value)
Checks if a given value is a number.
is.number(Number(1)); // true
is.number(new Number(1)); // true
is.number(1); // trueAlias reg(value)
Checks if a given value is a regular expression.
is.regexp(\a\); // true
is.regexp(RegExp()); // true
is.regexp(new RegExp()); // trueAlias obj(value)
Checks if a given value is an object.
is.object({}); // true
is.object(String(1)); // true
is.object('1'); // falseAlias pure(value)
Checks if a given value is a pure JSON object.
is.pureObject({}); // true
is.pureObject({ value: 1 }); // true
is.pureObject(new Date()); // falseAlias func(value)
Checks if a given value is a function.
is.function(function () { }); // true
is.function(x => x); // true
is.function(new Function('x', 'return x')); // trueAlias err(value)
Checks if a given value is an error.
is.error(Error('Fatal error')); // true
is.error(new Error('Nothing works anymore')); // trueAlias dom(value)
Checks 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); // trueAlias window(value)
Checks 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); // trueThis 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.
Check if a given string is lower case.
is.lowerCase('abc'); // true
is.lowerCase('abc 123'); // true
is.lowerCase('ABC'); // falseCheck if a given string is upper case.
is.upperCase('ABC'); // true
is.upperCase('ABC 123'); // true
is.upperCase('abc'); // falseThis group of methods allows to verify if a given number is a from a specific class of numbers.
Checks if a given value is even.
is.even(10); // true
is.even(11); // falseChecks if a given value is odd.
is.odd(9); // true
is.odd(10); // falseAlias int(value)
Checks if a given value is an integer.
is.integer(12); // true
is.integer(10.0); // true
is.integer(3.14); // false
is.integer(Number.MIN_VALUE); // false
is.integer(Infinity); // false
is.integer('6');// falseThis 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.
Checks if a given value is a Date object in the past.
is.past(new Date(0)); // true
let pastDate = new Date();
pastDate.setUTCFullYear(pastDate.getUTCFullYear() - 1);
is.past(pastDate); // true
let futureDate = new Date();
futureDate.setUTCFullYear(futureDate.getUTCFullYear() + 1);
is.past(futureDate); // falseChecks if a given value is a Date object in the future.
let futureDate = new Date();
futureDate.setUTCFullYear(futureDate.getUTCFullYear() + 1);
is.future(futureDate); // true
is.future(new Date(0)); // false
let pastDate = new Date();
pastDate.setUTCFullYear(pastDate.getUTCFullYear() - 1);
is.future(pastDate); // falseChecks if a given value is a Date object set today.
let date = new Date();
date.setUTCHours(12);
date.setUTCMinutes(30);
is.today(date); // true
is.today(new Date()); // true
is.today(new Date(0)); // false
let pastDate = new Date();
pastDate.setUTCFullYear(pastDate.getUTCFullYear() - 1);
is.today(pastDate); // false
let futureDate = new Date();
futureDate.setUTCFullYear(futureDate.getUTCFullYear() + 1);
is.today(futureDate); // falseall(enumerable, matcher, strict = false)
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); // falseall({ 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; }); // falseThe 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); // trueFeel 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)); // trueany(enumerable, matcher, strict = false)
This method verifies if any element in a given enumerable (array or object) matches a specific value or function.
import { any } from 'checkif.js';
any([0,1,2,3], 2); // true
any([0,1,2,3], x => x === 0); // true
any({ x : 1, y : 0}, x => x === 1); // true
any([0, 1, 2, 3], 1); // false
any({ x : 1, y : 1}, 2); // false
any([0,1,2,3], x => x === 5); //falseany also supports strict mode.
This group of methods allows to verify if a given enumerable (array or object) has an element verifying a specific condition. This checker contains the same methods as is. In fact, has.method(array) is equivalent to any(array, is.method).
import { any } from 'checkif.js';
has.uppercase(['abc', 'Abc', 'ABC']); // true
has.even({ x : 1, y : 2, z : 3}); // true
has.function({ x : function(){}}); // true
has.nan([0, 1, 2, 3]); // false
has.integer([1.2, 1.3]); // false
has.null([]); // falseThis group of methods allows to verify if a given enumerable (array or object) has only elements verifying a specific condition. This checker contains the same methods as is.
hasOnly.method(array) is equivalent to all(array, is.method).
import { any } from 'checkif.js';
hasOnly.lowercase(['abc', 'def', 'ghi']); // true
hasOnly.integer({ x : 1, y : 2, z : 3}); // true
hasOnly.function({ x : function(){}, y : x => x}); // true
hasOnly.null([null, 1, 2, 3]); // false
hasOnly.odd([1, 2, 3]); // falseatLeast(enumerable, count, matcher, strict = false)
Alias atl(enumerable, count, matcher, strict = false)
This method verifies if at least a number of elements in a given enumerable (array or object) match a specific value or function.
import { atLeast } from 'checkif.js';
atLeast([1,1,2,3], 2, 1); // true
atLeast([0,0,2,0], 3, x => x === 0); // true
atLeast({ x : 1, y : 0}, 1, x => x === 1); // true
atLeast([0, 1, 2, 3], 2, 1); // false
atLeast({ x : 1, y : 1}, 3, 2); // false
atLeast([0,1,2,3], 1, x => x === 5); //falseatLeast also supports strict mode.
atMost(enumerable, count, matcher, strict = false)
Alias atm(enumerable, count, matcher, strict = false)
This method verifies if at most a number of elements in a given enumerable (array or object) match a specific value or function.
import { atMost } from 'checkif.js';
atMost([0, 0, 1, 2], 2, 0); // true
atMost([true, true, true, false], 3, x => x === true); // true
atMost({ x: a, y: a }, 3, x => x === a); // true
atMost([0, 0, 1, 1], 1, 0); // false
atMost({ x: 1, y: 0 }, 0, 1); // false
let _function = function () { return true };
atMost({ x: _function, y: _function }, 1, _function); // falseatMost also supports strict mode.
hasAtLeast(count)
This group of methods allows to verify if a given enumerable (array or object) has at least a number of elements verifying a specific condition from is.
hasAtLeast(count).method(enumerable) is equivalent to atLeast(enumerable, count, is.method).
hasAtMost(count)
This group of methods allows to verify if a given enumerable (array or object) has at most a number of elements verifying a specific condition from is.
hasAtMost(count).method(enumerable) is equivalent to atMost(enumerable, count, is.method).
Keep in mind that using the has* notation is less performant than using the equivalent atLeast or atMost function. Everytime has*(count) is called, the resulting functions are regenerated from is.
Any help is wanted and welcome. You can check out our github issues and projects or our slack page.
Please follow our contributing guidelines.