How do I covert kebab-case into PascalCase?
Asked Answered
L

8

15

How could I convert this to PascalCase and to camelCase?

var text = "welcome-to-a-New-day";
toPascalCase(text); // "WelcomeToANewDAY"
toCamelCase(text); // "WelcomeToANewDAY"
Latrice answered 12/2, 2019 at 13:28 Comment(8)
did you try anything yet?Forman
was trying RegExLatrice
you can look at function called split and see if that can help youForman
{pascal: WelcomeToANewDAY, Why is DAY not Day ?Patristic
Possible duplicate of #2971025Becker
Day to day is a type mistakeLatrice
Possible duplicate of Convert string to Pascal Case (aka UpperCamelCase) in JavascriptDomini
Possible duplicate of Converting any string into camel caseMenon
D
39

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();
}
Domini answered 12/2, 2019 at 13:35 Comment(0)
P
2

You do that using replace and RegExp

let string = "welcome-to-a-New-day"
function toPascalCase(str){
  let arr = str.split('-');
  let last = arr[arr.length - 1];
  str = str.replace(/-\w/g,(x) => `${x[1].toUpperCase()}`)
  str = str[0].toUpperCase() + str.substring(1,str.length - last.length) + last.toUpperCase();
  return str;
}
function toCamelCase(str){
   return str.replace(/-\w/g,(x) => `${x[1].toUpperCase()}`)
}
console.log(toPascalCase(string))
console.log(toCamelCase(string))
Prescience answered 12/2, 2019 at 13:41 Comment(0)
B
2

There you go

    function capitalizeFirstLetter(string) {
        return string.charAt(0).toUpperCase() + string.slice(1);
    }

    function getPascalFromSnake(input){
      return input.split("-").map(capitalizeFirstLetter).join("")
    }

    function getCamelFromSnake(input){
      var toReturn = getPascalFromSnake(input);
      return toReturn.charAt(0).toLowerCase() + toReturn.slice(1);
    }
Bifarious answered 12/2, 2019 at 13:41 Comment(0)
P
1

const myString = 'welcome-to-a-New-day';

function capitalize(string) {
    // take first character, uppercase it
    // add the rest of the string
    return string.charAt(0).toUpperCase() + string.slice(1);
}

function pascalize (string) {
  // splitting words by dash
  const words = string.split('-')
  // use capitalize function to capitalize every word
  const capitalized = words.map(word => capitalize(word))
  // glue up words with .join()
  return capitalized.join('')
}

function camelize (string) {
  // splitting words by dash
  const words = string.split('-')
  // use capitalize function to capitalize every but first word
  const capitalized = words.map((word, index) => {
    if (index === 0) return word
    return capitalize(word)
  })
  // glue up words with .join()
  return capitalized.join('')
}

console.log(pascalize(myString))
console.log(camelize(myString))
Prolific answered 12/2, 2019 at 13:37 Comment(0)
B
0

Here is a way to do it with string.replace() and a regex:

const camelCase = str => str.replace(/\s*-\s*\w/g, parts => parts[parts.length-1].toUpperCase());
const pascalCase = str => camelCase(str).replace(/^\w/, s => s.toUpperCase());

const cases = str => ({ pascal: pascalCase(str), camel: camelCase(str) });

console.log(cases('welcome-to-a-New-day'));
console.log(cases('welcome -to-a- New - day'));
Bothwell answered 12/2, 2019 at 13:36 Comment(1)
Direct kebab to PascalCase alternative - str.replace(/(^\w)|(-\w)/g, (gr0, gr1, gr2) => gr1?.toUpperCase() ?? gr2[gr2.length-1].toUpperCase())Xerosere
T
0
function getCase(str) {
  let arr= str.split("-");

  return arr.reduce((acc, word,i)=>{
      let lower = word.toLowerCase(),
            cap = lower.slice(0,1).toUpperCase() + lower.slice(1);

      return {
         camel: i === 0 ? lower : acc.camel + cap,
         pascal: acc.pascal + cap
     }
  },{camel:"",pascal:""})
}
Troglodyte answered 12/2, 2019 at 13:47 Comment(0)
H
0

@Nick's answer, just in one function:

function toPascalCase(text) {
  return text.replace(/(^\w|-\w)/g, (text) => text.replace(/-/, "").toUpperCase());
}
Halation answered 18/3, 2023 at 8:57 Comment(0)
S
0

First put a dash in front of the string, and then replace every -\w with just the uppercase character, omitting the dash:

function pascalCase(str) {
  return `-${str}`.replace(/-\w/g, (c) => c[1].toUpperCase())
}
Slyke answered 19/11, 2023 at 13:54 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.