Giter Club home page Giter Club logo

checkif.js's Introduction

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)

Check if a given value is null.

undefined(value)

Check if a given value is undefined.

nan(value)

Check if a given value is NaN. Same as Number.isNaN.

is.nan(NaN); // true
is.nan(Number.NaN); // true
array(value)

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
boolean(value)

Check if a given value is a boolean.

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

Check if a given value is a string.

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

Check if a given value is a char.

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

Check if a given value is a date.

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

Check if a given value is a number.

is.number(Number(1)); // true
is.number(new Number(1)); // true
is.number(1); // false
regexp(value)

Check if a given value is a regular expression.

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

Check if a given value is an object.

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

Check if a given value is a pure JSON object.

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

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
error(value)

Check if a given value is an error.

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

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
windowObject(value)

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

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. To be implemented

Arithemetic

This group of methods allows to verify if a given number is a from a specific class of numbers. To be implemented

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. To be implemented

all

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

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 on arrays

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 on objects

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

Strict mode

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

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.

checkif.js's People

Contributors

joeltankam avatar boristane avatar jvdoorn avatar codacy-badger avatar

Stargazers

Roman avatar

Watchers

Lucas Carl avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.