Type Check Methods
These are utility methods for type checking.
Available Functions
Basic Type Checks
isString(value)
Checks if value is classified as a String primitive or object.
import { isString } from 'xfunc'
isString('abc')
// => true
isString(123)
// => falseisNumber(value)
Checks if value is classified as a Number primitive or object.
import { isNumber } from 'xfunc'
isNumber(123)
// => true
isNumber('123')
// => falseisBoolean(value)
Checks if value is classified as a boolean primitive or object.
import { isBoolean } from 'xfunc'
isBoolean(true)
// => true
isBoolean('true')
// => falseNull/Undefined Checks
isNil(value)
Checks if value is null or undefined.
import { isNil } from 'xfunc'
isNil(null)
// => true
isNil(undefined)
// => true
isNil(0)
// => falseisEmpty(value)
Checks if value is an empty object, collection, map, or set.
import { isEmpty } from 'xfunc'
isEmpty([])
// => true
isEmpty({})
// => true
isEmpty('')
// => true
isEmpty([1, 2, 3])
// => falseObject Type Checks
isObject(value)
Checks if value is the language type of Object.
import { isObject } from 'xfunc'
isObject({})
// => true
isObject([1, 2, 3])
// => true
isObject(null)
// => falseisPlainObject(value)
Checks if value is a plain object.
import { isPlainObject } from 'xfunc'
isPlainObject({})
// => true
isPlainObject(new Object())
// => true
isPlainObject([1, 2, 3])
// => falseFunction Checks
isFunction(value)
Checks if value is classified as a Function object.
import { isFunction } from 'xfunc'
isFunction(() => {})
// => true
isFunction(function() {})
// => true
isFunction('function')
// => falseDate and RegExp Checks
isDate(value)
Checks if value is classified as a Date object.
import { isDate } from 'xfunc'
isDate(new Date())
// => true
isDate('2023-01-01')
// => falseisRegExp(value)
Checks if value is classified as a RegExp object.
import { isRegExp } from 'xfunc'
isRegExp(/abc/)
// => true
isRegExp(new RegExp('abc'))
// => true
isRegExp('abc')
// => falseAdvanced Checks
isPromise(value)
Checks if value is a Promise.
import { isPromise } from 'xfunc'
isPromise(Promise.resolve())
// => true
isPromise({ then: () => {} })
// => true
isPromise({})
// => falseisError(value)
Checks if value is an Error object.
import { isError } from 'xfunc'
isError(new Error())
// => true
isError(new TypeError())
// => true
isError({ message: 'error' })
// => falseIterable Checks
isArrayLike(value)
Checks if value is an array-like object.
import { isArrayLike } from 'xfunc'
isArrayLike([1, 2, 3])
// => true
isArrayLike('hello')
// => true
isArrayLike({ length: 2, 0: 'a', 1: 'b' })
// => trueisLength(value)
Checks if value is a valid array-like length.
import { isLength } from 'xfunc'
isLength(3)
// => true
isLength(Number.MAX_SAFE_INTEGER)
// => true
isLength(-1)
// => false
isLength(3.2)
// => falseisObjectLike(value)
Checks if value is object-like (not null, typeof is 'object').
import { isObjectLike } from 'xfunc'
isObjectLike({})
// => true
isObjectLike([])
// => true
isObjectLike(() => {})
// => false
isObjectLike(null)
// => falseisPrimitive(value)
Checks if value is a primitive type.
import { isPrimitive } from 'xfunc'
isPrimitive('hello')
// => true
isPrimitive(42)
// => true
isPrimitive(null)
// => true
isPrimitive({})
// => falseisIterable(value)
Checks if value is iterable.
import { isIterable } from 'xfunc'
isIterable([1, 2, 3])
// => true
isIterable('hello')
// => true
isIterable(new Set())
// => true
isIterable({})
// => falseUtility Functions
isTypeString(value, type)
Checks if value's raw type matches the given type string.
import { isTypeString } from 'xfunc'
isTypeString('hello', 'String')
// => true
isTypeString(42, 'Number')
// => true
isTypeString(null, 'Null')
// => true
isTypeString('hello', 'Number')
// => falsetoRawType(value)
Gets the raw type string of a value.
import { toRawType } from 'xfunc'
toRawType('hello')
// => 'string'
toRawType(42)
// => 'number'
toRawType(null)
// => 'null'
toRawType(undefined)
// => 'undefined'
toRawType([])
// => 'array'makeMap(list)
Creates a mapping function to check if a value is in the given list.
import { makeMap } from 'xfunc'
const isReservedWord = makeMap(['if', 'else', 'for', 'while'])
isReservedWord('if')
// => true
isReservedWord('hello')
// => falseisNumericKey(value)
Checks if value is a numeric key.
import { isNumericKey } from 'xfunc'
isNumericKey('0')
// => true
isNumericKey('42')
// => true
isNumericKey('abc')
// => false
isNumericKey(0)
// => trueComplete List
All available type checking functions:
isArrayLikeisBooleanisDateisEmptyisErrorisFunctionisIterableisLengthisNilisNumberisObjectisObjectLikeisPlainObjectisPrimitiveisPromiseisRegExpisStringisTypeStringtoRawTypemakeMapisNumericKey
Each function returns a boolean value indicating whether the value matches the expected type.