小驼峰命名和下划线命名相互转换

字符串 小驼峰格式转为下划线格式

function stringHumpTurn(val) {
    return val.replace(/([A-Z])/g, "-$1").toLowerCase()
}
let s = "htmlCssJs";
console.log(stringHumpTurn(s), '字符串 小驼峰格式转为下划线格式');

字符串 下划线格式转为小驼峰格式

function stringConnectorTurn(val) {
    if (val.indexOf("-") != -1) {
        return val.replace(/-([a-z])/g, (p, m) => m.toUpperCase())
    }
    if (val.indexOf("_") != -1) {
        return val.replace(/_([a-z])/g, (p, m) => m.toUpperCase())
    }
    return val
}
let s1 = "foo_style_css";
console.log(stringConnectorTurn(s1), '字符串_ 下划线格式转为小驼峰格式');
let s2 = "foo-style-css";
console.log(stringConnectorTurn(s2), '字符串- 下划线格式转为小驼峰格式');

对象 下划线格式转为小驼峰格式

function objHumpTurn(data) {
    if (typeof data != 'object' || !data) return data
    if (Array.isArray(data)) {
        return data.map(item => objHumpTurn(item))
    }
    const newData = {}
    for (let key in data) {
        let newKey = key.replace(/_([a-z])/g, (p, m) => m.toUpperCase())
        newData[newKey] = objHumpTurn(data[key])
    }
    return newData
}
let dataList = [
    {
        user_id: 1,
        dept_id: 103,
        user_name: 'admin',
        nick_name: '张',
        user_type: '00'
    }, {
        user_id: 2,
        dept_id: 105,
        user_name: 'test',
        nick_name: '王',
        user_type: '00'
    }
]
console.log(objHumpTurn(dataList), '对象 下划线格式转为小驼峰格式');

对象 小驼峰格式转为下划线格式

function objConnectorTurn(data) {
    if (typeof data != 'object' || !data) return data
    if (Array.isArray(data)) {
        return data.map(item => objConnectorTurn(item))
    }
    const newData = {}
    for (let key in data) {
        let newKey = key.replace(/([A-Z])/g, (p, m) => `_${m.toLowerCase()}`)
        newData[newKey] = objConnectorTurn(data[key])
    }
    return newData
}

let dataStr = [
    {
      userId: 1,
      deptId: 103,
      userName: 'admin',
      nickName: '张',
      userType: '00'
    },
    {
      userId: 2,
      deptId: 105,
      userName: 'test',
      nickName: '王',
      userType: '00'
    }
]
console.log(objConnectorTurn(dataStr), '对象 小驼峰格式转为下划线格式');

数据对象key 驼峰下划线互相转化

/**
 * 数据对象key 驼峰下划线互相转化
 * @param {Object} data 需要转换的对象
 * @param {String} type hump-转驼峰 toLine-转下划线
 */
const formatHumpLineTransfer = (data, type = 'hump') => {
    if (typeof data != 'object' || !data) return data
    if (Array.isArray(data)) {
        return data.map(item => formatHumpLineTransfer(item, type))
    }
    const newData = {}
    for (let key in data) {
        let newKey = key.replace(/_([a-z])/g, (p, m) => m.toUpperCase())
        if (type === 'toLine') {
            newKey = key.replace(/([A-Z])/g, (p, m) => `_${m.toLowerCase()}`)
        }
        newData[newKey] = formatHumpLineTransfer(data[key], type)
    }
    return newData
}
// 对象 下划线格式转为小驼峰格式
console.log(formatHumpLineTransfer(dataList, 'hump'), '对象 下划线格式转为小驼峰格式');
// 对象 小驼峰格式转为下划线格式
console.log(formatHumpLineTransfer(dataStr, 'toLine'), '对象 小驼峰格式转为下划线格式');

结果

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值