Browse files

minor tweaks

  • Loading branch information...
1 parent 6c40dfe commit 432a7a229b84a9f02b24e4449f0e74853c2301fc @chjj committed Apr 26, 2011
Showing with 44 additions and 9 deletions.
  1. +11 −3 N-features.js
  2. +13 −0 N-minimal.js
  3. +13 −6 N.js
  4. +7 −0 package.json
View
14 N-features.js
@@ -4,19 +4,21 @@
// experimental version with more features and consistent arguments
// uses "this" instead of "next"
-var $N = module.exports = function() {
+var $N = function $N() {
var cur = 0, func = Array.prototype.slice.call(arguments);
var total = 0, called = 0, results = []; // used for .para
var next = function next() {
if (!func[cur]) return;
var args = Array.prototype.slice.call(arguments);
- if (args[0] != null && !(args[0] instanceof Error))
+ if (args[0] != null && !(args[0] instanceof Error)) {
args.unshift(null);
+ }
try {
func[cur++].apply(next, args);
} catch(err) {
next(err);
}
+ return next;
};
next.N = function() {
func = func.concat(Array.prototype.slice.call(arguments));
@@ -25,11 +27,17 @@ var $N = module.exports = function() {
next.__defineGetter__('para', function() {
return ++total && function() {
results.push(Array.prototype.slice.call(arguments));
- if ((++called) === total) {
+ if (++called === total) {
total = called = 0;
next.call(null, results);
}
};
});
return next;
};
+
+if (typeof module !== 'undefined' && module.exports) {
+ module.exports = $N;
+} else {
+ this.$N = $N;
+}
View
13 N-minimal.js
@@ -0,0 +1,13 @@
+// $N - flow control - the minimal version
+// Copyright (c) 2011, Christopher Jeffrey (MIT Licensed)
+var $N = function $N() {
+ var i = 0, fn = Array.prototype.slice.call(arguments);
+ var next = function n() {
+ fn[i] && fn[i++].apply(n.N, [n].concat(fn.slice.call(arguments)));
+ };
+ next.N = function() {
+ fn = fn.concat(fn.slice.call(arguments));
+ return next;
+ };
+ return next;
+};
View
19 N.js
@@ -1,17 +1,24 @@
// $N - flow control
// Copyright (c) 2011, Christopher Jeffrey (MIT Licensed)
-var $N = module.exports = function() {
+var $N = function $N() {
var cur = 0, func = Array.prototype.slice.call(arguments);
- var exec = function next() {
+ var next = function next() {
if (!func[cur]) return;
var args = Array.prototype.slice.call(arguments);
if (func[cur+1]) args.unshift(next);
func[cur++].apply(next.N, args);
+ return next;
};
- exec.N = function() {
+ next.N = function() {
func = func.concat(Array.prototype.slice.call(arguments));
- return exec;
+ return next;
};
- return exec;
-};
+ return next;
+};
+
+if (typeof module !== 'undefined' && module.exports) {
+ module.exports = $N;
+} else {
+ this.$N = $N;
+}
View
7 package.json
@@ -0,0 +1,7 @@
+{
+ "name": "$N",
+ "description": "simple flow control",
+ "author": "Christopher Jeffrey",
+ "version": "0.1",
+ "main": "./N.js"
+}

0 comments on commit 432a7a2

Please sign in to comment.