1`驼峰转连字符
export function camelToHyphen(str) {
if (typeof str === 'string') {
return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
} else {
throw 'str must be string'
}
}
2`连字符转驼峰
export function hyphenToCamel(str) {
if(typeof str === 'string') {
return str.replace(/(\-)(\w)/g, function(match, p1, p2) {
return p2.toUpperCase();
})
} else {
throw 'str must be string'
}
}
3` 深度克隆
import { isArray, isObject } from "xxx";
// 检测数组, 对象的方法, 自行拟定
export function deepClone(args) {
let res;
if(isArray(args)) {
res = [];
for(let i = 0; i < args.length; i++) {
res.push(deepClone(args[i]))
}
return res;
} else if(isObject(args)) {
res = {};
for(let key in args) {
if(args.hasOwnProperty(key)) {
res[key] = deepClone(args[key])
}
}
return res;
} else {
return args;
}
}
网友评论