14

How could I convert this to PascalCase and to camelCase?

var text = "welcome-to-a-New-day";
toPascalCase(text); // "WelcomeToANewDAY"
toCamelCase(text); // "WelcomeToANewDAY"
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Benedict Ng-Wai
  • 143
  • 1
  • 4

7 Answers7

33

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();
}
nick zoum
  • 7,216
  • 7
  • 36
  • 80
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))
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
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);
    }
Shivam Tiwari
  • 345
  • 3
  • 11
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))
dporechny
  • 638
  • 5
  • 12
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'));
jo_va
  • 13,504
  • 3
  • 23
  • 47
  • Direct kebab to PascalCase alternative - `str.replace(/(^\w)|(-\w)/g, (gr0, gr1, gr2) => gr1?.toUpperCase() ?? gr2[gr2.length-1].toUpperCase())` – Sølve T. Jan 06 '23 at 09:33
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:""})
}
cantuket
  • 1,582
  • 10
  • 19
0

@Nick's answer, just in one function:

function toPascalCase(text) {
  return text.replace(/(^\w|-\w)/g, (text) => text.replace(/-/, "").toUpperCase());
}
Artur Müller Romanov
  • 4,417
  • 10
  • 73
  • 132