vue-驼峰和下划线转换

function getHumpLineName (s) {
  return s.replace(/_(\w)/g, function (all, letter) {
    return letter.toUpperCase()
  })
}

function getUnderLineName (s) {
  return s.replace(/([A-Z])/g, '_$1').toLowerCase()
}

export function toHumpLineNameStyle (obj) {
  if (obj instanceof Array) {
    obj.forEach(function (v) {
      toHumpLineNameStyle(v)
    })
  } else if (obj instanceof Object) {
    Object.keys(obj).forEach(function (key) {
      let newKey = getHumpLineName(key)
      if (newKey !== key) {
        obj[newKey] = obj[key]
        delete obj[key]
      }
      toHumpLineNameStyle(obj[newKey])
    })
  }
}

export function toUnderLineNameStyle (obj) {
  if (obj instanceof Array) {
    obj.forEach(function (v) {
      (v)
    })
  } else if (obj instanceof Object) {
    Object.keys(obj).forEach(function (key) {
      let newKey = getUnderLineName(key)
      if (newKey !== key) {
        obj[newKey] = obj[key]
        delete obj[key]
      }
      toUnderLineNameStyle(obj[newKey])
    })
  }
}

posted @ 2021-08-31 14:30  花兒向陽開  阅读(86)  评论(0编辑  收藏  举报