Git Product home page Git Product logo

the-type-validator's Introduction

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

the-type-validator's People

Contributors

xoxefdp avatar

Stargazers

 avatar Victor Nogueira avatar

Watchers

James Cloos avatar  avatar

Forkers

xoxefdp

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.