Skip to content

Jayasankar-m/checkif.js

 
 

Repository files navigation

checkif.js

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

Build Status Codacy Badge Codacy Badge npm version slack checkif.js

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); // true
is.nan(Number.NaN); // true
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([]); // true
is.array(new Array(0)); // true
boolean(value)

Alias bool(value)

Checks if a given value is a boolean.

is.boolean(true); // true
is.boolean(new Boolean(0)); // true
string(value)

Alias str(value)

Checks if a given value is a string.

is.string(''); // true
is.string(String('')); // true
is.string(new String('')); // true
char(value)

Checks if a given value is a char.

is.char(' '); // true
is.char('1'); // true
is.char(1); // false
date(value)

Checks if a given value is a date.

is.date(new Date('November 23, 1998 03:24:00')); // true, my birthdate btw ;)
number(value)

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); // true
regexp(value)

Alias reg(value)

Checks if a given value is a regular expression.

is.regexp(\a\); // true
is.regexp(RegExp()); // true
is.regexp(new RegExp()); // true
object(value)

Alias obj(value)

Checks if a given value is an object.

is.object({}); // true
is.object(String(1)); // true
is.object('1'); // false
pureObject(value)

Alias pure(value)

Checks if a given value is a pure JSON object.

is.pureObject({}); // true
is.pureObject({ value: 1 }); // true
is.pureObject(new Date()); // false
function(value)

Alias 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')); // true
error(value)

Alias err(value)

Checks if a given value is an error.

is.error(Error('Fatal error')); // true
is.error(new Error('Nothing works anymore')); // true
domNode(value)

Alias 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); // true
windowObject(value)

Alias 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); // true

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'); // true
is.lowerCase('abc 123'); // true
is.lowerCase('ABC'); // false
upperCase(value)

Check if a given string is upper case.

is.upperCase('ABC'); // true
is.upperCase('ABC 123'); // true
is.upperCase('abc'); // false

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); // true
is.even(11); // false
odd(value)

Checks if a given value is odd.

is.odd(9); // true
is.odd(10); // false
integer(value)

Alias 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');// false

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)); // 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); // false
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); // true

is.future(new Date(0)); // false

let pastDate = new Date();
pastDate.setUTCFullYear(pastDate.getUTCFullYear() - 1);
is.future(pastDate); // false
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); // 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); // false

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); // true
all([2, 4, 6, 8], x => x%2 === 0); // true

all([0, 1, 2, 3], 1); // false

all on objects

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

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); // 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

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); // 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); //false

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']); // 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([]); // false

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']); // 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]); // false

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); // 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); //false

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); // 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); // false

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.

About

Javascript check library

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%