[Pkg-javascript-commits] [node-async] 62/480: added memoize

Jonas Smedegaard js at moszumanska.debian.org
Fri May 2 08:58:12 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 2bc2ec32f2b49048ab975bc25c09cc46b0273c9c
Author: Caolan McMahon <caolan at caolanmcmahon.com>
Date:   Wed Nov 24 20:06:16 2010 +0000

    added memoize
---
 lib/async.js       | 21 +++++++++++++++++++
 test/test-async.js | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 79 insertions(+), 1 deletion(-)

diff --git a/lib/async.js b/lib/async.js
index c8fccc3..5314372 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -599,4 +599,25 @@
     async.warn = _console_fn('warn');
     async.error = _console_fn('error');*/
 
+    async.memoize = function (fn, hasher) {
+        var memo = {};
+        hasher = hasher || function (x) {
+            return x;
+        };
+        return function () {
+            var args = Array.prototype.slice.call(arguments);
+            var callback = args.pop();
+            var key = hasher.apply(null, args);
+            if (key in memo) {
+                callback.apply(null, memo[key]);
+            }
+            else {
+                fn.apply(null, args.concat([function () {
+                    memo[key] = arguments;
+                    callback.apply(null, arguments);
+                }]));
+            }
+        };
+    };
+
 }());
diff --git a/test/test-async.js b/test/test-async.js
index b1c1f56..60b1d81 100644
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -862,7 +862,9 @@ var console_fn_tests = function(name){
     }
 
     exports[name + ' without console.' + name] = function(test){
-        if (typeof global === 'undefined') var global = window;
+        if (typeof global === 'undefined') {
+            global = window;
+        }
         var _console = global.console;
         global.console = undefined;
         var fn = function(callback){callback(null, 'val');};
@@ -1214,3 +1216,58 @@ exports['queue push without callback'] = function (test) {
         test.done();
     }, 200);
 };
+
+exports['memoize'] = function (test) {
+    test.expect(4);
+    var call_order = [];
+
+    var fn = function (arg1, arg2, callback) {
+        call_order.push(['fn', arg1, arg2]);
+        callback(null, arg1 + arg2);
+    };
+
+    var fn2 = async.memoize(fn);
+    fn2(1, 2, function (err, result) {
+        test.equal(result, 3);
+    });
+    fn2(1, 2, function (err, result) {
+        test.equal(result, 3);
+    });
+    fn2(2, 2, function (err, result) {
+        test.equal(result, 4);
+    });
+
+    test.same(call_order, [['fn',1,2], ['fn',2,2]]);
+    test.done();
+};
+
+exports['memoize error'] = function (test) {
+    test.expect(1);
+    var testerr = new Error('test');
+    var fn = function (arg1, arg2, callback) {
+        callback(testerr, arg1 + arg2);
+    };
+    async.memoize(fn)(1, 2, function (err, result) {
+        test.equal(err, testerr);
+    });
+    test.done();
+};
+
+exports['memoize custom hash function'] = function (test) {
+    test.expect(2);
+    var testerr = new Error('test');
+
+    var fn = function (arg1, arg2, callback) {
+        callback(testerr, arg1 + arg2);
+    };
+    var fn2 = async.memoize(fn, function () {
+        return 'custom hash';
+    });
+    fn2(1, 2, function (err, result) {
+        test.equal(result, 3);
+    });
+    fn2(2, 2, function (err, result) {
+        test.equal(result, 3);
+    });
+    test.done();
+};

-- 
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