krulik
7/28/2015 - 10:06 AM

toCamelCase

function toCamelCase (str) {
    var DASH_OR_SPACE = /-|\s/gi;
    var FIRST_LETTER = /\w/;

    var words = str.split(DASH_OR_SPACE);

    return words
    	.slice(0,1)
        .map(function (word) {
        	return replaceFirstLetter(word, toLower)
        })
        .concat(words.slice(1))
        .reduce(function(previous, current, index) {
            return previous + replaceFirstLetter(current, toUpper);
        });


    function replaceFirstLetter (word, f) {
    	return word.replace(FIRST_LETTER, f);
    }

    function toUpper (letter) {
    	return letter.toUpperCase();
    }

    function toLower (letter) {
    	return letter.toLowerCase();
    }
};