Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

polishing code

  • Loading branch information...
commit b6c710f40a00bf8f5ae1ee3b3942ac5827db47d6 1 parent 432a7a2
@chjj authored
View
2  LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2011, Christopher Jeffrey (http://epsilon-not.net/)
+Copyright (c) 2011, Christopher Jeffrey (//github.com/chjj)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
View
43 N-features.js
@@ -1,43 +0,0 @@
-// $N - flow control
-// Copyright (c) 2011, Christopher Jeffrey (MIT Licensed)
-
-// experimental version with more features and consistent arguments
-// uses "this" instead of "next"
-
-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)) {
- 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));
- return next;
- };
- next.__defineGetter__('para', function() {
- return ++total && function() {
- results.push(Array.prototype.slice.call(arguments));
- 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
24 N.js
@@ -1,24 +0,0 @@
-// $N - flow control
-// Copyright (c) 2011, Christopher Jeffrey (MIT Licensed)
-
-var $N = function $N() {
- var cur = 0, func = Array.prototype.slice.call(arguments);
- 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;
- };
- next.N = function() {
- func = func.concat(Array.prototype.slice.call(arguments));
- return next;
- };
- return next;
-};
-
-if (typeof module !== 'undefined' && module.exports) {
- module.exports = $N;
-} else {
- this.$N = $N;
-}
View
120 README.md
@@ -1,31 +1,43 @@
-# $N - A flow control tool
+# $N
$N is my attempt at making something similar to Step, but with less indentation
-and slightly different syntax. $N is very light-weight and small, only a few lines,
-so it is copy & pastable if need be.
+and slightly different syntax. $N is very light-weight and small, only a few
+lines, so it is copy & pastable if need be.
-## Example Usage
+## Usage
- $N(function(next) {
- fs.readFile('./text.txt', 'utf-8', next);
- })
- .N(function(next, err, txt) {
- if (err) {
- console.log(err.message);
- } else {
- console.log('Some text: ' + txt);
- }
- next.N(function(next) { // you can even push more functions onto the stack!
- console.log('world');
- next();
- }, function() { // you can pass multiple args if you want to use it like Step
- console.log('how are you?');
- })('hello'); // the same thing as calling next() when inside a stack
- })
- .N(function(next, hello) {
- console.log(hello);
+``` js
+var $N = require('N')
+ , fs = require('fs');
+
+$N(function(next) {
+ fs.readFile('./log.txt', 'utf-8', next);
+})
+.N(function(next, err, txt) {
+ if (err) {
+ console.log(err.message);
+ } else {
+ console.log('Some text: ' + txt);
+ }
+ next.N(
+ // you can even push
+ // more functions onto the stack!
+ function(next) {
+ console.log('world');
next();
- })(); // execute the chain
+ },
+ // you can pass multiple args if
+ // you want to use it like Step
+ function() {
+ console.log('how are you?');
+ }
+ )('hello');
+})
+.N(function(next, hello) {
+ console.log(hello);
+ next();
+})();
+```
### Outputs:
@@ -36,33 +48,43 @@ so it is copy & pastable if need be.
## More Features
-There's also an "N-features.js" file, which is something I'm experimenting with. It includes parallel calls and
-consistent arguments with error handling, e.g. (err, data). I'm unsure whether I want to include these features,
-I like keeping things simple.
+There's also an "N-features.js" file, which is something I'm experimenting with.
+It includes parallel calls and consistent arguments with error handling, e.g.
+(err, data). I'm unsure whether I want to include these features, I like
+keeping things simple.
- $N(function() {
- fs.readFile('./text1.txt', 'utf-8', this);
- })
- .N(function(err, text) {
- if (err) {
- console.log(err);
- } else {
- console.log('Some text: ' + text);
- }
- this.N(function(err, results) {
- console.log('world');
- this(results);
- }, function(err, results) {
- console.log('how are you?');
- console.log('...even more text:', results);
- })('hello');
- })
- .N(function(err, hello) {
- if (err) console.log('oh no, an error:', err)
- console.log(hello);
- fs.readFile('./text2.txt', 'utf-8', this.para);
- fs.readFile('./text3.txt', 'utf-8', this.para);
- })();
+``` js
+$N(function() {
+ fs.readFile('./log1.txt', 'utf-8', this);
+})
+.N(function(err, text) {
+ if (err) {
+ console.log(err);
+ } else {
+ console.log('Some text: ' + text);
+ }
+ this.N(
+ // you can even push
+ // more functions onto the stack!
+ function(err, results) {
+ console.log('world');
+ this(results);
+ },
+ // you can pass multiple args if
+ // you want to use it like Step
+ function(err, results) {
+ console.log('how are you?');
+ console.log('...even more text:', results);
+ }
+ )('hello');
+})
+.N(function(err, hello) {
+ if (err) console.log('oh no, an error:', err)
+ console.log(hello);
+ fs.readFile('./log2.txt', 'utf-8', this.para);
+ fs.readFile('./log3.txt', 'utf-8', this.para);
+})();
+```
## License
View
2  index.js
@@ -1 +1 @@
-module.exports = require('./N');
+module.exports = require('./lib/N');
View
49 lib/N-features.js
@@ -0,0 +1,49 @@
+// $N - features
+// Copyright (c) 2011, Christopher Jeffrey (MIT Licensed)
+
+// experimental version with more features and consistent arguments
+// uses "this" instead of "next"
+
+var slice = [].slice;
+
+var $N = function() {
+ var i = 0
+ , fn = slice.call(arguments)
+ , pending = 0
+ , results = [];
+
+ var next = function() {
+ if (!fn[i]) return;
+ var args = slice.call(arguments);
+ if (args.length && !(args[0] instanceof Error)) {
+ args.unshift(undefined);
+ }
+ try {
+ fn[i++].apply(next, args);
+ } catch(e) {
+ next(e);
+ }
+ return next;
+ };
+
+ next.N = function() {
+ fn = fn.concat(slice.call(arguments));
+ return next;
+ };
+
+ next.__defineGetter__('para', function() {
+ pending++;
+ return function() {
+ results.push(slice.call(arguments));
+ --pending || next(results);
+ };
+ });
+
+ return next;
+};
+
+if (typeof module !== 'undefined' && module.exports) {
+ module.exports = $N;
+} else {
+ this.$N = $N;
+}
View
10 N-minimal.js → lib/N-minimal.js
@@ -1,9 +1,11 @@
-// $N - flow control - the minimal version
+// $N - minimal version
// Copyright (c) 2011, Christopher Jeffrey (MIT Licensed)
-var $N = function $N() {
- var i = 0, fn = Array.prototype.slice.call(arguments);
+
+var $N = function() {
+ 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)));
+ if (fn[i]) fn[i++].apply(n.N, [n].concat(fn.slice.call(arguments)));
};
next.N = function() {
fn = fn.concat(fn.slice.call(arguments));
View
30 lib/N.js
@@ -0,0 +1,30 @@
+// $N
+// Copyright (c) 2011, Christopher Jeffrey (MIT Licensed)
+
+var slice = [].slice;
+
+var $N = function() {
+ var i = 0
+ , fn = slice.call(arguments);
+
+ var next = function() {
+ if (!fn[i]) return;
+ var args = slice.call(arguments);
+ if (fn[i+1]) args.unshift(next);
+ fn[i++].apply(next.N, args);
+ return next;
+ };
+
+ next.N = function() {
+ fn = fn.concat(slice.call(arguments));
+ return next;
+ };
+
+ return next;
+};
+
+if (typeof module !== 'undefined' && module.exports) {
+ module.exports = $N;
+} else {
+ this.$N = $N;
+}
View
8 package.json
@@ -1,7 +1,7 @@
{
- "name": "$N",
- "description": "simple flow control",
+ "name": "N",
+ "description": "next flow",
"author": "Christopher Jeffrey",
- "version": "0.1",
- "main": "./N.js"
+ "version": "0.0.2",
+ "main": "./lib/N.js"
}
View
2  test/test-features.js
@@ -1 +1 @@
-var fs = require('fs');
+var fs = require('fs');
View
23 test/test.js
@@ -1,6 +1,6 @@
var fs = require('fs');
-var $N = require('../N');
+var $N = require('../');
$N(function(next) {
fs.readFile('./log.txt', 'utf-8', next);
@@ -11,14 +11,21 @@ $N(function(next) {
} else {
console.log('Some text: ' + txt);
}
- next.N(function(next) { // you can even push more functions onto the stack!
- console.log('world');
- next();
- }, function() { // you can pass multiple args if you want to use it like Step
- console.log('how are you?');
- })('hello'); // the same thing as calling next() when inside a stack, the .N() func returns "next"
+ next.N(
+ // you can even push
+ // more functions onto the stack!
+ function(next) {
+ console.log('world');
+ next();
+ },
+ // you can pass multiple args if
+ // you want to use it like Step
+ function() {
+ console.log('how are you?');
+ }
+ )('hello');
})
.N(function(next, hello) {
console.log(hello);
next();
-})(); // execute the chain
+})();
Please sign in to comment.
Something went wrong with that request. Please try again.