[Pkg-javascript-commits] [node-async] 101/480: Add unmemoize function

Jonas Smedegaard js at moszumanska.debian.org
Fri May 2 08:58:16 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 bdd631e104a848ad163ea10b308a61d5b4e04659
Author: Balint Erdi <balint at secretsaucepartners.com>
Date:   Thu Oct 6 17:22:45 2011 +0200

    Add unmemoize function
---
 lib/async.js       | 10 +++++++++-
 test/test-async.js | 25 +++++++++++++++++++++++++
 2 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/lib/async.js b/lib/async.js
index 73eddc8..3b202b6 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -614,7 +614,7 @@
         hasher = hasher || function (x) {
             return x;
         };
-        return function () {
+        var memoized = function () {
             var args = Array.prototype.slice.call(arguments);
             var callback = args.pop();
             var key = hasher.apply(null, args);
@@ -628,6 +628,14 @@
                 }]));
             }
         };
+        memoized.original = fn;
+        return memoized;
     };
 
+    async.unmemoize = function (fn) {
+      return function () {
+        return fn.original.apply(null, arguments);
+      }
+    }
+
 }());
diff --git a/test/test-async.js b/test/test-async.js
index 4a7f986..4c8e420 100644
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -1253,6 +1253,31 @@ exports['memoize'] = function (test) {
     test.done();
 };
 
+exports['unmemoize'] = 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);
+  var fn3 = async.unmemoize(fn2);
+  fn3(1, 2, function (err, result) {
+      test.equal(result, 3);
+  });
+  fn3(1, 2, function (err, result) {
+      test.equal(result, 3);
+  });
+  fn3(2, 2, function (err, result) {
+      test.equal(result, 4);
+  });
+
+  test.same(call_order, [['fn',1,2], ['fn',1,2], ['fn',2,2]]);
+  test.done();
+}
+
 exports['memoize error'] = function (test) {
     test.expect(1);
     var testerr = new Error('test');

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