stuppid_bot

Untitled

Mar 21st, 2013
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var input = ' { Здоров | Даров | Привет | Прив | Ку } , %name%. { Как { дела | жизнь | оно } ? } ';
  2. // var input = ' { Здоров | Даров | Привет | Прив | Ку } , %name%. { Как { дела | жизнь | оно } ? | Что делаешь? | Покидай музыки на стену. | Поставь лойс на аву. } ';
  3.  
  4. var i = 0, j = 0, l = input.length, output = '';
  5.  
  6. function parseBraces() {
  7.     var arr = [], temp;
  8.  
  9.     while (++i < l) {
  10.         if (input[i] == '{') {
  11.             temp = input.slice(j, i);
  12.             j = i + 1;
  13.             arr.push( temp + parseBraces() );          
  14.         }      
  15.         else if (input[i] == '}') {
  16.             arr.push( input.slice(j, i) );
  17.             j = i++ + 1;
  18.             alert( JSON.stringify(arr) );
  19.             return arr[ Math.floor( Math.random() * arr.length ) ];
  20.         }      
  21.         else if (input[i] == '|') {
  22.             arr.push( input.slice(j, i) );
  23.             j = i + 1;
  24.         }              
  25.     }
  26.  
  27.     throw Error('unexpected end');
  28. }
  29.  
  30. while (i < l) {
  31.     if (input[i] == '{') {
  32.         alert( i )
  33.         output += input.slice(j, i);  
  34.         j = i + 1;
  35.         output += parseBraces();
  36.     }
  37.     else if (input[i] == '}') {
  38.         throw Error('unexpected }');
  39.     }
  40.    
  41.     ++i;
  42. }
  43.  
  44. alert( output + input.slice(j, l) );
  45.  
  46.  
  47. // =======================================================================================================
  48.  
  49. function prepareMessage(input, replacements) {
  50.     return input.replace(/\{(.+?)\}/g, function(s, m) {
  51.         m = m.split('|');
  52.         return m[ Math.floor( Math.random() * m.length ) ];
  53.     }).replace(/%(.+?)%/g, function(s, m) {
  54.         return replacements[m] ? replacements[m] : s;
  55.     });  
  56. }
  57.  
  58. alert( prepareMessage('{Даров|Прив|Ку}, %name%. {Как дела?|Что делаешь?| Покидай музыки на стену.|Поставь лойс на аву.}', {name: 'Иван'}) );
Advertisement
Add Comment
Please, Sign In to add comment