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