title: How can I reuse code between Javascript macros and minimize work done within the macros?
body: <p>I currently have two macros that are part of a (very limited-audience) plugin I'm developing, that both look basically like:</p>
  
  <pre><code>(function(){
  exports.name = "name";
  
  exports.params = [
      {name: "value"}
  ];
  
  function get(tiddler) {
      // return some contents of some tiddler fields according to some rule
  }
  
  function parse(data) {
      // convert string to some kind of useful object
  }
  
  function logic(x, y) {
      // determine whether the two objects correspond in some way
  };
  
  function format(data, title) {
      // produce WikiText for a link with some additional decoration
  };
  
  exports.run = function(value) {
      value = parse(value);
  
      var result = [];
      this.wiki.each(function(tiddler, title) {
          var data = get(tiddler);
          if (data !== undefined &amp;&amp; logic(value, parse(data))) {
              result.push(format(data, title));
          }
      });
      return result.join(" | ");
  };
  })();
  </code></pre>
  
  <p>So they're already fairly neatly factored when considered individually; the problem is that only the core <code>logic</code> is really different between the two macros. How can I share the functions <code>get</code>, <code>logic</code> and <code>format</code> between the macros? I tried just putting them in a separate tiddler, but that doesn't work; when the macros run, TW raises an error claiming that the functions are "not defined". Wrapping each function as its own javascript macro in a separate tiddler, e.g.</p>
  
  <pre><code>(function(){
  exports.name = "get";
  
  exports.params = [
      {name: "tiddler"}
  ];
  
  exports.run = function(tiddler) {
      // return some contents of some tiddler fields according to some rule
  }
  })();
  </code></pre>
  
  <p>also didn't help.</p>
  
  <p>I'd also like to set this up to be more modular/flexible, by turning the main <code>get</code>/<code>parse</code>/<code>logic</code>/<code>format</code> process into a custom filter, then letting a normal filter expression take care of the iteration and using e.g. the  widget or &lt;> macro to display the items. How exactly do I set this up? The documentation tells me</p>
  
  <blockquote>
    <p>If the provided filter operators are not enough, a developer can add
    new filters by adding a module with the <code>filteroperator</code> type</p>
  </blockquote>
  
  <p>but I can't find any documentation of the API for this, nor any examples.</p>