function capitalize(string){
    if ( typeof string == 'string' ){
        // Replaces underscores with spaces
        var replaced = string.replace(/_/gi, " ");
        // adds spaces before in-word uppercase characters
        var result = "";
        for ( var cidx = 0; cidx < replaced.length; cidx++ ){
            var chr = replaced.charAt(cidx);
            var chr_upper = chr.toUpperCase();
            result += ( chr != " " && chr == chr_upper ? ` ${chr}` : chr );
        }
        // converts all letters to lowercase
        // then capitalizes the first letter and every letter following a space character.
        result = result.split(" ");
        for( var widx = 0; widx < result.length; widx++ ){
            var low_word = result[widx].toLowerCase();
            result[widx] = ( low_word.charAt(0).toUpperCase() + low_word.substring(1) );
        }
        result = result.join(" ");
        return result;
    } else {
        throw new Error("Called with Wrong Arguments: Expected string.");
    }
}
// For `capitalize camelCase mixed_with_underscores`, it will return `Capitalize Camel Case Mixed With Underscores`.
For `capitalize camelCase mixed_with_underscores`, it will return `Capitalize Camel Case Mixed With Underscores`.
For ` capitalize camelCase mixed_with_underscores ` it will return ` Capitalize Camel Case Mixed With Underscores ` .
function capitalize(string){
    if ( typeof string == 'string' ){
        var replaced = string.replace(/_/gi, " ");
        var result = "";
        for ( var cidx = 0; cidx < replaced.length; cidx++ ){
            var chr = replaced.charAt(cidx);
            var chr_upper = chr.toUpperCase();
            result += ( chr != " " && chr == chr_upper ? ` ${chr}` : chr );
        }
        result = result.split(" ");
        for( var widx = 0; widx < result.length; widx++ ){
            var low_word = result[widx].toLowerCase();
            result[widx] = ( low_word.charAt(0).toUpperCase() + low_word.substring(1) );
        }
        result = result.join(" ");
        return result;
    } else {
        throw new Error("Called with Wrong Arguments: Expected string.");
    }
}
function capitalize ( string ) { if ( typeof string == 'string' ) { var replaced = string . replace ( / _ / gi , " " ) ; var result = "" ; for ( var cidx = 0 ; cidx < replaced . length ; cidx ++ ) { var chr = replaced . charAt ( cidx ) ; var chr_upper = chr . toUpperCase ( ) ; result += ( chr != " " && chr == chr_upper ? ` ${ chr } ` : chr ) ; } result = result . split ( " " ) ; for ( var widx = 0 ; widx < result . length ; widx ++ ) { var low_word = result [ widx ] . toLowerCase ( ) ; result [ widx ] = ( low_word . charAt ( 0 ) . toUpperCase ( ) + low_word . substring ( 1 ) ) ; } result = result . join ( " " ) ; return result ; } else { throw new Error ( "Called with Wrong Arguments: Expected string." ) ; } }
For `capitalize camelCase mixed_with_underscores`, it will return `Capitalize Camel Case Mixed With Underscores`.
For ` capitalize camelCase mixed_with_underscores ` it will return ` Capitalize Camel Case Mixed With Underscores ` .
// Replaces underscores with spaces // adds spaces before in-word uppercase characters // converts all letters to lowercase // then capitalizes the first letter and every letter following a space character.