export const numberToChinese = (val) => {
    const num = parseInt(val)
    const changeNum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
    const unit = ['', '十', '百', '千', '万']
    const getWan = (temp) => {
        const strArr = temp.toString().split('').reverse()
        let newNum = ''
        for (var i = 0; i < strArr.length; i++) {
            newNum = (i === 0 && strArr[i] === '0' ? '' : i > 0 && strArr[i] === '0' && strArr[i - 1] === '0' ? '' : changeNum[strArr[i]] + (strArr[i] === '0' ? unit[0] : unit[i])) + newNum
        }
        return newNum
    }
    const overWan = Math.floor(num / 10000)
    let noWan = num % 10000
    if (noWan.toString().length < 4) noWan = '0' + noWan

    const chinanum = overWan ? getWan(overWan) + '万' + getWan(noWan) : getWan(num)
    return chinanum
}

/**
 * 下划线转驼峰
 * @param {String} str 
 */
export const toCamelCase = (str) => {
    if (typeof str === 'string') {
        return str.toLowerCase().split('_').map((p, i) => {
            if (i > 0) {
                return p[0].toUpperCase() + p.slice(1)
            } else {
                return p
            }
        }).join('')
    }
    return str
}

/**
 * 驼峰转下划线
 * @param {String} str 
 */
export const toUnderScoreCase = (str) => {
    if (typeof str === 'string') {
        str = str.replace(/[A-Z]/g, (match) => {
            return `_${match}`
        }).toLowerCase()
        return str.startsWith('_') ? str.slice(1) : str
    }
    return str
}