@wicle/is
v2.0.1
Published
JavaScript and TypeScript type testing library uniquely identifying types without overlap.
Maintainers
Readme
@wicle/is
JavaScript and TypeScript type testing library uniquely identifying types without overlap.
It does type testing for the following types:
- Array
- Object
- Function
- String
- Number
- Date
- RegExp
- Error
- Symbol
- Map
- WeakMap
- Set
- WeakSet
- Class
- Promise
- AsyncFunction
- Glob
If a variable is one of the above types, it means it is not of any other types above. For example, if a variable is of type Function, then it is not of type Object. A class instance is of type Object, but not of type Class or Function, etc.
Installation
npm install @wicle/is
# or
yarn add @wicle/is
# or
pnpm add @wicle/isUsage
import is from '@wicle/is'
function processValue(arg) {
if (is.isFunction(arg)) {
// Handle function
arg()
} else if (is.isClass(arg)) {
// Handle class constructor
const instance = new arg()
} else if (is.isString(arg)) {
// Handle string
console.log(arg.toUpperCase())
}
// ... handle other types
}API
All functions return true if the value matches the specified type, false otherwise.
is.isArray(arg)- Tests if value is an Arrayis.isObject(arg)- Tests if value is a plain Objectis.isFunction(arg)- Tests if value is a Function (excluding classes and async functions)is.isString(arg)- Tests if value is a Stringis.isNumber(arg)- Tests if value is a Numberis.isDate(arg)- Tests if value is a Dateis.isRegExp(arg)- Tests if value is a RegExpis.isError(arg)- Tests if value is an Erroris.isSymbol(arg)- Tests if value is a Symbolis.isMap(arg)- Tests if value is a Mapis.isWeakMap(arg)- Tests if value is a WeakMapis.isSet(arg)- Tests if value is a Setis.isWeakSet(arg)- Tests if value is a WeakSetis.isClass(arg)- Tests if value is a Class constructoris.isPromise(arg)- Tests if value is a Promise-like objectis.isAsyncFunction(arg)- Tests if value is an async functionis.isGlob(arg)- Tests if value is a glob pattern (string or array of strings)
Important Notes
Class vs Object vs Function
This library makes clear distinctions between:
- Class: A class constructor/definition
- Function: A regular function (not a class or async function)
- Object: A plain object or class instance
class MyClass {}
const myFunction = () => {}
const myObject = {}
const myInstance = new MyClass()
is.isClass(MyClass) // true
is.isFunction(MyClass) // false
is.isObject(MyClass) // false
is.isFunction(myFunction) // true
is.isClass(myFunction) // false
is.isObject(myFunction) // false
is.isObject(myObject) // true
is.isClass(myObject) // false
is.isFunction(myObject) // false
is.isObject(myInstance) // true (class instances are objects)
is.isClass(myInstance) // false
is.isFunction(myInstance) // falseType Safety
All functions provide TypeScript type guards for better type safety:
function processValue(value: unknown) {
if (is.isString(value)) {
// TypeScript now knows 'value' is a string
console.log(value.toUpperCase())
}
if (is.isArray(value)) {
// TypeScript now knows 'value' is an array
console.log(value.length)
}
}