Permalink
Browse files

first

  • Loading branch information...
0 parents commit 6c40dfe83b252f7423bddb9c7b1af80a14d52591 @chjj committed Apr 11, 2011
Showing with 166 additions and 0 deletions.
  1. +19 −0 LICENSE
  2. +35 −0 N-features.js
  3. +17 −0 N.js
  4. +69 −0 README.md
  5. +1 −0 index.js
  6. +1 −0 test/test-features.js
  7. +24 −0 test/test.js
@@ -0,0 +1,19 @@
+Copyright (c) 2011, Christopher Jeffrey (http://epsilon-not.net/)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
@@ -0,0 +1,35 @@
+// $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 = module.exports = function() {
+ 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);
+ }
+ };
+ 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;
+};
@@ -0,0 +1,17 @@
+// $N - flow control
+// Copyright (c) 2011, Christopher Jeffrey (MIT Licensed)
+
+var $N = module.exports = function() {
+ var cur = 0, func = Array.prototype.slice.call(arguments);
+ var exec = 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);
+ };
+ exec.N = function() {
+ func = func.concat(Array.prototype.slice.call(arguments));
+ return exec;
+ };
+ return exec;
+};
@@ -0,0 +1,69 @@
+# $N - A flow control tool
+
+$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.
+
+## Example 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);
+ next();
+ })(); // execute the chain
+
+### Outputs:
+
+ Some text: [whatever text]
+ hello
+ world
+ how are you?
+
+## 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.
+
+ $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);
+ })();
+
+## License
+
+See LICENSE for more info (MIT).
@@ -0,0 +1 @@
+module.exports = require('./N');
Oops, something went wrong.
@@ -0,0 +1,24 @@
+var fs = require('fs');
+
+var $N = require('../N');
+
+$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(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"
+})
+.N(function(next, hello) {
+ console.log(hello);
+ next();
+})(); // execute the chain

0 comments on commit 6c40dfe

Please sign in to comment.