Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I covert kebab-case into PascalCase?

How could I convert this to PascalCase and to camelCase?

var text = "welcome-to-a-New-day";
toPascalCase(text); // "WelcomeToANewDAY"
toCamelCase(text); // "WelcomeToANewDAY"
like image 535
Benedict Ng-Wai Avatar asked Feb 12 '19 13:02

Benedict Ng-Wai


People also ask

How do you convert a kebab case to a Camelcase?

slice(1). toLowerCase()); let capitalString = capital. join(""); console. log(capitalString); } camelize("my-kebab-string");

What is kebab case example?

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 .

What is the difference between camel case and Pascal case?

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.


1 Answers

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();
}
like image 180
nick zoum Avatar answered Sep 30 '22 15:09

nick zoum