isEmpty 判空函数 内部分别判断是 null 空数组 等

import { oneOf, isEmpty } from '@/libs/tools'

export const isEmpty = (value) => {
  if (value == null) {
    return true
  }
  if (isArrayLike(value)) {
    return !value.length
  } else if (isPlainObject(value)) {
    for (let key in value) {
      if (hasOwnProperty.call(value, key)) {
        return false
      }
    }
    return true
  }
  return false
}

export const isArrayLike = (value) => {
  return value != null && isLength(value.length) && !isFunction(value)
}

export const isPlainObject = (obj) => {
  return Object.prototype.toString.call(obj) === '[object Object]'
}

export const isLength = (value) => {
  return typeof value === 'number' && value > -1 && value % 1 === 0 && value <= Number.MAX_SAFE_INTEGER
}

export const isFunction = (value) => {
  return Object.prototype.toString.call(value) === '[object Function]'
}

 

posted @ 2019-10-09 10:08  彭成刚  阅读(539)  评论(0编辑  收藏  举报