前端面试题:驼峰体与匈牙利语法的相互转换

 1 /* 
 2             驼峰体转匈牙利
 3         */
 4         function TfToXyl(params) {
 5             if (typeof params === 'string') {
 6                 let reg = /^[A-Z]+$/
 7                 let strArr = params.split('')
 8                 let result;
 9                 for (let i = 0; i < strArr.length; i++) {
10                     if (reg.test(strArr[i]) === true) {
11                         strArr[i] = `_${strArr[i].toLowerCase()}`
12                     }
13                 }
14                 result = strArr.join('').slice(1)
15                 return result
16             } else {
17                 throw new Error(`${params} is not a string`)
18             }
19         }
/* 
           匈牙利转驼峰体
        */
        function xylToTf(params) {
            if (typeof params === 'string') {
                let result = [];
                params.split('_').forEach(e => {
                    e = `${e.charAt(0).toUpperCase()}${e.slice(1)}`
                    result.push(e)
                })
                result = result.join('')
                return result
            } else {
                throw new Error(`${params} is not a string`)
            }
        }
原文地址:https://www.cnblogs.com/blucesun/p/13183027.html