Wednesday, February 27, 2019

Turn the text value of a TypeScript enum back into something with some spaces in it.

The backflips below account for acronyms.

private GetFriendlyRoleName():string {
   let betterName:string = "";
   if (this.user) {
      let spacelessName:string = Role[this.user.Role];
      let counter = 0;
      while (counter < spacelessName.length){
         if(counter > 0){
            if (spacelessName.charAt(counter) ==
                  spacelessName.charAt(counter).toUpperCase()){
               if (spacelessName.charAt(counter-1) == spacelessName.charAt(counter-
                     1).toUpperCase()){
                  if (counter + 1 < spacelessName.length) {
                     if (spacelessName.charAt(counter+1) ==
                           spacelessName.charAt(counter+1).toUpperCase()){
                        betterName = betterName + spacelessName.charAt(counter);
                     } else {
                        betterName = betterName + " " + spacelessName.charAt(counter);
                     }
                  } else {
                     betterName = betterName + spacelessName.charAt(counter);
                  }
               } else {
                  betterName = betterName + " " + spacelessName.charAt(counter);
               }
            } else {
               betterName = betterName + spacelessName.charAt(counter);
            }
         } else {
            betterName = betterName + spacelessName.charAt(counter);
         }
         counter++;
      }
   }
   return betterName;
}

No comments:

Post a Comment