npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

the-type-validator

v1.1.1

Published

A type validator for node or browser

Downloads

48

Readme

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

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