How could I convert this to PascalCase
and to camelCase
?
var text = "welcome-to-a-New-day";
toPascalCase(text); // "WelcomeToANewDAY"
toCamelCase(text); // "WelcomeToANewDAY"
slice(1). toLowerCase()); let capitalString = capital. join(""); console. log(capitalString); } camelize("my-kebab-string");
Kebab case is similar to snake case, but you use a hyphen (-) instead of an underscore (_) to separate the words. Here are some examples of kebab case: first-name and last-name .
Camel case and Pascal case are similar. Both demand variables made from compound words and have the first letter of each appended word written with an uppercase letter. The difference is that Pascal case requires the first letter to be uppercase as well, while camel case does not.
A fully ES5 compatible way to do this is, to find all the dashes that are followed by an alphanumeric character using this simple regex /-\w/g
. Then just remove the dash and uppercase the character.
The same can be done for pascal case just by also checking for the first character in the string using ^\w|-\w
. The rest is the same.
Here are a couple of examples:
console.log(toCamelCase("welcome-to-a-New-day"));
console.log(toPascalCase("welcome-to-a-New-day"));
console.log(toCamelCase("bsd-asd-csd"));
console.log(toPascalCase("bsd-asd-csd"));
function toCamelCase(text) {
return text.replace(/-\w/g, clearAndUpper);
}
function toPascalCase(text) {
return text.replace(/(^\w|-\w)/g, clearAndUpper);
}
function clearAndUpper(text) {
return text.replace(/-/, "").toUpperCase();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With