what-type-is
v1.2.0
Published
A simple type checking library for Node.js
Downloads
8
Maintainers
Readme
Table of Contents
Example
const { getType, isPlainObject, isArray } = require('what-type-is')
getType([]) // array
isPlainObject([]) // false
isArray(['array']) // true
Installation
npm i --save what-type-is
Tests
npm test
Usage
You can import all functions or import it on-demand using destructuring assignment
Importing all functions
const it = require('what-type-is')
const message = 'Hello'
console.log(
it.isString(message)
) // true
Importing on-demand
This is a great way to import only what you need to use. You can import any functions available using destructuring assignment. Just take a look at API section to know all functions you can use and import it on-demand.
const { isArray, isObject, isPlainObject isFunction } = require('what-type-is')
const messages = ['Hello', 'World', 'How are you?']
console.log(
isArray(messages)
) // true
console.log(
isObject(messages)
) // true
console.log(
isPlainObject(messages)
) // false
console.log(
isFunction(messages)
) // false
API
- getType
- isArray
- isString
- isNumber
- isBoolean
- isObject
- isPlainObject
- isFunction
- isDate
- isRegExp
- isNull
- isUndefined
getType( any )
Returns a string containing the type of the given argument.
Example
getType(['array']) // array
getType(new Date) // date
getType({}) // object
isArray( any )
Check if a given argument is an array and returns a boolean.
Example
isArray(['array']) // true
isArray('string') // false
isString( any )
Check if a given argument is a string and returns a boolean.
Example
isString('string') // true
isString(['array']) // false
isNumber( any )
Check if a given argument is a number and returns a boolean.
Example
isNumber(10) // true
isNumber(['array']) // false
isBoolean( any )
Check if a given argument is a boolean and returns a boolean.
Example
isBoolean(true) // true
isBoolean(20) // false
isObject( any )
Check if a given argument is an object according with ECMA spec and returns a boolean.
Example
isObject({}) // true
isObject(['array']) // true
isPlainObject( any )
Check if a given argument is a plain object and returns a boolean.
Example
isPlainObject({}) // true
isPlainObject(['array']) // false
isFunction( any )
Check if a given argument is a function and returns a boolean.
Example
isFunction(() => {}) // true
isFunction(undefined) // false
isDate( any )
Check if a given argument is a date object and returns a boolean.
Example
isDate(new Date) // true
isDate('27/10/2017') // false
isRegExp( any )
Check if a given argument is a regular expression and returns a boolean.
Example
isRegExp(new RegExp) // true
isRegExp(/regularexpression/i) // true
isNull( any )
Check if a given argument is a null and returns a boolean.
Example
isNull(null) // true
isNull(0) // false
isUndefined( any )
Check if a given argument is an undefined and returns a boolean.
Example
isUndefined(undefined) // true
isUndefined(null) // false