Advertisement
stoianpp

RegionsCasting

May 13th, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var text = "We are <mixcase>living</mixcase> in a <upcase>yellow submarine</upcase>." +
  2. " We <mixcase><upcase>don't</upcase></mixcase> have <lowcase>anything</lowcase> else.";
  3.  
  4. var closeIndex = text.indexOf("</");
  5. while (closeIndex >= 0) {
  6.     text = implementTags(text, closeIndex);
  7.     closeIndex = text.indexOf("</");
  8. }
  9. console.log(text);
  10.  
  11. function implementTags(str, closeStartIndex) {
  12.     var tag, tagLen, indexOpenBracketStart, closeEndIndex, textToReplace;
  13.     closeEndIndex = str.indexOf(">", closeStartIndex);
  14.     tag = str.substring(closeStartIndex + 2, closeEndIndex);
  15.     tagLen = tag.length;
  16.     indexOpenBracketStart = (str.substring(0, closeStartIndex)).lastIndexOf("<" + tag + ">");
  17.     textToReplace = str.substring(indexOpenBracketStart, closeEndIndex+1);
  18.     return str.replace(textToReplace, textManipulation(textToReplace, tag, tagLen));
  19. }
  20.  
  21. function textManipulation(text, tagType, len) {
  22.     var replacementText = text.substring(len + 2, text.length - len - 3);
  23.     switch (tagType) {
  24.         case "upcase": replacementText = replacementText.toUpperCase(); break;
  25.         case "lowcase": replacementText = replacementText.toLowerCase(); break;
  26.         case "mixcase": replacementText = executeMixcase(replacementText); break;
  27.     }
  28.     return replacementText;
  29. }
  30.  
  31. function executeMixcase(text) {
  32.     var result = "",
  33.         option = 0;
  34.     for (var i = 0; i < text.length; i++) {
  35.         option = Math.random();
  36.         result += (option < 0.5 ? text[i].toLowerCase() : text[i].toUpperCase());
  37.     }
  38.     return result;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement