Skip to content

Commit

Permalink
feat(connect): implement junction
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Sep 17, 2015
1 parent 4902598 commit 6d33c42
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/connect/junction.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
"use strict";
function applyMiddleware(text, middleware, next) {
middleware(text, next);
function isErrorHandingMiddleware(middleware) {
// middleware(error, text, next)
var arity = middleware.length;
return arity === 3;
}
function applyMiddleware(error, text, middleware, next) {
let errorOnMiddleware = null;
try {
if (error && isErrorHandingMiddleware(middleware)) {
middleware(error, text, next);
} else {
middleware(text, next);
}
return;
} catch (error) {
errorOnMiddleware = error;
}
// skip the middleware or Error on the middleware
next(errorOnMiddleware, text);
}

export default class Junction {
Expand All @@ -18,7 +35,7 @@ export default class Junction {
if (!middleware) {
return callback(error, data);
}
applyMiddleware(data, middleware, next);
applyMiddleware(error, data, middleware, next);
};
next(null, text);
}
Expand Down
27 changes: 27 additions & 0 deletions test/connect/junction-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// LICENSE : MIT
"use strict";
import assert from "power-assert";
import Junction from "../../src/connect/junction";
describe("junction", function () {
describe("when", function () {
it("should", function (done) {
var junction = new Junction();
junction.use(function errorHandling(error, text, next) {
next(error);
});
junction.use(function toUpper(text, next) {
next(null, text.toLocaleUpperCase());
});
junction.use(function addDesu(text, next) {
next(null, text + " suffix");
});
junction.process("text", (error, result) => {
if (error) {
return done(error);
}
assert.equal(result, "TEXT suffix");
done();
});
});
});
});

0 comments on commit 6d33c42

Please sign in to comment.