ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

FirstCharToUpper

Contact for email:ugurdemirel@bayburt.edu.tr

Source

let FirstCharToUpper = function () {
    let value, spc, sepet,
        sk = true,
        st = arguments.length,
        ret = '';
    if (st > 0 && typeof arguments[0] === 'string')
    {
        if (st === 1 || (st > 1 && typeof arguments[1] === 'boolean')) {
            value = arguments[0];
            spc = st === 1 ? true : arguments[1];
            sepet = st > 2 ? Array.from(arguments).slice(2) : [];
        }
        else if (st > 1 && typeof arguments[1] === 'string') {
            value = arguments[0];
            spc = true;
            sepet = Array.from(arguments).slice(1);
        }
        else { sk = false; }
        if (!sk) { ret = ''; }
        else {
             let tk = false;
            sepet = spc ? [' '].concat(sepet) : sepet;
            for (let i = 0; i < value.length; i++) {
                if (i === 0) { ret= ret+value.charAt(i).toUpperCase(); }
                else {
                    if(tk && value.charAt(i) === ' '){
                       ret = ret + ' ';
                       continue;
                    }
                    if(sepet.indexOf(value.charAt(i)) > -1) {
                       tk = true;
                       ret = ret + value.charAt(i);
                       continue;
                    }
                    switch (tk){
                      case true:
                          ret = ret + value.charAt(i).toUpperCase();
                          tk = false;
                      break;
                      default:
                          ret = ret + value.charAt(i).toLowerCase();
                      break;
                    }
                }
            }
        }
    }
    return ret;
};

Example

FirstCharToUpper('lorem sit amet,consectetur elit:duis risus.') => 'Lorem Sit Amet,consectetur Elit:duis Risus.'

Author: Uğur Demirel

Submitted on: 19 dec. 2019

Language: javascript

Type: String

Views: 3403