I'm looking for a JS method that will turn snake_case
into PascalCase
while keeping slashes intact.
// 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
Here's what I've got so far:
_snakeToPascal(string){
return string.replace(/(_w)/g, (m) => {
return m[1].toUpperCase();
});
}
Any advice is appreciated!
EDIT - Solution found
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. 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);
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…