Gathering detailed insights and metrics for @mongez/supportive-is
Gathering detailed insights and metrics for @mongez/supportive-is
Gathering detailed insights and metrics for @mongez/supportive-is
Gathering detailed insights and metrics for @mongez/supportive-is
npm install @mongez/supportive-is
Typescript
Module System
TypeScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
12 Stars
14 Commits
3 Forks
2 Watchers
2 Branches
1 Contributors
Updated on Oct 16, 2024
Latest Version
2.0.4
Package Id
@mongez/supportive-is@2.0.4
Unpacked Size
71.01 kB
Size
11.04 kB
File Count
13
Published on
Oct 16, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
5
A very lightweight library to validate different things
npm i @mongez/supportive-is
Or using Yarn
yarn add @mongez/supportive-is
Read the previous version documentation from here.
In older version, all checkers are exported in teh default object Is
, but from version 2, each method is separated in its own function, this will increase performance when tree shaking is enabled.
1// Old Way 2import Is from "@mongez/supportive-is"; 3 4console.log(Is.empty({})); // true 5 6// New Way 7import { isEmpty } from "@mongez/supportive-is"; 8 9console.log(isEmpty({})); // true
We've tons of primitives to be checked, here are some of them.
Check if the given value is a string
, number
, boolean
, null
, or symbol
1import { isPrimitive } from "@mongez/supportive-is";
2
3console.log(isPrimitive("hello")); // true
4console.log(isPrimitive(22.5)); // true
5console.log(isPrimitive(false)); // true
6console.log(isPrimitive(null)); // true
7console.log(isPrimitive(Symbol("SymbolKey"))); // true
8console.log(isPrimitive([])); // false
9console.log(isPrimitive({})); // false
Check if the given value is a string
, number
, bigInt
or boolean
1import { isScalar } from "@mongez/supportive-is";
2
3console.log(isScalar("hello")); // true
4console.log(isScalar(22.5)); // true
5console.log(isScalar(false)); // true
6console.log(isScalar(null)); // false
7console.log(isScalar(undefined)); // false
8console.log(isScalar([])); // false
9console.log(isScalar({})); // false
Check if the given value string is a valid json format
1import { isJson } from "@mongez/supportive-is"; 2 3let value = '{"name":"Hasan","job":"Software Engineer"}'; 4 5console.log(isJson(value)); // true
Check if the given value is a string
1import { isString } from "@mongez/supportive-is"; 2 3let myString = "hello world"; 4 5console.log(isString(myString)); // true
Check if the given value is a number whether if its data type is String
or Number
1import { isNumeric } from "@mongez/supportive-is"; 2 3let numberInt = 12; 4console.log(isNumeric(numberInt)); // true 5 6let numberFloat = 12.55; 7console.log(isNumeric(numberFloat)); // true 8 9let numberWrittenInString = "99"; 10console.log(isNumeric(numberWrittenInString)); // true 11 12let floatWrittenInString = "99.99"; 13console.log(isNumeric(floatWrittenInString)); // true
Check if the given value is an integer and its data type is number
1import { isInt } from "@mongez/supportive-is"; 2 3let number = 12; 4console.log(isInt(numberInt)); // true 5 6let numberInString = "12"; 7console.log(isInt(numberInString)); // false
Check if the given value is a float number and its data type is number
1import { isFloat } from "@mongez/supportive-is"; 2 3let number = 921; 4console.log(isFloat(number)); // true 5 6number = 82.42; 7console.log(isFloat(number)); // true 8 9let numberInString = "12"; 10console.log(isFloat(numberInString)); // false 11 12let floatInString = "12.5"; 13 14console.log(isFloat(floatInString)); // false
Check if the given value is a regular expression
1import { isRegex } from "@mongez/supportive-is"; 2 3let regex = /hello/; 4 5console.log(isRegex(regex)); // true 6 7let regexString = "/hello/"; 8 9console.log(isRegex(regexString)); // false 10 11let regexObject = new RegExp("hello"); 12 13console.log(isRegex(regexObject)); // true
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.
1import { isObject } from "@mongez/supportive-is";
2
3let myObject = {};
4console.log(isObject(myObject)); // true
5
6class myClass {}
7let anotherObject = new myClass();
8console.log(isObject(myObject)); // true
9
10let myArray = [];
11console.log(isObject(myArray)); // true
12
13console.log(isObject(null)); // false
14
15// to check if the given value is an object but not an array
16//you must mix between isObject AND Is.array to avoid an array
17if (isObject(myVar) && !Is.array(myVar)) {
18 // do something with that object
19}
Check if the given value is an array
1import { isArray } from "@mongez/supportive-is"; 2 3let myArray = []; 4 5console.log(isArray(myArray)); // true
This is just an alias to
Array.isArray
method
Check if the given value is a plain javascript object
1import { isPlainObject } from "@mongez/supportive-is";
2
3// plain objects
4let myObject = {};
5console.log(isPlainObject(myObject)); // true
6
7// classes
8class myClass {}
9let anotherObject = new myClass();
10
11console.log(isPlainObject(myObject)); // false
12
13// arrays
14let myArray = [];
15
16console.log(isPlainObject(myArray)); // false
17
18// null value
19console.log(isPlainObject(null)); // false
Check if the given value is iterable
1import { isIterable } from "@mongez/supportive-is";
2
3let myArray = [1, 2, 3];
4
5console.log(isIterable(myArray)); // true
6
7let myObject = { a: 1, b: 2, c: 3 };
8
9console.log(isIterable(myObject)); // true
10
11let myString = "hello";
12
13console.log(isIterable(myString)); // true
14
15let myNumber = 123;
16
17console.log(isIterable(myNumber)); // false
18
19let myBoolean = true;
20
21console.log(isIterable(myBoolean)); // false
22
23let myFunction = function () {};
24
25console.log(isIterable(myFunction)); // false
26
27let myNull = null;
28
29console.log(isIterable(myNull)); // false
30
31let myUndefined = undefined;
32
33console.log(isIterable(myUndefined)); // false
It works fine as well with any class that implements the Symbol.iterator method
1class myClass { 2 *[Symbol.iterator]() { 3 yield 1; 4 yield 2; 5 yield 3; 6 } 7} 8 9let myObject = new myClass(); 10 11console.log(Is.iterable(myObject)); // true
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
1import { isEmpty } from "@mongez/supportive-is";
2
3// undefined values are considered empty
4let value = undefined;
5console.log(isEmpty(value)); // true
6
7// null values are considered empty
8value = null;
9console.log(isEmpty(value)); // true
10
11// Also any objects with no values are considered empty
12value = {};
13console.log(isEmpty(value)); // true
14
15value.name = "Hasan";
16console.log(isEmpty(value)); // false
17
18// Arrays
19value = [];
20console.log(isEmpty(value)); // true
21
22value.push(12);
23console.log(isEmpty(value)); // false
24
25// The `Zero` is not considered as empty value
26value = 0;
27console.log(isEmpty(value)); // false
Check if the given value is a generator
1function* myGenerator() { 2 yield 1; 3 yield 2; 4 yield 3; 5} 6 7let myGeneratorObject = myGenerator(); 8 9console.log(Is.generator(myGeneratorObject)); // true
Check if the given value is a valid url
1import { isUrl } from "@mongez/supportive-is"; 2 3let url = "google.com"; 4console.log(isUrl(url)); // true 5 6url = "https://google.com"; 7console.log(isUrl(url)); // true 8 9url = "www.google.com"; 10console.log(isUrl(url)); // true 11url = "www.google.com:8080"; 12console.log(isUrl(url)); // true 13 14url = "www.google.com?q=hello+world"; 15console.log(isUrl(url)); // true 16 17let url = "google"; 18console.log(isUrl(url)); // false
Check if the given value string is a valid email
1import { isEmail } from "@mongez/supportive-is"; 2 3let myEmail = "hassanzohdy@gmail.com"; 4console.log(isEmail(myEmail)); // true
Check if the given value is a date
1import { isDate } from "@mongez/supportive-is"; 2 3let myDate = new Date(); 4 5console.log(isDate(myDate)); // true
Check if the given value is a promise
1import { isPromise } from "@mongez/supportive-is"; 2 3let myPromise = new Promise((resolve, reject) => { 4 resolve("hello world"); 5}); 6 7console.log(isPromise(myPromise)); // true
Check if the given value is a form element
1import { isFormElement } from "@mongez/supportive-is"; 2 3let myForm = document.querySelector("form"); 4 5console.log(isFormElement(myForm)); // true
Is.formElement is an alias for Is.form
Check if the given value is a form data
1import { isFormData } from "@mongez/supportive-is"; 2 3let myFormData = new FormData(); 4 5console.log(isFormData(myFormData)); // true
Check if current browser matches the given name
1import { isBrowser } from "@mongez/supportive-is"; 2 3console.log(isBrowser("chrome")); 4console.log(isBrowser("firefox")); 5console.log(isBrowser("safari")); 6console.log(isBrowser("opera")); 7console.log(isBrowser("edge")); 8console.log(isBrowser("ie"));
Check if the given value is a valid html id
1import { isValidHtmlId } from "@mongez/supportive-is"; 2 3let id = "myId"; 4 5console.log(isValidHtml(id)); // true 6 7id = "myId-1"; 8 9console.log(isValidHtml(id)); // true 10 11id = "myId-1-1"; 12 13console.log(isValidHtml(id)); // true 14 15id = "myId-1-"; 16 17console.log(isValidHtml(id)); // false
Check if current visitor is browsing from a sort-of mobile
this property contains set of methods
1import { isMobile } from "@mongez/supportive-is"; 2// To check if user is browsing from an android device 3if (isMobile.android()) { 4 // do something 5} 6 7// To check if user is browsing from an ios device 8if (isMobile.ios()) { 9 // do something 10} 11 12// To check if user is browsing from an iphone 13if (isMobile.iphone()) { 14 // do something 15} 16 17// To check if user is browsing from an ipad 18if (isMobile.ipad()) { 19 // do something 20} 21 22// To check if user is browsing from an ipod 23if (isMobile.ipod()) { 24 // do something 25} 26 27// To check if user is browsing from a windows mobile 28if (isMobile.windows()) { 29 // do something 30} 31 32// To check if user is browsing from any type of mobile 33if (isMobile.any()) { 34 // do something 35}
Check if current visitor is browsing from a desktop device
Please note that any non mobile type will be considered as desktop.
1import { isDesktop } from "@mongez/supportive-is"; 2 3if (!isDesktop()) { 4 // do something 5}
To run tests, run the following command
1npm run test
OR
1yarn test
No vulnerabilities found.
No security vulnerabilities found.