Turning a camel into a camel conversion

I wrote a function for lines with a camel casing (the requirement is to expand the characters at the beginning of the word and after each hyphen that targets personal names).

function sadCamelize(input) { return input.toLowerCase().replace(/([-\s])(.)/g, function(match, separator, starter) { return separator + starter.toUpperCase(); }); } 

Now I would like to make my camel happy so that even the first character of the line (without succeeding a space or hyphen) is not hacked. Id est, not:

Honka-Honka → honka-Honka

I would like to get

Honka-Honka → Honka-Honka

I'm stuck at the moment, perhaps due to annoyance. All camels are suppressed, and so am I. - What is the correct nomenclature of what I call a sad / happy camel (head down / up)?

+6
source share
3 answers
 function happyCamelize(str) { return str.replace(/([az])([az]+)/gi, function(_, $1, $2) { // _: The entire matched string. not used here. // $1: The first group. The first alphabet. // $2: The second group. The rest alphabets. return $1.toUpperCase() + $2.toLowerCase(); }); } 

Example:

 happyCamelize('HONKA-HONKA') // "Honka-Honka" 

NOTE This code will not change a single line word.

 happyCamelize('h') // => "h" happyCamelize('H') // => "H" 

If you also want to watch a single-slot word, use /([az])([az]*)/gi .

+7
source

This ugly single liner does the job.

 "HONKA-HONKA".toLowerCase().split(/\b/g).map(function(word) { return word[0].toUpperCase() + word.slice(1)}).join('') 

Let me blow it up:

 "HONKA-HONKA".toLowerCase().split(/\b/g).map(function(word) { return word[0].toUpperCase() + word.slice(1); }).join(''); 

Now wrap in function

 function sadCamelize(str) { return str.toLowerCase().split(/\b/g).map(function(word) { return word[0].toUpperCase() + word.slice(1); }).join(''); } 

Here's the sequence of actions:

  • convert everything to lowercase
  • by word (\ b)
  • displays each part of the regular expression result so that the first letter is uppercase
  • word.slice (1) removes the first element from an array
+4
source

The first part of your question was answered by lukas below, so I will answer the second part.

honkaHonka = camelCase

HonkaHonka = PascalCase

There are other options and synonyms. See here: http://en.wikipedia.org/wiki/CamelCase#Variations_and_synonyms

+2
source

Source: https://habr.com/ru/post/955550/


All Articles