flk-supportive-is v0.2.3
Supportive Is
This is a very lightweight library to validate different things
Table of contents
- Supportive Is
- Table of contents
- Install
- Importing
- Dependencies
- Usage
- Is.null
- Is.undefined
- Is.numeric
- Is.int
- Is.float
- Is.NaN
- Is.regex
- Is.array
- Is.object
- Is.plainObject
- Is.validHtmlId
- Is.jquery
- Is.dom
- Is.formElement
- Is.formData
- Is.a
- Is.iterable
- Is.symbol
- Is.string
- Is.bool
- Is.function
- Is.callable
- Is.native
- Is.scalar
- Is.empty
- Is.json
- Is.url
- Is.email
- Is.cookieEnabled
- Is.mobile.*
- Is.desktop
- Is.browser
- Is.mobileNumber
Install
npm install flk-supportive-is
Importing
Node JS
const {Is} = require('flk-supportive-is');using import syntax
import Is from 'flk-supportive-is';Browser
<script src="dist/is.min.js"></script>Dependencies
No dependencies required to use this library
Usage
Is.null
Check if the given value is null
let name = null;
console.log(Is.null(name)); // true
name = undefined;
console.log(Is.null(name)); // falseIs.undefined
Check if the given value is undefined
let name = 'Hasan';
console.log(Is.undefined(name)); // false
let myVar; // any declared variable without a value will be treated as undefined
console.log(Is.undefined(myVar)); // true
let age = undefined;
console.log(Is.undefined(age)); // trueIs.numeric
Check if the given value is a number whatever if its data type is String or Number
let numberInt = 12;
console.log(Is.numeric(numberInt)); // true
let numberFloat = 12.55;
console.log(Is.numeric(numberFloat)); // true
let numberWrittenInString = '99';
console.log(Is.numeric(numberWrittenInString)); // true
let floatWrittenInString = '99.99';
console.log(Is.numeric(floatWrittenInString)); // trueIs.int
Check if the given value is an integer and its data type is number
let number = 12;
console.log(Is.int(numberInt)); // true
let numberInString = '12';
console.log(Is.int(numberInString)); // falseIs.float
Check if the given value is a float number and its data type is number
let number = 921;
console.log(Is.float(number)); // false
number = 82.42;
console.log(Is.float(number)); // true
let numberInString = '12.5';
console.log(Is.float(numberInString)); // falseIs.NaN
Check if the given value is Not a Number This method works exactly same as isNaN as it is already implementing it xD.
let number = 76; // number int
console.log(Is.NaN(number)); // false
number = '76'; // string
console.log(Is.NaN(number)); // trueIs.regex
Check if the given value is a regex expression.
let regex = /[a-z]+/;
console.log(Is.regex(regex)); // true
let string = '/[a-z]+/';
console.log(Is.regex(string)); // falseIs.array
Check if the given value is an array
let myArray = [4 , 'hello', 9];
console.log(Is.array(myArray)); // trueIs.object
Check if the given value is an object
Any type of objects will be validated true no matter its object type
Arrays are types of objects so any passed array will be validated as true
let myObject = {};
console.log(Is.object(myObject)); // true
class myClass {}
let anotherObject = new myClass;
console.log(Is.object(myObject)); // true
let myArray = [];
console.log(Is.object(myArray)); // true
// to check if the given value is an object but not an array
//you must mix between Is.object AND Is.array to avoid an array
if (Is.object(myVar) && ! Is.array(myVar)) {
// do something with that object
}Is.plainObject
Check if the given value is a plain object
Any type of objects will be validated as false unless it's a {} plain object.
let myObject = {};
console.log(Is.plainObject(myObject)); // true
class myClass {}
let anotherObject = new myClass;
console.log(Is.plainObject(myObject)); // false
let myArray = [];
console.log(Is.plainObject(myArray)); // falseIs.validHtmlId
Check if the given value is a valid html id attribute
let id = 'my-id';
console.log(Is.validHtmlId(id)); // true
let invalidId = 'my,id';
console.log(Is.validHtmlId(invalidId)); //falseIs.jquery
Check if the given value is a jquery object
let body = $('body');
console.log(Is.jquery(body)); // trueIs.dom
Check if the given value is a dom element
Dom Elements are objects of HTMLElement, so any html element will be validated as true, something like document.body
document and window are not validated as true as they are not part of the html elements
console.log(Is.dom(document)); // false
console.log(Is.dom(document.document)); // true
let myForm = document.getElementById('my-form');
console.log(Is.dom(document.body)); // trueIs.formElement
Check if the given value is an instance of HTMLFormElement.
let myForm = document.getElementById('my-form');
console.log(Is.formElement(myForm)); // true
console.log(Is.formElement(document.body)); // falseIs.formData
Check if the given value is an instance of FormData.
let myForm = document.getElementById('my-form');
let formDataForm = new FormData(myForm);
console.log(Is.formData(myForm)); // false
console.log(Is.formData(formDataForm)); // trueIs.a
Check if the given object value is an object of the given class name
class MyClass {}
let myObject = new MyClass;
console.log(Is.a(moyObject, 'MyClass')); // trueIs.iterable
Check if the given object is iterable
console.log(Is.iterable([])); // true
console.log(Is.iterable({})); // false
console.log(Is.iterable(new Map)); // true
console.log(Is.iterable(new Set)); // trueIs.symbol
Check if the given value is a Symbol
console.log(Is.symbol(Symbol())); // true
console.log(Is.symbol(Symbol('foo'))); // trueIs.string
Check if the given value is string
console.log(Is.string('this is a string')); // trueIs.bool
Is.boolhas an alias methodIs.boolean
Check if the given value is a boolean value.
console.log(Is.bool(true)); // true
console.log(Is.bool(false)); // trueIs.function
Check if the given value is a function
function sum(x, y) {
return x + y;
}
// you must pass the function name not the function call
console.log(Is.function(sum)); // true
// if you passed the function call it will be not a function in this situation
console.log(Is.function(sum(2, 3))); // falseIs.callable
Check if the given value is a callable value
function sum(x, y) {
return x + y;
}
console.log(Is.callable(sum)); // true
let callback = function () {
return 'hello';
}
console.log(Is.callable(callback)); // true
console.log(Is.callable(callback())); // falseIs.native
Check if the given value is a native function
let isArray = Array.isArray;
console.log(Is.native(isArray)); // true
console.log(Is.native(Math.min)); // true
Math.sum = function (num1, num2) {
return num1 + num2;
};
console.log(Is.native(Math.sum)); // falseIs.scalar
Check if the given value is a string, number or boolean
console.log(Is.scalar('hello')); // true
console.log(Is.scalar(22.5)); // true
console.log(Is.scalar(false)); // true
console.log(Is.scalar(null)); // false
console.log(Is.scalar(undefined)); // false
console.log(Is.scalar([])); // false
console.log(Is.scalar({})); // falseIs.empty
Check if the given value is empty
This is a kind of smart method that will validate the given value whether it is empty or not based on its type
// undefined values are considered empty
let value = undefined;
console.log(Is.empty(value)); // true
// null values are considered empty
value = null;
console.log(Is.empty(value)); // true
// Also any objects with no values are considered empty
value = {};
console.log(Is.empty(value)); // true
value.name = 'Hasan';
console.log(Is.empty(value)); // false
// Arrays
value = [];
console.log(Is.empty(value)); // true
value.push(12);
console.log(Is.empty(value)); // false
// The `Zero` is not considered as empty value
value = 0;
console.log(Is.empty(value)); // falseIs.json
Check if the given value string is a valid json format
let value = '{"name":"Hasan","job":"Full Stack Developer"}';
console.log(Is.json(value)); // trueIs.url
Check if the given value is a valid url
let url = 'google.com';
console.log(Is.url(url)); // true
url = 'https://google.com';
console.log(Is.url(url)); // true
url = 'www.google.com';
console.log(Is.url(url)); // true
url = 'www.google.com?q=hello+world';
console.log(Is.url(url)); // true
let url = 'google';
console.log(Is.url(url)); // falseIs.email
Check if the given value string is a valid email
let myEmail = 'hassanzohdy@gmail.com';
console.log(Is.email(myEmail)); // trueIs.cookieEnabled
Check if cookies are enabled in the browser
if (! Is.cookeEnabled()) {
// Oops!, cookies are not enabled!
}Is.mobile.*
Check if current visitor is browsing from a sort-of mobile
this property contains set of methods
// To check if user is browsing from an android device
if (Is.mobile.andriod()) {
// do something
}
// To check if user is browsing from a blackBerry mobile
if (Is.mobile.blackBerry()) {
// do something
}
// To check if user is browsing from an ios device
if (Is.mobile.ios()) {
// do something
}
// To check if user is browsing from an ios device
if (Is.mobile.ios()) {
// do something
}
// To check if user is browsing from an iphone
if (Is.mobile.iphone()) {
// do something
}
// To check if user is browsing from an ipad
if (Is.mobile.ipad()) {
// do something
}
// To check if user is browsing from an ipod
if (Is.mobile.ipod()) {
// do something
}
// To check if user is browsing from a windows mobile
if (Is.mobile.windows()) {
// do something
}
// To check if user is browsing from any type of mobile
if (Is.mobile.any()) {
// do something
}Is.desktop
Check if current visitor is browsing from a desktop device
Please note that any non mobile type will be considered as desktop.
if (! Is.cookeEnabled()) {
// Oops!, cookies are not enabled!
}Is.browser
Check if the current browser matches the given name
if (Is.browser('chrome')) {
// do something
} else if (Is.browser('firefox')) {
// do something
} else if (Is.browser('opera')) {
// do something
} else if (Is.browser('safari')) {
// do something
} else if (Is.browser('ie')) {
// do something
} else if (Is.browser('edge')) {
// do something
}Is.mobileNumber
Works only with Egypt mobile numbers, feel free to
PRyour country mobile number validation.
.eg
Validate mobile number for the following syntaxes:
01[0|1|2|5]-8(digits)+201[0|1|2|5]-8(digits)201[0|1|2|5]-8(digits)
let withCode = true; // to bypass mobile numbers starting with country code
console.log(Is.mobileNumber.eg('01002223344')); // true
console.log(Is.mobileNumber.eg('+201002223344', withCode)); // true
console.log(Is.mobileNumber.eg('201002223344', withCode)); // true