A fast and lightweight library built to help with type checking in plain Javascript that works with new and old versions!
Local: download the library and include in the <head>
section of your website. Link to library
With Node: npm i --save check-your-js
If you included locally, you can use the funcations as you normally would with an included script.
With Node: import * as CheckJS from 'check-your-js';
This function is used to check if the passed param is of type Integer. Will throw error if not.
Integer(42) // returns 42
Integer('text') // throw error
Integer(42.2) // throw error - cannot convert float to int
This function is used to check if the passed param is of type Float. Will throw error if not.
Float(42.2) // returns 42.2
Float({"name": "bob"}) // throw error
Float(42) // throw error - cannot convert int to float
This function is used to check if the passed param is of type String. Will throw error if not.
String('text') // returns text
String(42) // returns "42"
String({"name": "bob"}) // throw error
This function is used to check if the passed param is of type Object. Will throw error if not.
Object({"name": "bob"}) // return {"name": "bob"}
Object('text') // throw error
This function is used to check if the passed param is of type Boolean. Will throw error if not.
Object(false) // return false
Object('text') // throw error
This function is used to check if the passed param is of type Function. Will throw error if not.
Function(function(){}) // return function
Function('text') // throw error
In Checkjs we use a special "type" called a collection. This is the similar to other languages that have a type of Interface. Here, the Collection type is an array of any passed values / types. It is primarly used as a constructor to make an array that accepts any types. You can then run any other type checker on the Collection's items by passing them to another check function.
Collection('text', 42, {"name": "bob"}) // returns ['text', 42, {"name": "bob"}]
Collection() // throw error
To then check one of the values you can pass the Collection's item like so:
var collection = Collection('text', 42, {"name": "bob"}); // new Collection
var int = Integer(collection[1]) // returns true because the second item is an integer
The NotEmpty function is used to check if the passed param is empty or not. Useful when you want to perform an action only if there is something in the variable.
var word = 'text';
var empty = {};
NotEmpty(word) // returns true
NotEmpty(empty) // returns false