Javascript does not have a pretty way to prove something belongs to a specific type. (Except for Typescript)
Ex1. typeof checking
typeof null
"object"
typeof undefined
"undefined"
typeof "undefined"
"string"
typeof Object
"function"
typeof object
"undefined"
typeof {}
"object"
typeof true
"boolean"
typeof (1 < 2)
"boolean"
typeof 1
"number"
typeof 0
"number"
Ex2. boolean comparisons | Expresion | Result | | - | - | | true == 0 | false | | true == 1 | true | | true == 2 | false | | true == (1 < 2) | true | | true == "" | false | | true == "a | false | | true == {} | false | | true == !{} | false | | true == [] | false | | true == ![] | false | | true == null | false | | true == !null | true | | true == undefined | false | | true == !undefined | true |
Ex3. conditionals
if blocks or ternaries comparisons result are "casted" using ECMA-262 normative.
// FALSE goes to else block
if (0) {
//
}
// TRUE goes to if block
if (1) {
//
}
// FALSE goes to else block
if ("") {
//
}
// TRUE goes to if block
if ("a") {
//
}
// TRUE goes to if block
if ({}) {
//
}
// FALSE goes to else block
if (!{}) {
//
}
Not as intuitive as you would like.
null
undefined
function
[] // Array
{} // Object
Promise
"" // String
- isNull
- isUndefined
- isFunction
- isString
- isNumber
- isInteger
- isFloat
- isArray
- isEmptyArray
- isObject
- isEmptyObject
- isPromise
- isEmpty
First you need to import it in your project
The require way
let { isObject } = require("the-type-validator");
The import way
import { isObject } from "the-type-validator";
All validator methods returns boolean
Ex.1 - Checking a null
const aNull = null
const isVarObject = isObject(aNull)
const isVarNull = isNull(aNull)
const isVarUndefined = isUndefined(aNull)
console.log('isVarObject', isVarObject)
false
console.log('isVarNull', isVarNull)
true
console.log('isVarUndefined', isVarUndefined)
false
Ex.2 - Checking an object
const anObject = {}
const isVarObject = isObject(aNull)
const isVarArray = isArray(aNull)
console.log('isVarObject', isVarObject)
true
console.log('isVarArray', isVarArray)
false
Ex.3 - We can check if object is already empty
const isVarObjectAndEmpty = isEmptyObject(anObject)
const isVarArrayAndEmpty = isEmptyArray(anObject)
console.log('isVarObjectAndEmpty', isVarObjectAndEmpty)
true
console.log('isVarArrayAndEmpty', isVarArrayAndEmpty)
false
Additional JSDOC info
- isArray
- isEmptyArray
- isEmpty
- getType
- isNumber
- isInteger
- isFloat
- isObject
- isPlainObject
- isEmptyObject
- isNull
- isUndefined
- isFunction
- isString
- isPromise
Checks if data is an array.
data
any the data to check
Returns boolean true or false wheter data is an array or not
Checks if data is an empty array.
data
any the data to check
Returns boolean true or false wheter data is an empty array or not
Checks if data is empty, whether is an array or an object.
data
any the data to check
Returns boolean true or false wheter data is an empty objct or empty array
Gets data type.
data
any the data to check
Returns string the type to return
Checks if data is a number.
data
any the data to check
Returns boolean true or false wheter data is Number or not
Checks if data is an integer number.
data
any the data to check
Returns boolean true or false wheter data is Integer or not
Checks if data is a float number.
data
any the data to check
Returns boolean true or false wheter data is Float or not
Checks if data is an object.
data
any the data to check
Returns boolean true or false wheter data is Object or not
Checks if data is a plain object. Borrowing definition as stands in https://stackoverflow.com/questions/51722354/the-implementation-of-isplainobject-function-in-redux
data
any the data to check
Returns boolean true or false wheter data is Plain Object or not
Checks if data is an empty object.
data
any the data to check
Returns boolean true or false wheter data is Empty Object or not
Checks if data is null.
data
any the data to check
Returns boolean true or false wheter data is Null or not
Checks if data is undefined.
data
any the data to check
Returns boolean true or false wheter data is Undefined or not
Checks if data is a function.
data
any the data to check
Returns boolean true or false wheter data is Function or not
Checks if data is a string.
data
any the data to check
Returns boolean true or false wheter data is String or not
Checks if data is a promise.
data
any the data to check
Returns boolean true or false wheter data is Promise or not
Checks if data is empty, whether is an array or an object.
data
any the data to check
Returns boolean true or false wheter data is an empty objct or empty array
Gets data type.
data
any the data to check
Returns string the type to return
Checks if data is an array.
data
any the data to check
Returns boolean true or false wheter data is an array or not
Checks if data is an empty array.
data
any the data to check
Returns boolean true or false wheter data is an empty array or not
Checks if data is a number.
data
any the data to check
Returns boolean true or false wheter data is Number or not
Checks if data is an integer number.
data
any the data to check
Returns boolean true or false wheter data is Integer or not
Checks if data is a float number.
data
any the data to check
Returns boolean true or false wheter data is Float or not
Checks if data is an object.
data
any the data to check
Returns boolean true or false wheter data is Object or not
Checks if data is a plain object. Borrowing definition as stands in https://stackoverflow.com/questions/51722354/the-implementation-of-isplainobject-function-in-redux
data
any the data to check
Returns boolean true or false wheter data is Plain Object or not
Checks if data is an empty object.
data
any the data to check
Returns boolean true or false wheter data is Empty Object or not
Checks if data is null.
data
any the data to check
Returns boolean true or false wheter data is Null or not
Checks if data is undefined.
data
any the data to check
Returns boolean true or false wheter data is Undefined or not
Checks if data is a function.
data
any the data to check
Returns boolean true or false wheter data is Function or not
Checks if data is a string.
data
any the data to check
Returns boolean true or false wheter data is String or not
Checks if data is a promise.
data
any the data to check
Returns boolean true or false wheter data is Promise or not