Skip to content
Browse files

initial commit, includes a functional version of filterchain

  • Loading branch information...
0 parents commit aa26511efb407a8dc3e32352492bd5633cb7ba99 @tmpvar committed Nov 15, 2011
Showing with 138 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +7 −0 LICENSE.txt
  3. +68 −0 index.js
  4. +15 −0 package.json
  5. +46 −0 test.js
2 .gitignore
@@ -0,0 +1,2 @@
+gmon.out
+v8.log
7 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 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 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 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.
Something went wrong with that request. Please try again.