//驼峰函数转换 insert-before ==> insertBefore
const cacheStringFunction = (fn) => {
const cache = Object.create(null);
return ((str) => {
const hit = cache[str];
return hit || (cache[str] = fn(str));
});
};
const camelizeRE = /-(\w)/g;
const camelize = cacheStringFunction((str) => {
return str.replace(camelizeRE, (_, c) => (c ? c.toUpperCase() : ''));
});
//首字母进行大写 insert == > Insert
const capitalize = cacheStringFunction((str) => str.charAt(0).toUpperCase() + str.slice(1));
// var result = camelize('insert-derf')
// console.log(result)
var result = capitalize('insert')
console.log(result)
js 驼峰函数转换以及首字母大写函数
最新推荐文章于 2024-06-04 10:27:08 发布