is
@bearclaw/is
is a set of runtime type-checking and environment-detecting functions.
Helpers
validate
Check that some assertion is true and return a ValidationException
(or some provided custom error) if it is not.
function validateIsNotNil(value: unknown) {
return validate(!isNil(value), 'isNotNil')
}
validateIsNotNil({})
validateIsNotNil(null)
assert
Check that some assertion is true and throw an AssertionException
(or some provided custom error) if it is not.
function assertIsNotNil(value: unknown) {
return assert(!isNil(value), 'isNotNil')
}
assertIsNotNil({})
assertIsNotNil(null)
getType
Get a value's type. Uses Object.prototype.toString
so it will not work with custom classes unless [Symbol.toStringTag]
is defined. See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures
getType([])
getType(1)
getType('1')
Type Checking
isArray
Is the value an Array?
isArray([1])
isArray(1)
validateArray([1])
validateArray(1)
assertArray([1])
assertArray(1)
isArrowFunction
Is the value an arrow function?
isArrowFunction(() => 'a')
isArrowFunction(function () {})
validateArrowFunction(() => 'a')
validateArrowFunction(function () {})
assertArrowFunction(() => 'a')
assertArrowFunction(function () {})
isAsyncFunction
Is the value an async function?
isAsyncFunction(async () => 'a')
isAsyncFunction(() => 'a')
validateAsyncFunction(async () => 'a')
validateAsyncFunction(() => 'a')
assertAsyncFunction(async () => 'a')
assertAsyncFunction(() => 'a')
isBigInt
Is the value a BigInt?
isBigInt(BigInt(1))
isBigInt(1)
validateBigInt(BigInt(1))
validateBigInt(1)
assertBigInt(BigInt(1))
assertBigInt(1)
isBindable
Is the value bindable? Arrow functions, constructors and functions that are already bound will not be bindable.
isBindable(function () {})
isBindable(function () { return 'a'; }.bind(this))
isBindable(() => 'a')
validateBindable(function () {})
validateBindable(function () { return 'a'; }.bind(this))
validateBindable(() => 'a')
assertBindable(function () {})
assertBindable(function () { return 'a'; }.bind(this))
assertBindable(() => 'a')
isBoolean
Is the value a boolean?
isBoolean(true)
isBoolean(1)
validateBoolean(true)
validateBoolean(1)
assertBoolean(true)
assertBoolean(1)
isBoundFunction
Is the value a bound function?
isBoundFunction(function () { return 'a'; }.bind(this))
isBoundFunction(function () {})
validateBoundFunction(function () { return 'a'; }.bind(this))
validateBoundFunction(function () {})
assertBoundFunction(function () { return 'a'; }.bind(this))
assertBoundFunction(function () {})
isClassCtor
Is the values a class constructor?
isClassCtor(class Person {})
isClassCtor(new Person())
validateClassCtor(class Person {})
validateClassCtor(new Person())
assertClassCtor(class Person {})
assertClassCtor(new Person())
isDateObject
Is the value a Date object?
isDateObject(new Date())
isDateObject(1)
validateDateObject(new Date())
validateDateObject(1)
assertDateObject(new Date())
assertDateObject(1)
isEmpty
Is the value an empty string, null
, undefined
, NaN
or an array, map, object or set with no values?
isEmpty('')
isEmpty([])
isEmpty('example')
validateEmpty('')
validateEmpty([])
validateEmpty('example')
assertEmpty('')
assertEmpty([])
assertEmpty('example')
isEmptyArray
Is the value an array with no values?
isEmptyArray([])
isEmptyArray(['1'])
validateEmptyArray([])
validateEmptyArray(['1'])
assertEmptyArray([])
assertEmptyArray(['1'])
isEmptyMap
Is the value a Map with no entries?
isEmptyMap(new Map())
isEmptyMap(new Map([['foo', 'bar']]))
validateEmptyMap(new Map())
validateEmptyMap(new Map([['foo', 'bar']]))
assertEmptyMap(new Map())
assertEmptyMap(new Map([['foo', 'bar']]))
isEmptyObject
Is the value a plain object with no entries?
isEmptyObject({})
isEmptyObject({ foo: 'bar' })
validateEmptyObject({})
validateEmptyObject({ foo: 'bar' })
assertEmptyObject({})
assertEmptyObject({ foo: 'bar' })
isEmptyPrimitive
Is the value an empty string, null
, undefined
or NaN
?
isEmptyPrimitive('')
isEmptyPrimitive('example')
validateEmptyPrimitive('')
validateEmptyPrimitive('example')
assertEmptyPrimitive('')
assertEmptyPrimitive('example')
isEmptySet
Is the value a Set with no values?
isEmptySet(new Set())
isEmptySet(new Set([1]))
validateEmptySet(new Set())
validateEmptySet(new Set([1]))
assertEmptySet(new Set())
assertEmptySet(new Set([1]))
isEmptyString
Is the value a string with no characters?
isEmptyString('')
isEmptyString('1')
validateEmptyString('')
validateEmptyString('1')
assertEmptyString('')
assertEmptyString('1')
isEmptyStructural
Is the value an array, map, object or set with no values?
isEmptyStructural([])
isEmptyStructural(['example'])
validateEmptyStructural([])
validateEmptyStructural(['example'])
assertEmptyStructural([])
assertEmptyStructural(['example'])
isFalsy
Is the value falsy?
isFalsy(0)
isFalsy(1)
validateFalsy(0)
validateFalsy(1)
assertFalsy(0)
assertFalsy(1)
isFunction
Is the value a function?
isFunction(() => {})
isFunction(1)
validateFunction(() => {})
validateFunction(1)
assertFunction(() => {})
assertFunction(1)
isGeneratorFunction
Is the value a generator function?
isGeneratorFunction(function* () { yield 'a' })
isGeneratorFunction(() => 'a')
validateGeneratorFunction(function* () { yield 'a' })
validateGeneratorFunction(() => 'a')
assertGeneratorFunction(function* () { yield 'a' })
assertGeneratorFunction(() => 'a')
isImmutable
Is the value immutable?
isImmutable(1)
isImmutable(Object.freeze({}))
isImmutable({})
validateImmutable(1)
validateImmutable(Object.freeze({}))
validateImmutable({})
assertImmutable(1)
assertImmutable(Object.freeze({}))
assertImmutable({})
isInstanceOf
Is the value an instance of the provided constructor?
isInstanceOf(Number, 1)
isInstanceOf(String, 1)
validateInstanceOf(Number, 1)
validateInstanceOf(String, 1)
assertInstanceOf(Number, 1)
assertInstanceOf(String, 1)
isJSON
Is the value a valid JSON value?
isJSON({ 'foo': 'bar' })
isJSON(new Map())
validateJSON({ 'foo': 'bar' })
validateJSON(new Map())
assertJSON({ 'foo': 'bar' })
assertJSON(new Map())
isMap
Is the value a Map?
isMap(new Map())
isMap({})
validateMap(new Map())
validateMap({})
assertMap(new Map())
assertMap({})
isNull
Is the value null?
isNull(null)
isNull(1)
validateNull(null)
validateNull(1)
assertNull(null)
assertNull(1)
isNil
Is the value null or undefined?
isNil(null)
isNil(undefined)
isNil(1)
validateNil(null)
validateNil(undefined)
validateNil(1)
assertNil(null)
assertNil(undefined)
assertNil(1)
isNumber
Is the value a number?
isNumber(1)
isNumber('')
validateNumber(1)
validateNumber('')
assertNumber(1)
assertNumber('')
isObject
Is the value a non-null object?
isObject({})
isObject(1)
validateObject({})
validateObject(1)
assertObject({})
assertObject(1)
isPlainObject
Is the value a plain object?
isPlainObject({})
isPlainObject(new Person())
validatePlainObject({})
validatePlainObject(new Person())
assertPlainObject({})
assertPlainObject(new Person())
isPrimitive
Is the value one of the primitive types?
isPrimitive(1)
isPrimitive({})
validatePrimitive(1)
validatePrimitive({})
assertPrimitive(1)
assertPrimitive({})
isPromise
Is the value a Promise?
isPromise(Promise.resolve(a))
isPromise(() => 'a')
validatePromise(Promise.resolve(a))
validatePromise(() => 'a')
assertPromise(Promise.resolve(a))
assertPromise(() => 'a')
isSet
Is the value a Set?
isSet(new Set())
isSet([])
validateSet(new Set())
validateSet([])
assertSet(new Set())
assertSet([])
isString
Is the value a string?
isString('')
isString(1)
validateString('')
validateString(1)
assertString('')
assertString(1)
isStructural
Is the value a structural type (object)?
isStructural({})
isStructural(1)
validateStructural({})
validateStructural(1)
assertStructural({})
assertStructural(1)
isSymbol
Is the value a Symbol?
isSymbol(Symbol(''))
isSymbol('')
validateSymbol(Symbol(''))
validateSymbol('')
assertSymbol(Symbol(''))
assertSymbol('')
isTruthy
Is the value truthy?
isTruthy(1)
isTruthy(0)
validateTruthy(1)
validateTruthy(0)
assertTruthy(1)
assertTruthy(0)
isType
Is the value the provided type? Uses Object.prototype.toString
so it will not work with custom classes unless [Symbol.toStringTag]
is defined.
isType('Number', 1)
isType('String', 1)
validateType('Number', 1)
validateType('String', 1)
assertType('Number', 1)
assertType('String', 1)
isUndefined
Is the value undefined?
isUndefined(undefined)
isUndefined(1)
validateUndefined(undefined)
validateUndefined(1)
assertUndefined(undefined)
assertUndefined(1)
isWeakMap
Is the value a WeakMap?
isWeakMap(new WeakMap())
isWeakMap({})
validateWeakMap(new WeakMap())
validateWeakMap({})
assertWeakMap(new WeakMap())
assertWeakMap({})
isWeakSet
Is the value a WeakSet?
isWeakSet(new WeakSet())
isWeakSet([])
validateWeakSet(new WeakSet())
validateWeakSet([])
assertWeakSet(new WeakSet())
assertWeakSet([])
Environment Detection
isBrowserContext
Is this function being invoked in a browser context?
isBrowserContext()
validateBrowserContext()
assertBrowserContext()
isDenoContext
Is this function being invoked in a Deno context?
isDenoContext()
validateDenoContext()
assertDenoContext()
isNodeContext
Is this function being invoked in a Node.js context?
isNodeContext()
validateNodeContext()
assertNodeContext()
isWebWorkerContext
Is this function being invoked in a WebWorker context?
isWebWorkerContext()
validateWebWorkerContext()
assertWebWorkerContext()
Contributing
Running unit tests
Run nx test is
to execute the unit tests via Jest.