Options
All
  • Public
  • Public/Protected
  • All
Menu

@cat5th/is

Index

Type aliases

Empty

Empty: Map<any, any> | Set<any> | FalseyPrimitive | EmptyArrayLike | EmptyPlainObject | [] | object

EmptyArrayLike

EmptyArrayLike: object

Type declaration

  • length: 0

EmptyPlainObject

EmptyPlainObject: object

Type declaration

  • [index: string]: never

FalseyPrimitive

FalseyPrimitive: "" | null | false | undefined | 0

ObjectLike

ObjectLike: any | Function

PlainObject

PlainObject: object

Type declaration

  • [name: string]: any

Primitive

Primitive: number | string | null | undefined | boolean | symbol

TypeCondition

TypeCondition: string | RegExp | Constructor

a condition for is function

  • string provide a string as condition to test target if is mets.

    for example: 'string' will match a new String('xxx') and 'xxx'. notice: not case sensitive

  • regexp such as string, but you can accurately distinguish primitive type or boxed type
     is('string', /^string$/) // => true
     is(new String('string'), /^string$/) // => false
     is(new String('string'), /^String$/) // => true
  • constructor you can also provide a constructor, is(promise, Promise) just simply equal promise instanceof Promise

TypedArray

TypedArray: Int8Array | Int16Array | Int32Array | Uint8Array | Uint16Array | Uint32Array | Uint8ClampedArray

Functions

is

  • determine if the target meets the type condition

    Parameters

    • target: any
    • type: TypeCondition

      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

    Returns boolean

  • you can provide an array of type condition, return true if any of them are met

    Parameters

    • target: any
    • types: TypeCondition[]

      array of condition

      is(new Set([1, 2]), [Map, Set]) // => true

    Returns boolean

isArray

  • isArray(target: any): boolean
  • isArray(target: any, loose: boolean): boolean
  • determine if the target is an array

    isArray([]) // => true
    isArray({ 0: 1, length: 1 }, true) // => true

    Parameters

    • target: any

    Returns boolean

  • you can provide secound params to check target if arraylike

    Parameters

    • target: any
    • loose: boolean

      return true if target likes an array

    Returns boolean

isArrayLike

  • isArrayLike(target: any): boolean
  • 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

    Parameters

    • target: any

    Returns boolean

isCloneble

  • isCloneble(target: any): boolean
  • detemine if target can be cloned, for example: jQuery Object

    Parameters

    • target: any

    Returns boolean

isDec

  • isDec(target: any): boolean
  • detemine if target is a decimal. notice any type which is not number type will return false

    isDec(1) // => false
    isDec(1.1) // => true

    Parameters

    • target: any

    Returns boolean

isEmpty

  • isEmpty(target: any): boolean
  • determine the target is empty, it's very useful in form validation the followings mean target is empty

    • target is a plain object and without any key-value
    • target is an array or array like object and length is 0
    • target is Map/Set and size is 0
    • target is type of primitive and falsey
    isEmpty({}) // => true
    isEmpty(['']) // => false
    isEmpty('xxx') // => false
    isEmpty(true) // => false
    isEmpty('') => true

    Parameters

    • target: any

    Returns boolean

isEqual

  • isEqual(target: any, other: any): boolean
  • 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

    Parameters

    • target: any
    • other: any

    Returns boolean

isGlobal

  • isGlobal(target: any): boolean
  • detemine if target is a global object, for example: window, global

    Parameters

    • target: any

    Returns boolean

isGt

  • isGt(target: any, other: any): boolean
  • 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

    Parameters

    • target: any
    • other: any

    Returns boolean

isInt

  • isInt(target: any): boolean
  • detemine if target is a integer. notice any type which is not number type will return false

    isInt(1) // => true
    isInt(1.1) // => false

    Parameters

    • target: any

    Returns boolean

isIterable

  • isIterable(target: any): boolean
  • 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) {
     //...
    }
    
    

    Parameters

    • target: any

    Returns boolean

isLt

  • isLt(target: any, other: any): boolean
  • 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

    Parameters

    • target: any
    • other: any

    Returns boolean

isNegative

  • isNegative(target: any): boolean
  • 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

    Parameters

    • target: any

    Returns boolean

isObjectLike

  • isObjectLike(target: any): boolean
  • 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 }

    `

    Parameters

    • target: any

    Returns boolean

isPlainObject

  • isPlainObject(target: any): boolean
  • detemine if target is a plain object

    isPlainObject({ a: 1 }) // => true
    isPlainObject(new Promise(() => {})) // false

    Parameters

    • target: any

    Returns boolean

isPositive

  • isPositive(target: any): boolean
  • 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

    Parameters

    • target: any

    Returns boolean

isPrimitive

  • isPrimitive(target: any): boolean
  • detemine if target is primitive type, such as 1, "foobar"

    Parameters

    • target: any
      
      isPrimitive(1) // => true
      isPrimitive(null) // => true
      isPrimitive({}) // => false
      

    Returns boolean

isThenable

  • isThenable(target: any): boolean
  • detemine if target is a thenable. it can be easily combined with asynchronous methods

    you can checkout what is thenable here

    Parameters

    • target: any
      if (isThenable(obj)) {
       obj.then(callback)
      }
      
      async foo() {
       if (isThenable(obj)) {
         await obj
       }
      }

    Returns boolean

isTypedArray

  • isTypedArray(target: any): boolean
  • determine if the target is an typed array, like Uint8Array, Int8Array

    isTypedArray(new Uint8Array(16)) // => true
    isTypedArray([]) // => false

    Parameters

    • target: any

    Returns boolean

isValidNumber

  • isValidNumber(target: any): boolean
  • detemine if target is a number but NaN.

    isValidNumber('string') // => false
    isValidNumber(new Number(11)) // => true
    isValidNumber(11) // => false
    isValidNumber('11') // => false

    Parameters

    • target: any

    Returns boolean

type

  • type(obj: any, boxedOnly?: boolean): string
  • return type of target. the return value will follow the following rules.

    • primitive return lowercase word
       type('string') // => 'string'
       type(1) // => 'number'
       type(null) // => 'null'
       type(undefined) // => 'undefined'
    • other types return is capitalization constructor name
       type(new Set([1, 2, 3])) // => 'Set'
       type(new Number(111)) // => 'Number'

    with boxed only

    type(1, true) // => 'Number'
    type(null, true) // => 'Null'

    Parameters

    • obj: any
    • Default value boxedOnly: boolean = false

      boxedOnly means return will always return boxed constructor name if target is primitive

    Returns string

Generated using TypeDoc