Permalink
Browse files

Add a function generating helper for the times you want to create a c…

…hain to run later.
  • Loading branch information...
1 parent a91b456 commit 484037645cb520c67ffa4d6e968d074661b74d22 @creationix committed Apr 9, 2010
Showing with 36 additions and 0 deletions.
  1. +23 −0 lib/step.js
  2. +13 −0 test.js
View
@@ -81,6 +81,29 @@ function Step() {
next([]);
}
+// Tack on leading and tailing steps for input and output and return
+// the whole thing as a function. Basically turns step calls into function
+// factories.
+Step.fn = function StepFn() {
+ var steps = Array.prototype.slice.call(arguments);
+ return function () {
+ var args = Array.prototype.slice.call(arguments);
+
+ // Insert a first step that primes the data stream
+ steps.unshift(function () {
+ this.apply(null, args);
+ });
+
+ // If the last arg is a function add it as a last step
+ if (typeof args[args.length-1] === 'function') {
+ steps.push(args.pop());
+ }
+
+ Step.apply(null, steps);
+ }
+}
+
+
// Hook into commonJS module systems
if (typeof module !== 'undefined' && "exports" in module) {
module.exports = Step;
View
@@ -52,3 +52,16 @@ Step(
sys.p(files);
}
);
+
+var myfn = Step.fn(
+ function (name) {
+ fs.readFile(name, this);
+ },
+ function capitalize(err, text) {
+ if (err) {
+ throw err;
+ }
+ return text.toUpperCase();
+ }
+);
+myfn(__filename, sys.p);

0 comments on commit 4840376

Please sign in to comment.