tamarack

Micro framework for working with the chain of responsibility pattern. Port of the Tamarack .NET library.

Usage no npm install needed!

<script type="module">
  import tamarack from 'https://cdn.skypack.dev/tamarack';
</script>

README

Tamarack(.js)

Build Status NPM version

This is a NodeJS port of the similarly named framework for .NET, written by Mike Valenty. The documentation on that page is more verbose than the stuff on this page. That's because the APIs are pretty much identical except for the case of the first letter in the functions. And there's less angle brackets in the JavaScript version.

Installation

Node

npm install tamarack

Browser

Reference tamarack.min.js somewhere, and use window.tamarack.

Usage

You start with a Pipeline. Pipelines contain filters. In the dynamic, non-type-safe JavaScript world, a filter is just an object that has an executeSync function on it. Here is the simplest filter of them all:

var simpleFilter = {
    executeSync: function(input, next) {
        return next(input);
    }
};
var Pipeline = require('tamarack').Pipeline;

function createNewPost(post) {
    var pipeline = new Pipeline()
        .add(new CanonicalizeHtml())
        .add(new StripMaliciousTags())
        .add(new RemoveJavaScript())
        .add(new RewriteProfanity())
        .add(new GuardAgainstDoublePost())
        .andFinally(function(post) { return repository.saveSync(post); });

    return pipeline.executeSync(post);
}

Here's what one of these filters might look like:

function RewriteProfanity() {}

RewriteProfanity.prototype = {
    executeSync: function(input, next) {
        input = input.replace(/ur mom sux/gi, 'Let\'s agree to disagree.');
        return next(input);
    }
};

Asynchronous Pipelines

Two things to do when handling asynchronous filters and pipelines:

  1. Add an execute(input, next, callback) function to your filter
  2. Call execute(input, null, callback) on the pipeline

For example, here's an asynchronous filter:

function AppendWord(word) {
    this.word = word;
}
AppendWord.prototype = {
    execute: function(input, next, callback) {
        input += this.word;
        next(input, callback);
    }
};

And then how you use it in a pipeline:

new Pipeline()
    .add(new AppendWord(' world'))
    .andFinally(function(input, callback) {
        callback(input + '!');
    })
    .execute('Hello', null, function(result) {
        console.log(result); //"Hello world!"
    });

If you want to modify the output using an asynchronous filter, AppendWord.prototype.execute would become:

function(input, next, callback) {
    var self = this;
    next(input, function(result) {
        result += self.word;
        callback(result);
    });
}

The result after executing the pipeline as above would be Hello !world.

Development

git clone git@github.com:tmont/tamarackjs.git
cd tamarackjs
npm install
node_modules/.bin/grunt test
  • Run node_modules/.bin/grunt build to create the minified browser version of tamarack.
  • Run node_modules/.bin/grunt coverage for code coverage.
  • Run node_modules/.bin/grunt release to prepare a release build.