Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. if (!Array.prototype.newStyle) {
  2.     Array.prototype.newStyle = function() {
  3.         this.push({'name' : '', 'string' : ''});
  4.     };
  5. }
  6.  
  7. if (!Array.prototype.last) {
  8.     Array.prototype.__defineGetter__('last', function() {
  9.         return this[this.length - 1];
  10.     });
  11. }
  12.  
  13.  
  14. if (!String.prototype.styleTokenizer) {
  15. String.prototype.styleTokenizer = function() {
  16.     var styles = [];
  17.     styles.newStyle();
  18.     var state  = 'ReadString';
  19.    
  20.     for (var symbol of this) {
  21.         switch (state) {
  22.             case 'ReadString':
  23.                 if (symbol == '[') {
  24.                     styles.newStyle();
  25.                     state = 'ReadStyleHeader';
  26.                 } else
  27.                     styles.last.string += symbol;                  
  28.                 break;
  29.                
  30.             case 'ReadStyleHeader':
  31.                 if (symbol == ']')
  32.                     state = 'ReadStyledString';
  33.                 else
  34.                     styles.last.name += symbol;
  35.                 break;
  36.                
  37.             case 'ReadStyledString':
  38.                 if (symbol == '}') {
  39.                     styles.newStyle();
  40.                     state = 'ReadString';
  41.                 } else if (symbol != '{')
  42.                     styles.last.string += symbol;
  43.                 break;
  44.         }
  45.     }
  46.     return styles;
  47. }}
  48.  
  49. var styles = 'kek kek kek [bold]{ its a bold string } vanilla string [SuperDuper] { superduper string }'.styleTokenizer();
  50.  
  51. styles.forEach(function(style) {
  52.     console.log('style[' + style.name + '] : ' + style.string)
  53. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement