js camelCase formatter
js camelCase formatter
驼峰命名 转换器
'A'.charCodeAt();
// 65
'Z'.charCodeAt();
// 90
'a'.charCodeAt();
// 97
'z'.charCodeAt();
// 122
Number.isNaN(+`x`)
// true
Number.isNaN(+`2`)
// false
'tools_finance_costInV2'
=> '/tool/finance/cost-in-v2'
'costInV2'
=> 'cost-in-v2'
solution
const log = console.log;
const camelCaseFormatter = (str = ``, debug = false) => {
let result = '';
for(let item of [...str]) {
if(item.charCodeAt() > 'a'.charCodeAt() || !Number.isNaN(+item)) {
result += item;
} else {
result += `-${item.toLocaleLowerCase()}`;
}
}
if(debug) {
log(`result = `, result);
}
return result;
}
const str = 'costInV2';
// "costInV2"
camelCaseFormatter(str, true);
/*
// node ./camelCase.js
// result = cost-in-v2
*/
demo
id to URL
'tools_finance_costInV2'
=> '/tool/finance/cost-in-v2'
const url = window.location.origin;
// https://app.xgqfrms.xyz
const config = {
key: 'costInV2',
level: 3,
id: 'tools_finance_costInV2',
name: '工具库-财务-消耗录入',
url: '/tool/finance/cost-in-v2',
// url: 'https://app.xgqfrms.xyz/tool/finance/cost-in-v2',
};
regex
refs
https://app.xgqfrms.xyz/index.html
©xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/14366997.html
未经授权禁止转载,违者必究!