determine if the target meets the type condition
A type condition, see TypeCondition
with string
is('xxx', 'string') // => true
with constructor
const p = new Promise()
is(p, Promise) // => true
with regexp
const $a = document.querySelector('a')
const $h1 = document.querySelector('h1')
is($a, /Element/) // => true
is($h1, /Element/) // => true
you can provide an array of type condition, return true if any of them are met
array of condition
is(new Set([1, 2]), [Map, Set]) // => true
determine if the target is an array
isArray([]) // => true
isArray({ 0: 1, length: 1 }, true) // => true
you can provide secound params to check target if arraylike
return true if target likes an array
determine if the target is an array like object. this function is alias of isArray(target, true)
isArrayLike([]) // => true
isArrayLike({ 0:1, length: 1 }) // => true
detemine if target can be cloned, for example: jQuery Object
detemine if target is a decimal. notice any type which is not number type will return false
isDec(1) // => false
isDec(1.1) // => true
determine the target is empty, it's very useful in form validation the followings mean target is empty
isEmpty({}) // => true
isEmpty(['']) // => false
isEmpty('xxx') // => false
isEmpty(true) // => false
isEmpty('') => true
compare target with other and determine if they are equal
isEqual(1, 1) // => true
isEqual(1, new Number(1)) // => true
isEqual({a: 1}, {a: 1}) // => true
isEqual(1, '1') // => false
detemine if target is a global object, for example: window
, global
detemine if target is greater than other notice any type which is not number type will return false
isGt(2, 1) // => true
isGt(1, 2) // => false
isGt('0', 1) // => false
detemine if target is a integer. notice any type which is not number type will return false
isInt(1) // => true
isInt(1.1) // => false
detemine if target is iterable it's very useful to check a object before for..of
let arr = [1, 2]
let map = new Map([1, 2])
isIterable(arr) // => true
isIterable(map) // => true
for (item of arr) {
//...
}
for (item of map) {
//...
}
detemine if target is less than other notice any type which is not number type will return false
isLt(1, 2) // => true
isLt('0', 1) // => false
detemine if target is a negative number. notice any type which is not number type will return false
isNegative(-1) // => true
isNegative(1) // => false
also work between 0 and -0
isNegative(-0) // => true
isNegative(0) // => false
detemine if target is object-like such as function
isObjectLike({}) // => true
isObjectLike(function() {}) // => true
it usually use to check some variable is extendable
`
javascript
let namespace = {}
if (isObjectLike(namespace)) {
namespace.member = 1
}
`
detemine if target is a plain object
isPlainObject({ a: 1 }) // => true
isPlainObject(new Promise(() => {})) // false
detemine if target is a positive number. notice any type which is not number type will return false
isPositive(-1) // => false
isPositive(1) // => true
also work between 0 and -0
isPositive(-0) // => false
isPositive(0) // => true
detemine if target is primitive type, such as 1
, "foobar"
isPrimitive(1) // => true
isPrimitive(null) // => true
isPrimitive({}) // => false
detemine if target is a thenable. it can be easily combined with asynchronous methods
you can checkout what is thenable here
if (isThenable(obj)) {
obj.then(callback)
}
async foo() {
if (isThenable(obj)) {
await obj
}
}
determine if the target is an typed array, like Uint8Array, Int8Array
isTypedArray(new Uint8Array(16)) // => true
isTypedArray([]) // => false
detemine if target is a number but NaN.
isValidNumber('string') // => false
isValidNumber(new Number(11)) // => true
isValidNumber(11) // => false
isValidNumber('11') // => false
return type of target. the return value will follow the following rules.
type('string') // => 'string'
type(1) // => 'number'
type(null) // => 'null'
type(undefined) // => 'undefined'
type(new Set([1, 2, 3])) // => 'Set'
type(new Number(111)) // => 'Number'
with boxed only
type(1, true) // => 'Number'
type(null, true) // => 'Null'
boxedOnly means return will always return boxed constructor name if target is primitive
Generated using TypeDoc
a condition for is function
is(promise, Promise)
just simply equalpromise instanceof Promise