Package Exports
- @mongez/supportive-is
- @mongez/supportive-is/cjs/index.js
- @mongez/supportive-is/esm/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@mongez/supportive-is) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Supportive Is
A very lightweight library to validate different things
Installation
npm i @mongez/supportive-is
Or using Yarn
yarn add @mongez/supportive-is
Usage
There are many data types here to be checked, but firstly let's import the package
import Is from '@mongez/supportive-is';
// use itPrimitives
We've tons of primitives to be checked, here are some of them.
Is.primitive
Check if the given value is a string, number, boolean, null, undefined or symbol
console.log(Is.primitive('hello')); // true
console.log(Is.primitive(22.5)); // true
console.log(Is.primitive(false)); // true
console.log(Is.primitive(null)); // true
console.log(Is.primitive(Symbol('SymbolKey'))); // true
console.log(Is.primitive(undefined)); // true
console.log(Is.primitive([])); // false
console.log(Is.primitive({})); // 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.json
Check if the given value string is a valid json format
let value = '{"name":"Hasan","job":"Software Engineer"}';
console.log(Is.json(value)); // trueIs.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 whether 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.number
Check if the given value is a number whether if its data type is Number
let numberInt = 12;
console.log(Is.number(numberInt)); // true
let numberFloat = 12.55;
console.log(Is.number(numberFloat)); // true
let numberWrittenInString = '99';
console.log(Is.number(numberWrittenInString)); // false
let floatWrittenInString = '99.99';
console.log(Is.number(floatWrittenInString)); // falseThe major difference between
Is.numericandIs.numberis thatIs.numericwill return true if the given value is a string that contains a number
Is.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)); // true
number = 82.42;
console.log(Is.float(number)); // true
let numberInString = '12';
console.log(Is.float(numberInString)); // falseIs.string
Check if the given value is string
console.log(Is.string('this is a string')); // trueIs.bool
Check if the given value is boolean
console.log(Is.bool(true)); // true
console.log(Is.bool(false)); // trueIs.boolean is an alias for Is.bool
Is.symbol
Check if the given value is a symbol
let mySymbol = Symbol('mySymbol');
console.log(Is.symbol(mySymbol)); // trueIs.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 regular expression
let regex = /hello/;
console.log(Is.regex(regex)); // true
let regexString = '/hello/';
console.log(Is.regex(regexString)); // false
let regexObject = new RegExp('hello');
console.log(Is.regex(regexObject)); // trueObjects, Classes And Arrays
Now let's check some objects.
Is.object
Check if the given value is an object
Any type of objects will be validated as true no matter its object type
Arrays are types of objects so any passed array will be validated as true null values are considered objects in javascript, but it will be validated as false if provided.
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
console.log(Is.object(null)); // false
// 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 javascript object
// plain objects
let myObject = {};
console.log(Is.plainObject(myObject)); // true
// classes
class myClass {}
let anotherObject = new myClass;
console.log(Is.plainObject(myObject)); // false
// arrays
let myArray = [];
console.log(Is.plainObject(myArray)); // false
// null value
console.log(Is.plainObject(null)); // falseIs.a
Check if the given value is an instance of the given class
class myClass {}
let myObject = new myClass;
console.log(Is.a(myObject, myClass)); // trueIs.array
Check if the given value is an array
let myArray = [4 , 'hello', 9];
console.log(Is.array(myArray)); // trueIs.iterable
Check if the given value is iterable
let myArray = [1, 2, 3];
console.log(Is.iterable(myArray)); // true
let myObject = {a: 1, b: 2, c: 3};
console.log(Is.iterable(myObject)); // true
let myString = 'hello';
console.log(Is.iterable(myString)); // true
let myNumber = 123;
console.log(Is.iterable(myNumber)); // false
let myBoolean = true;
console.log(Is.iterable(myBoolean)); // false
let myFunction = function() {};
console.log(Is.iterable(myFunction)); // false
let myNull = null;
console.log(Is.iterable(myNull)); // false
let myUndefined = undefined;
console.log(Is.iterable(myUndefined)); // falseIt works fine as well with any class that implements the Symbol.iterator method
class myClass {
*[Symbol.iterator]() {
yield 1;
yield 2;
yield 3;
}
}
let myObject = new myClass();
console.log(Is.iterable(myObject)); // trueIs.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)); // falseFunctions And Generators
Is.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 is an alias for Is.function Is.fn is an alias for Is.function
Is.generator
Check if the given value is a generator
function* myGenerator() {
yield 1;
yield 2;
yield 3;
}
let myGeneratorObject = myGenerator();
console.log(Is.generator(myGeneratorObject)); // trueIs.native
Check if the given value is a native javascript function
let myFunction = function() {
return 'hello world';
};
console.log(Is.native(myFunction)); // false
console.log(Is.native(Math.random)); // trueMisc
Is.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:8080';
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.date
Check if the given value is a date
let myDate = new Date();
console.log(Is.date(myDate)); // trueIs.promise
Check if the given value is a promise
let myPromise = new Promise((resolve, reject) => {
resolve('hello world');
});
console.log(Is.promise(myPromise)); // trueIs.error
Check if the given value is an error
let myError = new Error('my error');
console.log(Is.error(myError)); // trueDOM & Browser
Is.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.body)); // trueIs.form
Check if the given value is a form element
let myForm = document.querySelector('form');
console.log(Is.form(myForm)); // trueIs.formElement is an alias for Is.form
Is.formData
Check if the given value is a form data
let myFormData = new FormData();
console.log(Is.formData(myFormData)); // trueIs.browser
Check if current browser matches the given name
console.log(Is.browser('chrome'));
console.log(Is.browser('firefox'));
console.log(Is.browser('safari'));
console.log(Is.browser('opera'));
console.log(Is.browser('edge'));
console.log(Is.browser('ie'));Is.validHtmlId
Check if the given value is a valid html id
let id = 'myId';
console.log(Is.validHtmlId(id)); // true
id = 'myId-1';
console.log(Is.validHtmlId(id)); // true
id = 'myId-1-1';
console.log(Is.validHtmlId(id)); // true
id = 'myId-1-';
console.log(Is.validHtmlId(id)); // falseIs.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.android()) {
// 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.desktop()) {
// do something
}Is.enabled.*
Check for variant data that are enabled in the browser
console.log(Is.enabled.cookies());
console.log(Is.enabled.localStorage());
console.log(Is.enabled.sessionStorage());
console.log(Is.enabled.indexedDB());
console.log(Is.enabled.webWorkers());
console.log(Is.enabled.serviceWorkers());
console.log(Is.enabled.notifications());
console.log(Is.enabled.pushNotifications());
console.log(Is.enabled.geoLocation()); // also geolocation is an alias (with lower L)
console.log(Is.enabled.webRTC());
console.log(Is.enabled.webAudio());
console.log(Is.enabled.microphone());
console.log(Is.enabled.camera());
console.log(Is.enabled.speechRecognition());
console.log(Is.enabled.speechSynthesis());
console.log(Is.enabled.fullScreen()); // also fullscreen is an alias (with lower S)
console.log(Is.enabled.vibration());
console.log(Is.enabled.touch());
console.log(Is.enabled.battery());
console.log(Is.enabled.fetch());
console.log(Is.enabled.history());
console.log(Is.enabled.darkMode());
console.log(Is.enabled.lightMode());
console.log(Is.enabled.animation());
console.log(Is.enabled.transition());Tests
To run tests, run the following command
npm run testOR
yarn testChange Log
- 1.0.7 (11 Oct 2022)
- Added tests
- Enhanced Documentation
- Added more methods