Advertisement
Guest User

fiftyshadesstringmanip

a guest
Jul 1st, 2015
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Word processor obj factory.
  2. var wordProcessor = function(initialData) {
  3.   var _public = {};
  4.   var _private = {};
  5.   _public.iData = initialData;
  6.  
  7. // Clean up whitespace in array if present after initial regex eval.
  8.   _private.cleanWhitespace = function(arr){
  9.     var arrClean = [];
  10.     for (var elem in arr) {
  11.         if (arr[elem] && /\w+/.test(arr[elem])) {
  12.          var strClean =  arr[elem].trim();
  13.           arrClean.push(strClean);
  14.         }
  15.     }
  16.     return arrClean;
  17.   };
  18.  
  19.   // Return sentences as array.
  20.   _public.getSentences = function() {
  21.     var sentenceArr = this.iData.match(/\n|([^\r\n.!?]+([.!?]+|$))/gim);
  22.     return _private.cleanWhitespace(sentenceArr);
  23.   };
  24.    
  25.   // Return sentence array in reverse order.
  26.    _public.getReverseSentences = function() {
  27.      return this.getSentences().reverse();
  28.   };
  29.  
  30.   // Return words as array (optional sentence index parameter).
  31.   _public.getWords = function(sentenceIndex) {
  32.     var wordArr = []; //=
  33.     if(sentenceIndex){
  34.       if(typeof sentenceIndex != "undefined"){
  35.         var sentenceArr = this.getSentences();
  36.         wordArr = sentenceArr[sentenceIndex].toLowerCase().split(/\W+/);
  37.       }
  38.     }
  39.     else {wordArr = this.iData.toLowerCase().split(/\W+/gim);}
  40.     return _private.cleanWhitespace(wordArr);
  41.   };
  42.  
  43.   // Return word array in reverse order
  44.   _public.getReverseWords = function(sentenceIndex) {
  45.      return this.getWords(sentenceIndex).reverse();
  46.   };
  47.  
  48.   // Count words beginning with matched string from user input.
  49.   // Builds multi-character regex from simple string input - May behave oddly with non-alpha characters.
  50.   _public.countWordsBeginningWith = function(matchString,sentence) {
  51.     var rBegins = new RegExp("^(?!" + matchString.toLowerCase() + ")")
  52.     var wordArr = this.getWords();
  53.     var i0 = 0;
  54.     while(i0<wordArr.length)
  55.       {
  56.         if(rBegins.test(wordArr[i0])){wordArr.splice(i0,1);}
  57.         else{i0++;}
  58.       }
  59.     return wordArr.length;
  60.   };
  61.  
  62.   // Sort words by alpha or length, ascending. Should be easy to expand for reverse order & other sort methods.
  63.   _public.sortWords = function(sortType) {
  64.     var wordArr = this.getWords();
  65.  
  66.     if(sortType == "alpha") {return wordArr.sort(this.sortAlpha());}
  67.      if (sortType == "length") {return wordArr.sort(this.sortLength());}
  68.   };
  69.  
  70.   _public.sortAlpha = function() {
  71.     return function (x, y) {
  72.       return x>y?1:-1;
  73.     };
  74.   };
  75.  
  76.   _public.sortLength= function() {
  77.     return function (x, y) {
  78.       return x.length>y.length?1:-1;
  79.     };
  80.   };
  81.  
  82.   // Convert words to Pig Latin.
  83.   _public.pigLatinConvertPG = function() {
  84.     var arr = this.iData.split(/b/);
  85.     var arr2 = [];
  86.     for (var elem in arr) {
  87.       var text = arr[elem];
  88.  
  89.       //Regex replacement rules for Pig Latin
  90.       text = text.replace(/\b([aeiou][a-z]*)\b/gi, "$1way"); // Vowel-start rule
  91.       text = text.replace(/\b([bcdfghjklmnpqrstvwxy]+)([a-z]*)\b/gi, "$2$1ay"); // Consonant-start rule
  92.       arr2.push(text);
  93.     }
  94.     return arr2;
  95.   };
  96.  
  97.   return _public;
  98. };
  99.  
  100. // Validation function.
  101. assertIfNotEqual = function(a,b,mesg){
  102.   if (a !== b) console.log(mesg);
  103. };
  104.  
  105. // TEST DATA
  106. var sampleData = 'He presses his tongue flat against me as he laps at me with long strokes. He starts at the bottom and sweeps it upward in lazy, even lengths. Every time he comes up, I catch his eyes, blazing fire with pleasure and thick desire. Fuck! As always the pressure is just right and as he stops and re-connects with me again – there, yes! Right there! – I get a 1000 volt jolt, straight to my core.';
  107.  
  108. // TEST FUNCTIONS
  109. var wp = new wordProcessor(sampleData);
  110. console.log(wp.pigLatinConvertPG());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement