Skip to content

Commit 6d33c42

Browse files
committed
feat(connect): implement junction
1 parent 4902598 commit 6d33c42

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

src/connect/junction.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
"use strict";
2-
function applyMiddleware(text, middleware, next) {
3-
middleware(text, next);
2+
function isErrorHandingMiddleware(middleware) {
3+
// middleware(error, text, next)
4+
var arity = middleware.length;
5+
return arity === 3;
6+
}
7+
function applyMiddleware(error, text, middleware, next) {
8+
let errorOnMiddleware = null;
9+
try {
10+
if (error && isErrorHandingMiddleware(middleware)) {
11+
middleware(error, text, next);
12+
} else {
13+
middleware(text, next);
14+
}
15+
return;
16+
} catch (error) {
17+
errorOnMiddleware = error;
18+
}
19+
// skip the middleware or Error on the middleware
20+
next(errorOnMiddleware, text);
421
}
522

623
export default class Junction {
@@ -18,7 +35,7 @@ export default class Junction {
1835
if (!middleware) {
1936
return callback(error, data);
2037
}
21-
applyMiddleware(data, middleware, next);
38+
applyMiddleware(error, data, middleware, next);
2239
};
2340
next(null, text);
2441
}

test/connect/junction-test.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// LICENSE : MIT
2+
"use strict";
3+
import assert from "power-assert";
4+
import Junction from "../../src/connect/junction";
5+
describe("junction", function () {
6+
describe("when", function () {
7+
it("should", function (done) {
8+
var junction = new Junction();
9+
junction.use(function errorHandling(error, text, next) {
10+
next(error);
11+
});
12+
junction.use(function toUpper(text, next) {
13+
next(null, text.toLocaleUpperCase());
14+
});
15+
junction.use(function addDesu(text, next) {
16+
next(null, text + " suffix");
17+
});
18+
junction.process("text", (error, result) => {
19+
if (error) {
20+
return done(error);
21+
}
22+
assert.equal(result, "TEXT suffix");
23+
done();
24+
});
25+
});
26+
});
27+
});

0 commit comments

Comments
 (0)