is-explicit
Why ?
- You want to do explicit type checks.
- You don't want to have to differentiate between
typeof
andinstanceof
- You keep a cool head and bring justice to chaos.
2.0
Breaking changes from the 1.x release, but the resulting
changes make is-explicit
faster and more composable.
Usage
npm install is-explicit
Does a variable have a value?
is //falseis //falseis //falseis //true
Is a variable a specific type?
//true //true //true //true //true //true //false //true //true //true //true //true //true //true //False //true //true //false //true //true //false
Is a variable a custom type?
{} // true const foo = {}const bar = {} // true // false
Is a variable one of multiple types?
// true
Types are expected to be functions:
//throws Error
Is a value an Array of a specific type?
is // == trueis // == falseis // == true,
Is a value an Object of a specific type?
is // == falseis // == falseis // == trueis // == true
Is a value a plain Object?
is // == falseis // == falseis // == trueis // == true { } is // == false
Binding
Binding is now binds it to a type:
const isFoo = Foo::is // true
Bind to multiple types:
const isBoolOrFunc = Boolean Function ::is // true
Better composition:
const mixed = 0 'string' 1 true 2 Symbol'bad' const numbers = mixed
Shortcuts
const value = 'whatever' is // == String::is(value)is // == Number::is(value)is // == Boolean::is(value)is // == Function::is(value)is // == Symbol::is(value)is // == [ Boolean, Number, String ]::is(value) isarrayOf // == String::is.arrayOfisarrayOf // == Number::is.arrayOfisarrayOf // == Boolean::is.arrayOfisarrayOf // == Symbol::is.arrayOfisarrayOf // == Function::is.arrayOfisarrayOf // == [ Boolean, Number, String ]::is.arrayOf isobjectOf // == String::is.objectOfisobjectOf // == Number::is.objectOfisobjectOf // == Boolean::is.objectOfisobjectOf // == Symbol::is.objectOfisobjectOf // == Function::is.objectOfisobjectOf // == [ Boolean, Number, String ]::isArrayOf
Additional Helpers
is // returns true if input is not null, undefined or NaN isarrayOf // returns true if input is an array of plain objects is // returns true if input is a function with a prototype (ie: arrow functions will return false) is // returns true if input is a class that extends the provided type