isEmpty.ts 606 Bytes
Newer Older
hucy's avatar
hucy committed
1 2 3
export const isEmpty = function (data: any) {
  if (data === '' || data === null || data === undefined) {
    return true;
hucy's avatar
hucy committed
4 5 6
  }
  // [] {} 0 false/true
  else {
hucy's avatar
hucy committed
7
    const typeofs = Object.prototype.toString.call(data);
hucy's avatar
hucy committed
8
    // 数组
hucy's avatar
hucy committed
9 10 11 12 13 14
    if (typeofs === '[object Array]') {
      if (data.length > 0) {
        return false;
      } else {
        return true;
      }
hucy's avatar
hucy committed
15 16 17
    }
    // 对象
    else if (typeofs === '[object Object]') {
hucy's avatar
hucy committed
18 19 20 21 22
      if (Object.keys(data).length > 0) {
        return false;
      } else {
        return true;
      }
hucy's avatar
hucy committed
23 24 25
    } else {
      // 不为空
      return false;
hucy's avatar
hucy committed
26 27 28
    }
  }
};