Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert kebab-case to camelCase - Javascript

Say I have a function that transforms kebab-case to camelCase:

camelize("my-kebab-string") === 'myKebabString';

I'm almost there, but my code outputs the first letter with uppercase too:

function camelize(str){
  let arr = str.split('-');
  let capital = arr.map(item=> item.charAt(0).toUpperCase() + item.slice(1).toLowerCase());
  let capitalString = capital.join("");

  console.log(capitalString);
}
    
camelize("my-kebab-string");
like image 307
Robert Hovhannisyan Avatar asked Aug 19 '19 12:08

Robert Hovhannisyan


People also ask

Is Kebab case to camelCase?

Kebab case employs a dash to maximize white space between multiple words, while the camel case naming convention does not use any white space. Instead, camel case uses capitalization to distinguish between multiple words in a variable. Camel case starts each new word in a complex variable name with an uppercase letter.

How do you convert a Stringcase to a camelCase in typescript?

Approach: Use reduce() method to iterate over the character of string and convert it into camel case. The toUpperCase() and toLowerCase() methods are used to convert the string character into upper case and lower case respectively.

What is kebab case JavaScript?

Kebab case -- or kebab-case -- is a programming variable naming convention where a developer replaces the spaces between words with a dash. Programming variable names should be descriptive. Two or more words are often required to properly convey a resource's meaning.


4 Answers

You can also try regex.

camelize = s => s.replace(/-./g, x=>x[1].toUpperCase())

Looks only for hyphen followed by any character, and capitalises it and replaces the hyphen+character with the capitalised character.

like image 199
kanine Avatar answered Oct 23 '22 06:10

kanine


To keep your existing code, I've just added a check on the index that will return item instead of the transformed item if item is 0 (falsy), since the problem is just that you are upper-casing the first item as well, while you shouldn't.

In a nutshell, the inline expression becomes: (item, index) => index ? item.charAt(0).toUpperCase() + item.slice(1).toLowerCase() : item, because:

  • If index is not falsy (so, if index is > 0 in your context), the capitalized string is returned.
  • Otherwise, the current item is returned.

Of course, this could be cleaner and likely single line, but I wanted to stay as close as possible to your code so that you could understand what was wrong:

function camelize(str){
  let arr = str.split('-');
  let capital = arr.map((item, index) => index ? item.charAt(0).toUpperCase() + item.slice(1).toLowerCase() : item.toLowerCase());
  // ^-- change here.
  let capitalString = capital.join("");

  console.log(capitalString);
}

camelize("my-kebab-string");

As a side note, you could've found a potential cleaner answer here: Converting any string into camel case

like image 41
briosheje Avatar answered Oct 23 '22 05:10

briosheje


For lodash users:

_.camelCase('my-kebab-string') => 'myKebabString'
like image 3
Benny67b Avatar answered Oct 23 '22 07:10

Benny67b


The first method is to just transform to lower case the first entry of your capital array, like this:

capital[0] = capital[0].toLowerCase();

Another method, which I think to be more efficient, is to pass another parameter to the map callback, which is the index. Take a look at this for further reading: https://www.w3schools.com/jsref/jsref_map.asp

So you transform to upper case only if (index > 0). Like this:

let capital = arr.map((item, index) => index ? item.charAt(0).toUpperCase() + item.slice(1).toLowerCase() : item);
like image 2
FonzTech Avatar answered Oct 23 '22 06:10

FonzTech