Skip to content

Commit

Permalink
initial commit, includes a functional version of filterchain
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpvar committed Nov 16, 2011
0 parents commit aa26511
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
gmon.out
v8.log
7 changes: 7 additions & 0 deletions LICENSE.txt
@@ -0,0 +1,7 @@
Copyright (c) 2011 Elijah Insua

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
68 changes: 68 additions & 0 deletions index.js
@@ -0,0 +1,68 @@
(function() {

function FilterChain(array, op) {
this._chain = array;
this._op = op;
}

FilterChain.prototype = {
execute: function(data, fn) {
var
index = -1,
op = this._op,
that = this,
chain = this._chain,
errors = [],
bubbles = [],
capture = function() {
index++;

if (index>=chain.length) {
if (op) {
op(data, function() {
// called when the core of the chain is complete
bubble(data);
});
} else {
bubble(data);
}
} else {
chain[index](data, function next(data, bubbleFn) {
if (bubbleFn) {
bubbles.push(bubbleFn);
}

capture(data)
}, function cancel(err, data) {
if (err) {
errors.push(err)
}
bubble(data);
});
}

}, bubble = function(data) {

if (bubbles.length < 1) {
fn((errors.length) ? errors : null, data);
} else {
var bubbleFn = bubbles.pop();
bubbleFn(data, function done() {
bubble(data);
});
}
};

this._where = 0;
capture(bubble);
}
};

module.exports = {
createChain : function(array, coreFn) {
var ret = new FilterChain(array, coreFn);
return ret;
}
}

})();
15 changes: 15 additions & 0 deletions package.json
@@ -0,0 +1,15 @@
{
"author": "Elijah Insua <tmpvar@gmail.com> (http://tmpvar.com)",
"name": "filterchain",
"description": "perform work before and after an operation",
"version": "0.1.0",
"homepage": "http://tmpvar.com/project/filterchain",
"repository": {
"url": ""
},
"engines": {
"node": "*"
},
"dependencies": {},
"devDependencies": {}
}
46 changes: 46 additions & 0 deletions test.js
@@ -0,0 +1,46 @@
var
fc = require('./'),
equal = function(expected, actual) {
if (expected !== actual) {
console.log(new Error('expected "' + expected + '"; saw "' + actual + '"'));
process.exit(1);
}
}


// Successful pre + post execution
var basic = fc.createChain([
function(data, next, cancel) {
data.hello = 'pre-' + data.hello
next(data, function(data, done) {
data.hello += '-post';
done();
});
},
]);

basic.execute({ hello : 'world' }, function(errors, data) {
console.log(errors);
if (errors) { throw new Error(errors) }
equal('pre-world-post', data.hello);
});

// Cancel request
var cancel = fc.createChain([
function(data, next, cancel) {
next(data, function(data, done) {
done(data += 'outer');
});
},
function(data, next, cancel) {
cancel(null, data);
},
function(data, next, cancel) {
data += 'entered center';
next(data);
}
]);

cancel.execute('', function(errors, data) {
equal(data, 'outer');
});

0 comments on commit aa26511

Please sign in to comment.