JavaScript RegExp to CamelCase a hyphened CSS property

后端 未结 6 1237
心在旅途
心在旅途 2021-02-19 12:47

I am trying to change CSS properties like this one.

-moz-border-radius

To the JavaScript CSS property like so.

MozBorderRadius
         


        
6条回答
  •  忘掉有多难
    2021-02-19 13:24

    is also possible use indexOf with recursion for that task.

    input some-foo_sd_dsd-weqe
    output someFooSdDsdWeqe
    

    but is works slower

    MozBorderRadius
    test1: 3.535ms
    

    code:

    function camelCased (str) {
    
            function check(symb){
    
                let idxOf = str.indexOf(symb);
                if (idxOf === -1) {
                    return str;
                }
    
                let letter = str[idxOf+1].toUpperCase();
                str = str.replace(str.substring(idxOf+1,idxOf+2), '');
                str = str.split(symb).join(idxOf !== -1 ? letter : '');
    
                return camelCased(str);
            }       
    
            return check('_') && check('-');
    
        }
    
    console.log(camelCased ('-moz-border-radius'));
    

提交回复
热议问题