@langlois/is-a v4.3.0
Javascript Type Verification Library
A small JavaScript library to help with JavaScript's data typing.
Project setup
npm i @langlois/is-aRun your tests
npm run testLints and fixes files
npm run lintUsage
import { isA } from '@langlois/is-a'
isA.array([]) // true
isA.array('[]') // falseAPI
Arguments
Returns true if passed a function's arguments list.
function () {
isA.arguments(arguments) // true
isA.arguments(arguments[0]) // false
}Array
Returns true if passed an array.
isA.array([]) // trueBoolean
Returns true if passed a boolean.
isA.boolean(true) // true
isA.boolean(false) // trueDate
Returns true if passed a date.
const today = new Date()
isA.date(today) // trueReturns true if the string passed is a valid email address.
The regex for determining if an email is valid is taken from the HTML spec.
const email = 'test@test.com'
isA.email(email) // trueError
Returns true if passed an error.
const error = new Error()
isA.error(error) // trueFunction
Returns true if passed a function.
const myFunction = () => undefined
isA.function(myFunction) // trueMap
Returns true if passed a map.
const myMap = new Map()
isA.map(myMap) // trueNaN
Returns true if passed NaN.
isA.NaN(NaN) // trueNull
Returns true if passed null.
isA.null(null) // trueNumber
Returns true if passed a number, excluding NaN.
isA.number(123) // true
isA.number(NaN) // falseNumerical
Returns true if passed a number or a string representing a number, excluding NaN.
isA.numerical(23) // true
isA.numerical('23') // true
isA.numerical('-23') // true
isA.numerical(NaN) // falseObject
Returns true if passed an object.
isA.object({}) // trueEverything in JavaScript is an Object and that means that many data types return typeof x === 'object' as true as well as x instanceof Object as true. However, it isn't really useful to treat certain things as if they were objects when developing so this function will return false for the following data types:
- dates
- errors
- maps
- regExps
- sets
- weakMaps
- weakSets
isA.object(new Date()) // false
isA.object(new Error()) // false
isA.object(new Map()) // false
isA.object(/\d/) // false
isA.object(new Set()) // false
isA.object(new WeakMap()) // false
isA.object(new WeakSet()) // falseRegExp
Returns true if passed a regExp.
const myRegExp = /\d/
isA.regExp(myRegExp) // trueSet
Returns true if passed a set.
const mySet = new Set()
isA.set(mySet) // trueString
Returns true if passed a string.
isA.string('hello, world') // trueSymbol
Returns true if passed a symbol.
const mySymbol = Symbol()
isA.symbol(mySymbol) // trueUndefined
Returns true if passed undefined.
isA.undefined(undefined) // trueURI
Returns true if passed a URL instance or a string that can become a valid URL instance.
const myUrl = new URL('https://langlois.dev')
isA.uri(myUrl) // true
isA.uri('https://langlois.dev') // trueWeakMap
Returns true if passed a weakMap.
const myWeakMap = new WeakMap()
isA.weakMap(myWeakMap) // trueWeakSet
Returns true if passed a weakSet.
const myWeakSet = new WeakSet()
isA.weakSet(myWeakSet) // true