/*
1. 转换测试
2. 编写函数
3. 单元测试
*/

import _ from 'lodash'

function snakeCaseObject(originObj) {
  if (!_.isObject(originObj)) {
    return originObj
  }

  const ret = {};

  for (let key in originObj) {
    let val = originObj[key]
    // console.log(key, val)

    // 将key转换为下划线方式
    let keyNew = _.snakeCase(key)
    ret[keyNew] = val
  }

  return ret
}

function camelCaseObject(originObj) {
  if (!_.isObject(originObj)) {
    return originObj
  }

  const ret = {};

  for (let key in originObj) {
    let val = originObj[key]
    // console.log(key, val)

    // 将key转换为下划线方式
    let keyNew = _.camelCase(key)
    ret[keyNew] = val
  }

  return ret
}

function kebabCaseObject(originObj) {
  if (!_.isObject(originObj)) {
    return originObj
  }

  const ret = {};

  for (let key in originObj) {
    let val = originObj[key]
    // console.log(key, val)

    // 将key转换为下划线方式
    let keyNew = _.kebabCase(key)
    ret[keyNew] = val
  }

  return ret
}

// 转换为数据库接受的数据
function convert4db(originObj) {
  if (!_.isObject(originObj)) {
    return originObj
  }

  const ret = {};

  for (let key in originObj) {
    // 数据库中不定义bool类型的数据，转换为1和0
    let val = originObj[key]
    if (_.isBoolean(val)) {
      val = val ? 1 : 0
    }
    // 对象转换为字符串
    if (_.isObject(val)) {
      val = JSON.stringify(val)
    }

    ret[key] = val
  }

  return ret
}

export {
  camelCaseObject,
  snakeCaseObject,
  kebabCaseObject,
  convert4db,
}