Comparing version 0.0.1 to 0.0.2
{ | ||
"name": "zig", | ||
"version": "0.0.1", | ||
"description": "zig", | ||
"version": "0.0.2", | ||
"description": "Simple, but naughty, control flow for Node.js.", | ||
"main": "zig.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
114
README.md
@@ -1,4 +0,112 @@ | ||
zig | ||
=== | ||
Zig - Simple, but naughty, control flow for Node.js | ||
====================================================== | ||
zig | ||
> Why have an if statement when you can have an if function? | ||
A special case solution for callback hell that focuses on developer | ||
ease-of-use. Executes your functions in series or parallel, tracks errors and | ||
results, and provides conditionals. | ||
[![Build Status](https://travis-ci.org/rjrodger/zig.png?branch=master)](https://travis-ci.org/rjrodger/zig) | ||
[![NPM](https://nodei.co/npm/zig.png)](https://nodei.co/npm/zig/) | ||
[![NPM](https://nodei.co/npm-dl/zig.png)](https://nodei.co/npm-dl/zig/) | ||
If you're using this plugin module, feel free to contact me on twitter if you | ||
have any questions! :) [@rjrodger](http://twitter.com/rjrodger) | ||
Current Version: 0.0.2 | ||
Tested on: Node 0.10.31 | ||
### Install | ||
```sh | ||
npm install zig | ||
``` | ||
## Quick Example | ||
Here's the classic Mongo example. Look ma, no callbacks! Nice and | ||
linear down the page. | ||
```js | ||
var zig = require('..') | ||
var MongoClient = require('mongodb').MongoClient | ||
var format = require('util').format; | ||
var db,collection | ||
zig({trace:false}) | ||
.wait(function(data,done){ | ||
MongoClient.connect('mongodb://127.0.0.1:27017/test', done) | ||
}) | ||
.step(function(data){ | ||
db = data | ||
return collection = db.collection('test_insert') | ||
}) | ||
.wait(function(data,done){ | ||
collection.insert({a:2},done) | ||
}) | ||
.wait(function(data,done){ | ||
collection.count(done) | ||
}) | ||
.step(function(count){ | ||
console.log(format("count = %s", count)); | ||
return true; | ||
}) | ||
.wait(function(data,done){ | ||
collection.find().toArray(done) | ||
}) | ||
.end(function(err,docs){ | ||
if( err ) return console.log(err); | ||
console.dir(docs) | ||
db.close() | ||
}) | ||
``` | ||
And the original, callback hell: | ||
```js | ||
var MongoClient = require('mongodb').MongoClient | ||
, format = require('util').format; | ||
MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, db) { | ||
if(err) throw err; | ||
var collection = db.collection('test_insert'); | ||
collection.insert({a:2}, function(err, docs) { | ||
collection.count(function(err, count) { | ||
console.log(format("count = %s", count)); | ||
}); | ||
collection.find().toArray(function(err, results) { | ||
console.dir(results); | ||
db.close(); | ||
}); | ||
}); | ||
}) | ||
``` | ||
## Testing | ||
```sh | ||
npm test | ||
``` | ||
## Releases | ||
* 0.0.2: steps can exit | ||
* 0.0.1: first working version | ||
@@ -156,2 +156,17 @@ "use strict"; | ||
it('exit', function(fin){ | ||
var tmp = {} | ||
zig() | ||
.start() | ||
.step(function(){ tmp.a = 1; return true}) | ||
.step(function(){ tmp.b = 2; return false}) | ||
.step(function(){ tmp.c = 3; return null}) | ||
.step(function(){ tmp.d = 4; return true}) | ||
.end(function(e,o){ | ||
assert.equal(null,o) | ||
assert.deepEqual({a:1,b:2,c:3},tmp) | ||
fin(e) | ||
}) | ||
}) | ||
}) |
61
zig.js
//"use strict"; | ||
var util = require('util') | ||
var _ = require('underscore') | ||
@@ -10,2 +13,12 @@ | ||
var trace = options.trace || function(){} | ||
trace = (_.isBoolean(trace) && trace) ? function(){ | ||
var args = Array.prototype.slice.apply(arguments) | ||
args.unshift('zig') | ||
args = _.map(args,function(a){ | ||
return _.isObject(a) ? util.inspect(a) : a | ||
}) | ||
console.log(args.join('\t')) | ||
} : trace | ||
var errhandler = console.log | ||
@@ -22,7 +35,4 @@ var complete = console.log | ||
//console.log(options) | ||
if( options.timeout ) { | ||
setTimeout(function(){ | ||
//console.log('TO') | ||
dead = true | ||
@@ -42,17 +52,15 @@ | ||
step = steps.shift() | ||
//console.log(step,data,ifdepth) | ||
if( !step ) return exit(); | ||
if( !step ) { | ||
if( 0 < ifdepth ) throw new Error(ifdepth+' missing endifs.') | ||
return complete(null,data); | ||
} | ||
step.fn = step.fn || {nm:'---anon---'} | ||
step.fn.nm = step.fn.nm || step.fn.name | ||
if( 'step' == step.type && active ) { | ||
//console.log('step') | ||
data = step.fn(data) | ||
setImmediate(nextstep) | ||
trace(step.type,step.fn.nm,data) | ||
if( null == data ) return exit(); | ||
nextstep() | ||
} | ||
else if( 'run' == step.type && active ) { | ||
//console.log('run') | ||
collect++ | ||
@@ -64,7 +72,7 @@ step.fn(data,function(err,out){ | ||
}) | ||
setImmediate(nextstep) | ||
trace(step.type,step.fn.nm,data) | ||
nextstep() | ||
} | ||
else if( 'wait' == step.type && active ) { | ||
//console.log('wait') | ||
trace(step.type,step.fn.nm,data) | ||
if( 0 == collect ) return wait_fn(); | ||
@@ -74,4 +82,3 @@ check_collect() | ||
else if( 'if' == step.type ) { | ||
//console.log('if') | ||
if( 0 == ifdepth ) { | ||
if( active ) { | ||
active = evalif(data,step.cond) | ||
@@ -81,12 +88,15 @@ ifdepth++; | ||
else ifdepth++; | ||
setImmediate(nextstep) | ||
trace(step.type,step.fn.nm,active,ifdepth) | ||
nextstep() | ||
} | ||
else if( 'endif' == step.type ) { | ||
//console.log('endif') | ||
ifdepth--; | ||
ifdepth = ifdepth < 0 ? 0 : ifdepth; | ||
active = 0 == ifdepth; | ||
setImmediate(nextstep) | ||
trace(step.type,step.fn.nm,active,ifdepth+1) | ||
nextstep() | ||
} | ||
else setImmediate(nextstep) | ||
else nextstep() | ||
@@ -96,4 +106,3 @@ | ||
if( dead ) return; | ||
if( collector.length == collect ) { | ||
if( collector.length >= collect ) { | ||
data = _.clone(collector) | ||
@@ -114,2 +123,6 @@ collect = 0 | ||
function exit() { | ||
if( 0 < ifdepth ) throw new Error(ifdepth+' missing endifs.') | ||
return complete(null,data); | ||
} | ||
} | ||
@@ -133,3 +146,2 @@ nextstep() | ||
//console.log('evalif',bool,cond) | ||
return bool | ||
@@ -140,3 +152,3 @@ } | ||
self.start = function( cb ) { | ||
errhandler = cb | ||
errhandler = cb || errhandler | ||
return self; | ||
@@ -146,3 +158,2 @@ } | ||
self.end = function( cb ) { | ||
// TODO: catch unmatched if-endif | ||
complete = cb || errhandler | ||
@@ -149,0 +160,0 @@ errhandler = complete |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
167918302
18
392
113