An open API service indexing awesome lists of open source software.

https://github.com/xiscodev/the-type-validator

Type validation module for node and browsers.
https://github.com/xiscodev/the-type-validator

javascript types validation

Last synced: 10 months ago
JSON representation

Type validation module for node and browsers.

Awesome Lists containing this project

README

          



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

```js
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](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf) normative.

```js
// 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

```js
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_

```js
let { isObject } = require("the-type-validator");
```

_The import way_

```js
import { isObject } from "the-type-validator";
```

All validator methods returns boolean

Ex.1 - Checking a null

```js
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

```js
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

```js
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](#isarray)
- [Parameters](#parameters)
- [isEmptyArray](#isemptyarray)
- [Parameters](#parameters-1)
- [isEmpty](#isempty)
- [Parameters](#parameters-2)
- [getType](#gettype)
- [Parameters](#parameters-3)
- [isNumber](#isnumber)
- [Parameters](#parameters-4)
- [isInteger](#isinteger)
- [Parameters](#parameters-5)
- [isFloat](#isfloat)
- [Parameters](#parameters-6)
- [isObject](#isobject)
- [Parameters](#parameters-7)
- [isPlainObject](#isplainobject)
- [Parameters](#parameters-8)
- [isEmptyObject](#isemptyobject)
- [Parameters](#parameters-9)
- [isNull](#isnull)
- [Parameters](#parameters-10)
- [isUndefined](#isundefined)
- [Parameters](#parameters-11)
- [isFunction](#isfunction)
- [Parameters](#parameters-12)
- [isString](#isstring)
- [Parameters](#parameters-13)
- [isPromise](#ispromise)
- [Parameters](#parameters-14)

#### isArray

Checks if data is an array.

##### Parameters

- `data` **any** the data to check

Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** the type to return

#### isNumber

Checks if data is a number.

##### Parameters

- `data` **any** the data to check

Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** true or false wheter data is Object or not

#### isPlainObject

Checks if data is a plain object. Borrowing definition as stands in

##### Parameters

- `data` **any** the data to check

Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** the type to return

### isArray

Checks if data is an array.

#### Parameters

- `data` **any** the data to check

Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** true or false wheter data is Object or not

### isPlainObject

Checks if data is a plain object. Borrowing definition as stands in

#### Parameters

- `data` **any** the data to check

Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** true or false wheter data is Promise or not