checkif.js
This library aims to perform various checks in the javascript environment.

Installation
checkif.js is distributed as a npm package.
npm install checkif.js
Documentation
The library is constituted of a set of checkers to perform different verifications.
is
import { is } from 'checkif.js';
Types
This group of methods allows to verify if a given value is from a specific type.
null(value)
Checks if a given value is null.
undefined(value)
Alias und(value)
Checks if a given value is undefined.
nullable(value)
Checks if a given value is null or undefined.
nan(value)
Checks if a given value is NaN. Same as Number.isNaN.
is.nan(NaN);
is.nan(Number.NaN);
array(value)
Alias arr(value)
Checks if a given value is an array. This method is the same as Array.isArray, if available.
is.array([]);
is.array(new Array(0));
boolean(value)
Alias bool(value)
Checks if a given value is a boolean.
is.boolean(true);
is.boolean(new Boolean(0));
string(value)
Alias str(value)
Checks if a given value is a string.
is.string('');
is.string(String(''));
is.string(new String(''));
char(value)
Checks if a given value is a char.
is.char(' ');
is.char('1');
is.char(1);
date(value)
Checks if a given value is a date.
is.date(new Date('November 23, 1998 03:24:00'));
number(value)
Alias num(value)
Checks if a given value is a number.
is.number(Number(1));
is.number(new Number(1));
is.number(1);
regexp(value)
Alias reg(value)
Checks if a given value is a regular expression.
is.regexp(\a\);
is.regexp(RegExp());
is.regexp(new RegExp());
object(value)
Alias obj(value)
Checks if a given value is an object.
is.object({});
is.object(String(1));
is.object('1');
pureObject(value)
Alias pure(value)
Checks if a given value is a pure JSON object.
is.pureObject({});
is.pureObject({ value: 1 });
is.pureObject(new Date());
function(value)
Alias func(value)
Checks if a given value is a function.
is.function(function () { });
is.function(x => x);
is.function(new Function('x', 'return x'));
error(value)
Alias err(value)
Checks if a given value is an error.
is.error(Error('Fatal error'));
is.error(new Error('Nothing works anymore'));
domNode(value)
Alias dom(value)
Checks if a given value is a DOM node.
is.domNode(window.document.body);
let dom = new JSDOM(`<html !DOCTYPE><body></body></html>`);
is.domNode(dom.window.document.body);
windowObject(value)
Alias window(value)
Checks if a given value is a window object.
is.windowObject(window);
let dom = new JSDOM(`<html !DOCTYPE></html>`)
is.windowObject(dom.window);
RegExp
This group of methods allows to verify if a given string is from a specific known type of value.
To be implemented
String
This group of methods allows to verify if a given string is a from a specific format.
lowerCase(value)
Check if a given string is lower case.
is.lowerCase('abc');
is.lowerCase('abc 123');
is.lowerCase('ABC');
upperCase(value)
Check if a given string is upper case.
is.upperCase('ABC');
is.upperCase('ABC 123');
is.upperCase('abc');
Arithmetic
This group of methods allows to verify if a given number is a from a specific class of numbers.
even(value)
Checks if a given value is even.
is.even(10);
is.even(11);
odd(value)
Checks if a given value is odd.
is.odd(9);
is.odd(10);
integer(value)
Alias int(value)
Checks if a given value is an integer.
is.integer(12);
is.integer(10.0);
is.integer(3.14);
is.integer(Number.MIN_VALUE);
is.integer(Infinity);
is.integer('6');
Environment
This group of methods allows to verify if the current environment is a specific environment.
To be implemented
Time
This group of methods allows to verify if a date is a specific kind.
past(value)
Checks if a given value is a Date object in the past.
is.past(new Date(0));
let pastDate = new Date();
pastDate.setUTCFullYear(pastDate.getUTCFullYear() - 1);
is.past(pastDate);
let futureDate = new Date();
futureDate.setUTCFullYear(futureDate.getUTCFullYear() + 1);
is.past(futureDate);
future(value)
Checks if a given value is a Date object in the future.
let futureDate = new Date();
futureDate.setUTCFullYear(futureDate.getUTCFullYear() + 1);
is.future(futureDate);
is.future(new Date(0));
let pastDate = new Date();
pastDate.setUTCFullYear(pastDate.getUTCFullYear() - 1);
is.future(pastDate);
today(value)
Checks if a given value is a Date object set today.
let date = new Date();
date.setUTCHours(12);
date.setUTCMinutes(30);
is.today(date);
is.today(new Date());
is.today(new Date(0));
let pastDate = new Date();
pastDate.setUTCFullYear(pastDate.getUTCFullYear() - 1);
is.today(pastDate);
let futureDate = new Date();
futureDate.setUTCFullYear(futureDate.getUTCFullYear() + 1);
is.today(futureDate);
all
all(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 on arrays
all([0, 0, 0, 0], 0);
all([2, 4, 6, 8], x => x%2 === 0);
all([0, 1, 2, 3], 1);
all on objects
all({ x: 1, y: 1 }, 1);
let a = {};
all({ x: a, y: a }, x => x === a);
all({ x: 0, y: new Number(0) }, function(x){ return x === 0; });
Strict mode
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);
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));
any
any(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);
any([0,1,2,3], x => x === 0);
any({ x : 1, y : 0}, x => x === 1);
any([0, 1, 2, 3], 1);
any({ x : 1, y : 1}, 2);
any([0,1,2,3], x => x === 5);
any also supports strict mode.
has
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']);
has.even({ x : 1, y : 2, z : 3});
has.function({ x : function(){}});
has.nan([0, 1, 2, 3]);
has.integer([1.2, 1.3]);
has.null([]);
hasOnly
This 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']);
hasOnly.integer({ x : 1, y : 2, z : 3});
hasOnly.function({ x : function(){}, y : x => x});
hasOnly.null([null, 1, 2, 3]);
hasOnly.odd([1, 2, 3]);
atLeast
atLeast(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);
atLeast([0,0,2,0], 3, x => x === 0);
atLeast({ x : 1, y : 0}, 1, x => x === 1);
atLeast([0, 1, 2, 3], 2, 1);
atLeast({ x : 1, y : 1}, 3, 2);
atLeast([0,1,2,3], 1, x => x === 5);
atLeast also supports strict mode.
atMost
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);
atMost([true, true, true, false], 3, x => x === true);
atMost({ x: a, y: a }, 3, x => x === a);
atMost([0, 0, 1, 1], 1, 0);
atMost({ x: 1, y: 0 }, 0, 1);
let _function = function () { return true };
atMost({ x: _function, y: _function }, 1, _function);
atMost also supports strict mode.
hasAtLeast
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
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.
Contributing
Any help is wanted and welcome. You can check out our github issues and projects or our slack page.
Please follow our contributing guidelines.
评论