Skip to content

String Parser

Malexion edited this page Nov 6, 2016 · 2 revisions

new __.lib.StringParser(keywords, [options])

  • keywords Object map with given keywords/chars as property keys and functions to process those keys.
  • [options] [Optional] Option params, { ignoreCase: true, defaultAction: (char, idx, str, event) => {} }

Examples

  var target = '(this and that)',
    build = [];

  var parser = new __.lib.StringParser({
    '(': function(char, idx, fullString, event) {
      build.push('');
    },
    ')': function() {
      console.log(build); // we are done print it out
    },
    ' ': function() {
      // ignore space characters
    },
    '!': function(c, i, s, event) {
      event.skip = 5; // skip the next 5 characters in the string.
    },
    'and': function(phrase, idx, fullString, event) {
      build.push('[{0}]'.format(phrase));
      build.push('');
    }
  }, {
    defaultAction: function(char, idx, fullString, event) {
      build[build.length - 1] += char;
    }
  });

  parser.parse(target);

Clone this wiki locally