A simple type checking library for Node.js
npm install what-type-is
A simple type checking library for Node.js
js
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$3
`js
const it = require('what-type-is')
const message = 'Hello'console.log(
it.isString(message)
) // true
`
$3
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.
`js
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$3
Returns a string containing the type of the given argument.#### Example
`js
getType(['array']) // array
getType(new Date) // date
getType({}) // object
`$3
Check if a given argument is an array and returns a boolean.#### Example
`js
isArray(['array']) // true
isArray('string') // false
`$3
Check if a given argument is a string and returns a boolean.#### Example
`js
isString('string') // true
isString(['array']) // false
`$3
Check if a given argument is a number and returns a boolean.#### Example
`js
isNumber(10) // true
isNumber(['array']) // false
`$3
Check if a given argument is a boolean and returns a boolean.#### Example
`js
isBoolean(true) // true
isBoolean(20) // false
`$3
Check if a given argument is an object according with ECMA spec and returns a boolean.#### Example
`js
isObject({}) // true
isObject(['array']) // true
`$3
Check if a given argument is a plain object and returns a boolean.#### Example
`js
isPlainObject({}) // true
isPlainObject(['array']) // false
`$3
Check if a given argument is a function and returns a boolean.#### Example
`js
isFunction(() => {}) // true
isFunction(undefined) // false
`$3
Check if a given argument is a date object and returns a boolean.#### Example
`js
isDate(new Date) // true
isDate('27/10/2017') // false
`$3
Check if a given argument is a regular expression and returns a boolean.#### Example
`js
isRegExp(new RegExp) // true
isRegExp(/regularexpression/i) // true
`$3
Check if a given argument is a null and returns a boolean.#### Example
`js
isNull(null) // true
isNull(0) // false
`$3
Check if a given argument is an undefined and returns a boolean.#### Example
`js
isUndefined(undefined) // true
isUndefined(null) // false
``