Boolasync
Async boolean logic.
monkey: true ; // fn1 && fn2 || fn3 && (fn4 || !fn5 && fn6)fn1;
One advantage bool has over executing all your async functions, waiting for their return values and doing native boolean operations is that it lazy evaluates callbacks. Meaning that it won't wait for a function's return value if it already knows the final result will be false/true. Just like Javascript does.
Usage
// this will monkey patch Function.prototype to expose .and, .or,andNot and or Not var boolasync = monkey: true ; // if you dislike monkey patching var boolasync = w = boolasyncwrap; // now you need to wrap functions with boolasync.wrap before you// can call the boolean operators on them fn1 = ;fn2 = ; fn1;
(wannabe) BNF notation:
<expression> ::= "fn." <method> "(" <arguments> ")" <expression> | ""
<arguments> ::= <expression> | "[" <list> "]" | <list>
<list> ::= <expression> | <expression>,<list>
<method> ::= "and" | "or" | "andNot" | "orNot"
or
fnfnfnfn
The signature for fn
is: function (callback) {}
where callback's
signature is function (err, res)
. Res should be a boolean or it will
be coerced to a boolean.
The signature for cb
is: function (err, istrue) {}
where istrue
is
the result of the expression.
You can nest expressions:
// fn && (fn2 || fn3)fn
Here is a more concrete example:
var { process;}var { process;}var { process;} // is_user && (is_admin || id_superadmin)is_user;
3 ways to write the same thing:
is_user is_user is_user
Roadmap
- Support this type of syntax:
bool("? and ? or (? and ?)", [ fn1, fn2, fn3, fn4 ], cb);
-
Browser friendly build.
-
Wrapper utility for sync functions and non-functions.
Instead of .eval(cb)
, simply call (cb)
at the end.
-
Version that uses function generators
-
Stingify expression object to nice representation. i.e.: (fn1 && fn2 || fn3)