-4

I would like transform string with uppercase to string with underscore like :

 - "blablaBlabla" to "blabla_blabla" 
 - "firstName" to "first_name" 

And conversely :

 - "blabla_blabla" to "blablaBlabla"
 - "first_Name" to "firstName"

I use Typescript, but I think for that, there is no difference with the Javascript.

Thank's in advance.

Jérémy.

adiga
  • 34,372
  • 9
  • 61
  • 83
Jérémy Gachon
  • 252
  • 2
  • 6
  • 27
  • 5
    And what have you tried so far? – Scott Sauyet May 06 '19 at 12:23
  • Hint: useful search words are "camel cast" and "snake case" – Scott Sauyet May 06 '19 at 12:25
  • [This question](https://stackoverflow.com/questions/30521224/javascript-convert-pascalcase-to-underscore-case) asks for the opposite operation, but the process will be the same : using regex match `_` and the following letter which you put into a capturing group, extract the content of this capturing group, translate to uppercase, replace the match by the uppercase. – Aaron May 06 '19 at 12:27

4 Answers4

3

You could get all the upper case letters with [A-Z] and replace the match with _ + m.toLowerCase()

To change it the other way, match all _([a-z]) to get the alphabet to a capturing group. And then use toUpperCase on the capture

function trasnform1(str) {
  return str.replace(/[A-Z]/g, (m) => '_' + m.toLowerCase())
}

function trasnform2(str) {
  return str.replace(/_([a-z])/g, (m, p1) => p1.toUpperCase())
}

console.log(trasnform1("blablaBlabla"))
console.log(trasnform1("firstName"))

console.log(trasnform2("blabla_blabla"))
console.log(trasnform2("first_name"))
adiga
  • 34,372
  • 9
  • 61
  • 83
2

let word = "firstName";
let output = "";

// for conversion
for (let i = 0; i < word.length; i++) {
  if (word[i] === word[i].toUpperCase()) {
    output += "_" + word[i].toLowerCase();
  } else {
    output += word[i];
  }
}
console.log(output);

let source = output;
output = "";

//for reversion
for (let i = 0; i < source.length; i++) {
  if (source[i] === "_") {
    i++;
    output += source[i].toUpperCase();
  } else {
    output += source[i];
  }
}
console.log(output);
adiga
  • 34,372
  • 9
  • 61
  • 83
Shiva
  • 476
  • 3
  • 15
0
// Camel to snake and snake to camel case
function changeCasing(input) {
  if (!input) {
    return '';
  }
  if (input.indexOf('_') > -1) {
    const regex = new RegExp('_.', 'gm');
    return input.replace(regex, (match) => {
      const char = match.replace("_", "");
      return char.toUpperCase();
    });
  } else {
    const regex = new RegExp('[A-Z]', 'gm');
    return input.replace(regex, (match) => {
      return `_${match.toLowerCase()}`;
    });
  }
}
Chirag
  • 13
  • 2
0

This is example written in TypeScript. It's more readable using method chaining.

export const snake2Camel = (snake: string): string => {
  return snake.split('').reduce((prev: string, cur: string) => {
    if (prev.includes("_")) {
      prev = prev.substring(0, prev.length - 1);
      cur = cur.toUpperCase();
    }
    return prev + cur;
  }, "");
};
Ko1103
  • 61
  • 1
  • 3