import { isUndefined, isString, isNumber, isBoolean } from 'lodash-es'
export const trimData = (data, exclude = [], include) => {
  if (data === null || data === undefined) {
    return data
  }

  for (const key in data) {
    if (isUndefined(data[key]) || (exclude && exclude.indexOf(key) !== -1)) delete data[key]
    if (include && include.indexOf(key) === -1) delete data[key]
  }
  return data
}
// 检查email格式
export const isEmail = function (val) {
  // 允许汉字、英文字母、数字
  return /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(val)
}

// 是否为空
export const isNullOrUndefined = function (val) {
  return val === null || val === undefined
}

// 转换为boolean
export const toBoolean = function (val) {
  if (isString(val) && (val.toLowerCase() === 'true' || val.toLowerCase() !== '0')) {
    val = true
  } else if (isNumber(val) && val !== 0) {
    val = true
  } else {
    val = isBoolean(val) && val
  }

  return val
}
