the-type-validator

1.1.1 • Public • Published

The type validator

Why ?

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.


Which types can be validated

null
undefined
function
[] // Array
{} // Object
Promise
"" // String

Available methods

  • isNull
  • isUndefined
  • isFunction
  • isString
  • isNumber
  • isInteger
  • isFloat
  • isArray
  • isEmptyArray
  • isObject
  • isEmptyObject
  • isPromise
  • isEmpty

How to use it?

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

JSDOC

Table of Contents

isArray

Checks if data is an array.

Parameters
  • data any the data to check

Returns boolean true or false wheter data is an array or not

isEmptyArray

Checks if data is an empty array.

Parameters
  • data any the data to check

Returns boolean true or false wheter data is an empty array or not

isEmpty

Checks if data is empty, whether is an array or an object.

Parameters
  • data any the data to check

Returns boolean true or false wheter data is an empty objct or empty array

getType

Gets data type.

Parameters
  • data any the data to check

Returns string the type to return

isNumber

Checks if data is a number.

Parameters
  • data any the data to check

Returns boolean true or false wheter data is Number or not

isInteger

Checks if data is an integer number.

Parameters
  • data any the data to check

Returns boolean true or false wheter data is Integer or not

isFloat

Checks if data is a float number.

Parameters
  • data any the data to check

Returns boolean true or false wheter data is Float or not

isObject

Checks if data is an object.

Parameters
  • data any the data to check

Returns boolean true or false wheter data is Object or not

isPlainObject

Checks if data is a plain object. Borrowing definition as stands in https://stackoverflow.com/questions/51722354/the-implementation-of-isplainobject-function-in-redux

Parameters
  • data any the data to check

Returns boolean true or false wheter data is Plain Object or not

isEmptyObject

Checks if data is an empty object.

Parameters
  • data any the data to check

Returns boolean true or false wheter data is Empty Object or not

isNull

Checks if data is null.

Parameters
  • data any the data to check

Returns boolean true or false wheter data is Null or not

isUndefined

Checks if data is undefined.

Parameters
  • data any the data to check

Returns boolean true or false wheter data is Undefined or not

isFunction

Checks if data is a function.

Parameters
  • data any the data to check

Returns boolean true or false wheter data is Function or not

isString

Checks if data is a string.

Parameters
  • data any the data to check

Returns boolean true or false wheter data is String or not

isPromise

Checks if data is a promise.

Parameters
  • data any the data to check

Returns boolean true or false wheter data is Promise or not

isEmpty

Checks if data is empty, whether is an array or an object.

Parameters

  • data any the data to check

Returns boolean true or false wheter data is an empty objct or empty array

getType

Gets data type.

Parameters

  • data any the data to check

Returns string the type to return

isArray

Checks if data is an array.

Parameters

  • data any the data to check

Returns boolean true or false wheter data is an array or not

isEmptyArray

Checks if data is an empty array.

Parameters

  • data any the data to check

Returns boolean true or false wheter data is an empty array or not

isNumber

Checks if data is a number.

Parameters

  • data any the data to check

Returns boolean true or false wheter data is Number or not

isInteger

Checks if data is an integer number.

Parameters

  • data any the data to check

Returns boolean true or false wheter data is Integer or not

isFloat

Checks if data is a float number.

Parameters

  • data any the data to check

Returns boolean true or false wheter data is Float or not

isObject

Checks if data is an object.

Parameters

  • data any the data to check

Returns boolean true or false wheter data is Object or not

isPlainObject

Checks if data is a plain object. Borrowing definition as stands in https://stackoverflow.com/questions/51722354/the-implementation-of-isplainobject-function-in-redux

Parameters

  • data any the data to check

Returns boolean true or false wheter data is Plain Object or not

isEmptyObject

Checks if data is an empty object.

Parameters

  • data any the data to check

Returns boolean true or false wheter data is Empty Object or not

isNull

Checks if data is null.

Parameters

  • data any the data to check

Returns boolean true or false wheter data is Null or not

isUndefined

Checks if data is undefined.

Parameters

  • data any the data to check

Returns boolean true or false wheter data is Undefined or not

isFunction

Checks if data is a function.

Parameters

  • data any the data to check

Returns boolean true or false wheter data is Function or not

isString

Checks if data is a string.

Parameters

  • data any the data to check

Returns boolean true or false wheter data is String or not

isPromise

Checks if data is a promise.

Parameters

  • data any the data to check

Returns boolean true or false wheter data is Promise or not

Dependents (3)

Package Sidebar

Install

npm i the-type-validator

Weekly Downloads

1

Version

1.1.1

License

MIT

Unpacked Size

33.1 kB

Total Files

23

Last publish

Collaborators

  • xoxefdp