Nodejs String Camel Case camelCase()

Here you can find the source of camelCase()

Method Source Code

// Write simple .camelcase method (camel_case function in PHP) for strings. All
// words must have their first letter capitalized without spaces.

// For instance://from w w w.j a v a 2s  . c o  m
// "hello case".camelCase() => HelloCase
// "camel case word".camelCase() => CamelCaseWord

String.prototype.camelCase = function () {
  var splitArr = this.split(' ');
  var output = '';
  for (var i=0; i<splitArr.length; i++) {
    if (splitArr[i].match(/^[A-Z]+$/i)) {
      output = output + splitArr[i][0].toUpperCase() + splitArr[i].slice(1);
    }
  }
  return output;
};
console.log("test case".camelCase(), "TestCase");
console.log("camel case method".camelCase(), "CamelCaseMethod");
console.log("say hello ".camelCase(), "SayHello");
console.log(" camel case word".camelCase(), "CamelCaseWord");
console.log("".camelCase(), "");

Related

  1. camelCase()
    String.prototype.camelCase=function(){
        var a = this.split(' ');
        return a.map(function(val){
          return val.replace(/^[a-z]/, camelCaseFirstLetter);
        }).join('');
    function camelCaseFirstLetter(a){
      return a.toUpperCase();
    
  2. camelCase()
    String.prototype.camelCase=function(){
      var strArr = Array.from(this).join('').split(' ');
      return strArr.map( word => {
        var uppCase = word.charAt(0).toUpperCase();
        var replaced = word.charAt(0);
        word = word.replace( replaced, uppCase );
        return word;
      }).join('');
    
  3. camelCase()
    String.prototype.camelCase=function(){
        return this.split(' ').map((word)=>{return word.charAt(0).toUpperCase() + word.slice(1)}).join('')
    
  4. camelCase()
    String.prototype.camelCase=function(){
    console.log(this);
       return this.length == 0 ? "" :this.trim()
                                     .split(" ")
                                       .map(a => a[0].toUpperCase()+a.substring(1,a.length))
                                        .join("");
    
  5. camelCase()
    String.prototype.camelCase = function () {
      return this
        .split(' ')
        .map((word) =>
          word
            .split('')
            .map( (letter, index) => (index === 0)
             ? letter.toUpperCase()
             : letter )
    ...
    
  6. camelCaseDash()
    String.prototype.camelCaseDash = function() {
      return this.replace( /(\-[a-z])/g, function( $1 ) {return $1.toUpperCase().replace( "-", "" );} );
    };
    
  7. camelCaseToNormalText()
    'use strict';
    String.prototype.camelCaseToNormalText = function () {
      return this.replace(/([A-Z])/g, ' $1').replace(/^./, function (str) {
        return str.toUpperCase();
      });
    };
    angular.module('uiTreeGrid', []);
    angular.module('uiTreeGrid').value('uiGridConfig', {});
    
  8. camelCaseToSpineCase()
    String.prototype.camelCaseToSpineCase = function() {
        var input = this;
        var newInput = input.replace(/\s/g, "-").replace(/([A-Z])/g, "-$1").toLowerCase();
        var inputNoRepeats = newInput.replace(/([-])\1*/g, "-"); 
        return inputNoRepeats;
    
  9. camelCaseToWords()
    String.prototype.camelCaseToWords = function(){
      return this.split(/(?=[A-Z])/).join(" ");
    };