{

javaScript How to convert snake camel case for a string


In this blog post, we are going to discuss snake/camel case tutorials with examples in javascript or typescript.

Convert snake case to camel case in typescript:

The string is a group of characters that contains word(s).

snake case text is a combination of words that are separated by an underscore.

for example

hello_world is a snake case string example.

Important points about snake case sentences

  • no spaces or punctuation in between words
  • words are separated by an underscore.
  • these are used in declaring variables and method names in some programming languages
  • the first letter can be either uppercase or lowercase, but the conventional approach is for the first letter to be lowercase.

camel cases are string words in which the first letter is lower case and the middle words are upper case.

example camel case strings are firstName, lastName

Important points about camel case sentences

  • spaces and punctuation are not allowed
  • It will be used in defining variable names in programming languages like java.

camel cases and snake cases are compound words.

Conversion of a camel to a snake is a normal task for developers.

typescript is a superset of javascript, so javascript code works in typescript.

We can do this in many ways, one of the approaches is using map() function, other using regular expression.

using map() method

This approach is using two functions.

  • given snake case string input is first_name
  • split string into words by separator underscore
  • Iterate each word, returns the first word, from second-string onwards apply firstUppercase function
  • firstUppercase function is to convert the first letter to uppercase and the remaining string is converted to lower case
let snakeCaseString = "first_name"  
const snakeToCamelCase = sentence => sentence  
    .split('_')  
    .map((word, index) => {  
        if (word === 0) {  
            return part.toLowerCase();  
        }  
        return firstUppercase(word);  
    })  
    .join('');  
const firstUppercase = word => word &&  
    word.charAt(0).toUpperCase() + word.slice(1);  
  
console.log(snakeCaseString);  
console.log(snakeToCamelCase(snakeCaseString));  

Output is

first_name  
FirstName  

Using regular expression

Simply, using regular expression and performance is best compared with the map() function.

  • Given a string snake case string
  • separate words by separator underscore(_) using regular expression make the second-word first character to upper case.
snakeToCamel = snakeCaseString => snakeCaseString.replace(/([-_]\w)/g, g => g[1].toUpperCase())  
console.log(snakeToCamel(snakeCaseString));  

Output is

  
first_name  
firstName  

Please like and share your feedback if you like this post.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.