[Pkg-javascript-commits] [node-async] 33/480: added log and dir functions
Jonas Smedegaard
js at moszumanska.debian.org
Fri May 2 08:58:09 UTC 2014
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository node-async.
commit b438b9050d5ed488bd293963117fbf2f5d93aa6b
Author: Caolan McMahon <caolan at caolanmcmahon.com>
Date: Mon Aug 2 18:27:38 2010 +0100
added log and dir functions
---
README.md | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++
lib/async.js | 22 ++++++++++++++++++++
test/test-async.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 142 insertions(+)
diff --git a/README.md b/README.md
index 34e6371..c08cf8b 100644
--- a/README.md
+++ b/README.md
@@ -125,6 +125,14 @@ apply
: Creates a continuation with some arguments already applied, a useful
shorthand when combined with other flow control functions.
+### Utils
+
+log
+: Logs the result of an async function to the console.
+
+dir
+: Logs the result of an async function to the console using console.dir
+
### forEach(arr, iterator, callback)
@@ -629,3 +637,54 @@ continuation:
one
two
three
+
+
+## Utils
+
+### log(function, arguments)
+
+Logs the result of an async function to the console. Only works in node.js or
+in browsers that support console.log and console.error (such as FF and Chrome).
+If multiple arguments are returned from the async function, console.log is
+called on each argument in order.
+
+__Arguments__
+
+* function - The function you want to eventually apply all arguments to.
+* arguments... - Any number of arguments to apply to the function.
+
+__Example__
+
+ var hello = function(name, callback){
+ setTimeout(function(){
+ callback(null, 'hello ' + name);
+ }, 1000);
+ };
+
+ node> async.log(hello, 'world');
+ 'hello world'
+
+
+### dir(function, arguments)
+
+Logs the result of an async function to the console using console.dir to
+display the properties of the resulting object. Only works in node.js or
+in browsers that support console.dir and console.error (such as FF and Chrome).
+If multiple arguments are returned from the async function, console.dir is
+called on each argument in order.
+
+__Arguments__
+
+* function - The function you want to eventually apply all arguments to.
+* arguments... - Any number of arguments to apply to the function.
+
+__Example__
+
+ var hello = function(name, callback){
+ setTimeout(function(){
+ callback(null, {hello: name});
+ }, 1000);
+ };
+
+ node> async.dir(hello, 'world');
+ {hello: 'world'}
diff --git a/lib/async.js b/lib/async.js
index 6aca9e6..b4ba24b 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -383,4 +383,26 @@
};
};
+ var _console_fn = function(name){
+ return function(fn){
+ var args = Array.prototype.slice.call(arguments, 1);
+ fn.apply(null, args.concat([function(err){
+ var args = Array.prototype.slice.call(arguments, 1);
+ if(typeof console != 'undefined'){
+ if(err){
+ if(console.error) console.error(err);
+ }
+ else if(console[name]){
+ _forEach(args, function(x){console[name](x);});
+ }
+ }
+ }]));
+ };
+ };
+ exports.log = _console_fn('log');
+ exports.dir = _console_fn('dir');
+ /*exports.info = _console_fn('info');
+ exports.warn = _console_fn('warn');
+ exports.error = _console_fn('error');*/
+
})((typeof exports == 'undefined') ? this['async']={}: exports);
diff --git a/test/test-async.js b/test/test-async.js
index af1457b..312a81f 100644
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -743,3 +743,64 @@ exports['apply'] = function(test){
async.apply(fn)(1, 2, 3, 4);
test.done();
};
+
+
+// generates tests for console functions such as async.log
+var console_fn_tests = function(name){
+
+ exports[name] = function(test){
+ test.expect(5);
+ var fn = function(arg1, callback){
+ test.equals(arg1, 'one');
+ process.nextTick(function(){callback(null, 'test');});
+ };
+ var fn_err = function(arg1, callback){
+ test.equals(arg1, 'one');
+ process.nextTick(function(){callback('error');});
+ };
+ var _console_fn = console[name];
+ var _error = console.error;
+ console[name] = function(val){
+ test.equals(val, 'test');
+ test.equals(arguments.length, 1);
+ console.error = function(val){
+ test.equals(val, 'error');
+ console[name] = _console_fn;
+ console.error = _error;
+ test.done();
+ };
+ async[name](fn_err, 'one');
+ };
+ async[name](fn, 'one');
+ };
+
+ exports[name + ' without console.' + name] = function(test){
+ var _console = global.console;
+ global.console = undefined;
+ var fn = function(callback){callback(null, 'val');};
+ var fn_err = function(callback){callback('error');};
+ async[name](fn);
+ async[name](fn_err);
+ global.console = _console;
+ test.done();
+ };
+
+ exports[name + ' with multiple result params'] = function(test){
+ var fn = function(callback){callback(null, 'one', 'two', 'three');};
+ var _console_fn = console[name];
+ var called_with = [];
+ console[name] = function(x){
+ called_with.push(x);
+ };
+ async[name](fn);
+ test.same(called_with, ['one','two','three']);
+ console[name] = _console_fn;
+ test.done();
+ };
+};
+
+console_fn_tests('log');
+console_fn_tests('dir');
+/*console_fn_tests('info');
+console_fn_tests('warn');
+console_fn_tests('error');*/
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-async.git
More information about the Pkg-javascript-commits
mailing list