codewars--js--Convert all the cases!

Problem Description:

In this kata, you will make a function that converts between camelCasesnake_case, and kebab-case.

You must write a function that changes to a given case. It must be able to handle all three case types:

js> changeCase("snakeCase", "snake")
"snake_case"
js> changeCase("some-lisp-name", "camel") "someLispName" js> changeCase("map_to_all", "kebab") "map-to-all" js> changeCase("doHTMLRequest", "kebab") "do-h-t-m-l-request" js> changeCase("invalid-inPut_bad", "kebab") undefined js> changeCase("valid-input", "huh???") undefined js> changeCase("", "camel") ""

My train of thought:

First filter out the ones that do not meet the requirements, and return undefined;

Then through switch(targetCase), convert them respectively.

my answer:

Passed as 171/174. The problem occurs when the part that does not meet the requirements cannot be identified.

function changeCase(identifier, targetCase){

            // Your code here!
            var a=["snake","camel","kebab"];
            var id=identifier.split("");
            if(identifier==""){return "";}
            if(a.indexOf(targetCase)=="-1"|| (identifier.indexOf("-")!=-1 &&identifier.indexOf("_")!=-1 || (identifier.indexOf("-")!=-1 && identifier.indexOf(/A-Z/)!=-1) ||(identifier.indexOf(/A-Z/)!=-1 && identifier.indexOf("_")!=-1))){return undefined;}
            switch(targetCase)
            {
            case "snake":
                for(var i=0;i<id.length;i++){
                    if(/[A-Z]/.test(id[i])){
                        id.splice(i,0,"_",id[i+1].toLowerCase());
                        i++;
                    }
                    if(/[-]/.test(id[i])){
                        id.splice(i,1,'_')
                    }
                }
                return id.join("");
                break;
            case "camel":
                for(var i=0;i<id.length;i++){
                    if(/-/.test(id[i]) || /_/.test(id[i])){
                        id.splice(i,2,id[i+1].toUpperCase());
                        //i=i-1;
                    }
                }
                return id.join("");
                break;
            case "kebab":
                for(var i=0;i<id.length;i++){
                    if(/[A-Z]/.test(id[i])){
                        id.splice(i,0,"-");
                        i++;
                    }
                    if(/[-]/.test(id[i])){
                        id.splice(i,1,'-')
                    }
                }
                return id.join("");
                break;
            }
        }

 

Excellent answer:

(1)

function changeCase(identifier, targetCase){
 if(!/^$|^[a-z]+(([A-Z][a-z]*)+|(-[a-z]+)*|(_[a-z]+)*)$/.test(identifier)) return undefined; 
  switch(targetCase){
    case 'snake': return identifier.replace(/-([a-z])|([A-Z])/g, (_,x,y) => '_'+(x||y.toLowerCase()));
    case 'camel': return identifier.replace(/[-_]([a-z])/g, (_,x) => x.toUpperCase());
    case 'kebab': return identifier.replace(/_([a-z])|([A-Z])/g, (_,x,y) => '-'+(x||y.toLowerCase()));
    default: return undefined;
  }
}

(2)

function changeCase(i, t){
    if ( ( /[AZ]/.test(i) + /[_]/.test(i) + /[-]/.test(i)) > 1 )   //this It's easy to understand, when any two of [AZ], -, _, and three appear, return undefined
     return undefined;
   else  if (t == "snake" ) 
     return i.replace(/[AZ]/g,a= >'_'+a.toLowerCase()).replace(/-([az])/g,(_,a)=> '_'+ a); //First uppercase to "_"+lowercase, then Convert all -x to _+ lowercase
   else  if ( t== 'camel' ) 
     return i.replace(/_/g,'-').replace(/-([az])/g,(_,a )=> a.toUpperCase()); //Turn _ to - first, then capitalize the first letter after -
   else  if (t== 'kebab' )
     returni.replace(/_/g,'-').replace(/[AZ]/g,a=>'-'+ a.toLowerCase()); //First _ turn to -, then lowercase uppercase letters 
}

(3) It is suggested that this is convenient and easy to understand. The first two rules are slightly complicated and difficult to understand.

Separate all words into an array.

When needed, connect in snake/kebab/camel format.

function changeCase(identifier, targetCase){

  if (identifier === '') {
    return '';
  }
  if (/^[a-z-]+$/.test(identifier)) {
    identifier = identifier.split('-');
  }
  else if (/^[a-z_]+$/.test(identifier)) {
    identifier = identifier.split('_');
  }
  else if (/^[a-z][A-Za-z]+$/.test(identifier)) {
    identifier = identifier.replace(/([A-Z])/g, ' $1').toLowerCase().split(' ');
  }  
  else {
    return undefined;
  }  
  
  switch (targetCase) {
  
    case 'snake' : identifier = identifier.join('_'); break;
    case 'kebab' : identifier = identifier.join('-'); break;
    case 'camel' : identifier = identifier.map( (x,i) => i === 0 ? x : x[0].toUpperCase() + x.slice(1)).join(''); break;
    
    default : return undefined
  }
  

 

Hahaha!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324633328&siteId=291194637