As we already know, JavaScript is a dynamic language, which means that we don't need to specify the type of the variable when we declare it, and we can change the type of the variable at any time.
Supportive Is
Supportive Is is a package that helps you to check the type of the value in JavaScript, it's a very simple package that has only one object Is that contains a lot of methods to check the type of the value.
Installation
npm i @mongez/supportive-is
OR
yarn add @mongez/supportive-is
Before we move on
Before we move on, I want to mention that this package covers three main things, data type checks, dom checks, and whether browser is enabling some technologies (i.e notifications/service worker/ full screen and so on), so stay calm and keep up with me.
Also don't forget to check the super empty method, it will be in the middle of the article.
Usage
Just import the Is object from the package and use it.
importIsfrom'@mongez/supportive-is';// start using it
Primitives
Primitives are the most basic data types in JavaScript, they are immutable and can't be changed, they are: string , number , boolean , null , undefined , symbol , bigint .
However, there are some other cases where we need to check if the value is just integer or float, this will be in handy as well, also checking for numeric values regardless of the type whether its number or string.
Check for string:
Is.string('hello world');// true
Check for number:
Is.number(123);// true
Check for boolean:
Is.boolean(true);// true
Or Is.bool for short:
Is.bool(true);// true
Check for null:
Is.null(null);// true
Check for undefined:
Is.undefined(undefined);// true
Check for symbol:
Is.symbol(Symbol('hello world'));// true
Check for bigint:
Is.bigint(BigInt(123));// true
Check for integer:
Is.integer(123);// true
Or Is.int for short:
Is.int(123);// true
Check for float:
Is.float(123.45);// true
Check for numeric:
Is.numeric(123);// trueIs.numeric('123');// true
Objects
Objects are the most common data type in JavaScript, they are mutable and can be changed, we'll go through the most common objects in JavaScript.
Let's start with object, which refers to literally any object in JavaScript:
The super empty method is a method that checks if the value is empty or not, it's a very useful method that can be used in many cases, it validates strings, arrays, objects, maps, sets and any iterable objects, however, Zeros (0) and false values are not considered to be empty.
Now let's check if the user is browsing from mobile or desktop:
Is.mobile.android();Is.mobile.ios();Is.mobile.iphone();Is.mobile.ipod();Is.mobile.windows();// windows mobileIs.mobile.any();// any mobile type// check if working from desktopIs.desktop();
Note: The Is.mobile.any() method will return true if the user is browsing from any mobile device, including tablets.
Also note that Is.desktop is the negate of Is.mobile.any().
Our Final Part, the enabled checks
We can also check if the user has enabled some features in the browser, such as cookies, localStorage, sessionStorage, geolocation, notifications and tons of other stuff.
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());// will validate prefers-color-scheme media query (dark mode)console.log(Is.enabled.lightMode());// will validate prefers-color-scheme media query (light mode)console.log(Is.enabled.animation());console.log(Is.enabled.transition());
Conclusion
Finally i hope you enjoy this package, and if you have any suggestions or ideas, please feel free to post a comment to me in this post, it will be appreciated.
To see the entire package documentation, please visit the repository pageز
Top comments (0)
Subscribe
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (0)