import { isEmpty } from './isEmpty';

type IndexKey = string | number | symbol;
interface AnyObject {
  [propName: IndexKey]: any;
}

/**
 * @description 去除对象中为空的属性值 返回一个新对象
 * @param {AnyObject} data
 * @return 返回一个新对象
 */
export const omitEmpty = function (data: AnyObject) {
  const typeofs = Object.prototype.toString.call(data);
  if (typeofs === '[object Object]') {
    const obj: AnyObject = {};
    for (const key in data) {
      if (!isEmpty(data[key])) {
        obj[key] = data[key];
      }
    }
    return obj;
  } else {
    console.error(`Expect type [object Object], but get type ${typeofs}`);
    return data;
  }
};
