[英]Javascript method for changing snake_case to PascalCase
I'm looking for a JS method that will turn snake_case
into PascalCase
while keeping slashes intact.我正在寻找一种 JS 方法,它可以将
snake_case
转换为PascalCase
同时保持斜线完整。
// examples:
post -> Post
admin_post -> AdminPost
admin_post/new -> AdminPost/New
admin_post/delete_post -> AdminPost/DeletePost
etc.等。
I have something that will turn snake_case
into camelCase
and preserve slashes, but I'm having trouble converting this for PascalCase
我有一些东西可以将
snake_case
变成camelCase
并保留斜线,但是我无法将其转换为PascalCase
Here's what I've got so far:这是我到目前为止所得到的:
_snakeToPascal(string){
return string.replace(/(_\w)/g, (m) => {
return m[1].toUpperCase();
});
}
Any advice is appreciated!任何建议表示赞赏!
Here is what I ended up using.这是我最终使用的。 If you use this be mindful that I'm using
this._upperFirst
since I'm using it in a class.如果您使用它,请注意我使用的是
this._upperFirst
因为我在课堂上使用它。 It's kinda greasy but it works.它有点油腻,但它有效。
_snakeToPascal(string){
return string.split('_').map((str) => {
return this._upperFirst(
str.split('/')
.map(this._upperFirst)
.join('/'));
}).join('');
}
_upperFirst(string) {
return string.slice(0, 1).toUpperCase() + string.slice(1, string.length);
}
Here's a solution that preserves slashes and converts snake_case to PascalCase like you want.这是一个保留斜杠并将snake_case 转换为PascalCase 的解决方案。
const snakeToPascal = (string) => {
return string.split("/")
.map(snake => snake.split("_")
.map(substr => substr.charAt(0)
.toUpperCase() +
substr.slice(1))
.join(""))
.join("/");
};
It first splits the input at the '/'
characters to make an array of snake_case strings that need to be transformed.它首先在
'/'
字符处拆分输入以生成需要转换的 snake_case 字符串数组。 It then splits those strings at the '_'
characters to make an array of substrings.然后它在
'_'
字符处拆分这些字符串以生成子字符串数组。 Each substring in this array is then capitalized, and then rejoined into a single PascalCase string.然后将此数组中的每个子字符串大写,然后重新加入单个 PascalCase 字符串。 The PascalCase strings are then rejoined by the
'/'
characters that separated them.然后 PascalCase 字符串由分隔它们的
'/'
字符重新连接。
const snakeToCamel = str => str.replace( /([-_]\\w)/g, g => g[ 1 ].toUpperCase() ); const snakeToPascal = str => { let camelCase = snakeToCamel( str ); let pascalCase = camelCase[ 0 ].toUpperCase() + camelCase.substr( 1 ); return pascalCase; } console.log( snakeToPascal( "i_call_shop_session" ) );
Input : i_call_shop_session输入:i_call_shop_session
Output : ICallShopSession输出:ICallShopSession
This should do the trick.这应该可以解决问题。
function _snake2Pascal( str ){
str +='';
str = str.split('_');
for(var i=0;i<str.length;i++){
str[i] = str[i].slice(0,1).toUpperCase() + str[i].slice(1,str[i].length);
}
return str.join('');
}
edit:编辑:
a version that passes all your test cases shown in the OP:通过 OP 中显示的所有测试用例的版本:
function snake2Pascal( str ){
str +='';
str = str.split('_');
function upper( str ){
return str.slice(0,1).toUpperCase() + str.slice(1,str.length);
}
for(var i=0;i<str.length;i++){
var str2 = str[i].split('/');
for(var j=0;j<str2.length;j++){
str2[j] = upper(str2[j]);
}
str[i] = str2.join('');
}
return str.join('');
}
Or something like that:或类似的东西:
function snake2CamelCase(string) { return string .replace( /_(\\w)/g, ($, $1) => $1.toUpperCase() ) ; } function snake2PascalCase(string) { let s = snake2CamelCase(string); return `${s.charAt(0).toUpperCase()}${s.substr(1)}`; } [ 'something_went_wrong', 'thisIs_my_snakecase' ] .map(s => ({[s]: snake2PascalCase(s)})) .forEach((s, i) => console.log(i, s)) ;
const toString = (snake_case_str) => {
const newStr = snake_case_str.replace(/([-_][a-z])/gi, ($1) => {
return $1.toUpperCase().replace('-', ' ').replace('_', ' ');
});
let changedStr =
newStr.slice(0, 1).toUpperCase() + newStr.slice(1, newStr.length);
return changedStr;
};
let str = 'first_name';
console.log(toString(str));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.