diff options
author | dacoozheng <23788425@qq.com> | 2016-01-03 21:22:14 +0800 |
---|---|---|
committer | dacoozheng <23788425@qq.com> | 2016-01-03 21:22:14 +0800 |
commit | 16d61f49c88b4c3167a0c592f08216cbd23ade8d (patch) | |
tree | c265a781b3be09d32ee46e5c95289d6d7024b30b | |
parent | 4a6ac52b15f7431e01f6f92a9b03dc2fe36e3b8a (diff) | |
download | async-16d61f49c88b4c3167a0c592f08216cbd23ade8d.tar.gz |
Allow "hasOwnProperty" as key for memoize method.
-rw-r--r-- | lib/async.js | 5 | ||||
-rwxr-xr-x | test/test-async.js | 21 |
2 files changed, 20 insertions, 6 deletions
diff --git a/lib/async.js b/lib/async.js index 38da842..8cdb9ca 100644 --- a/lib/async.js +++ b/lib/async.js @@ -1085,16 +1085,17 @@ async.memoize = function (fn, hasher) { var memo = {}; var queues = {}; + var has = Object.prototype.hasOwnProperty; hasher = hasher || identity; var memoized = _restParam(function memoized(args) { var callback = args.pop(); var key = hasher.apply(null, args); - if (memo.hasOwnProperty(key)) { + if (has.call(memo, key)) { async.setImmediate(function () { callback.apply(null, memo[key]); }); } - else if (queues.hasOwnProperty(key)) { + else if (has.call(queues, key)) { queues[key].push(callback); } else { diff --git a/test/test-async.js b/test/test-async.js index 19c30c1..e6fe8ef 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -4314,9 +4314,9 @@ exports['memoize'] = { 'avoid constructor key return undefined': function (test) { test.expect(1); var fn = async.memoize(function(name, callback) { - async.setImmediate(function(){ + setTimeout(function(){ callback(null, name); - }); + }, 100); }); fn('constructor', function(error, results) { test.equal(results, 'constructor'); @@ -4327,14 +4327,27 @@ exports['memoize'] = { 'avoid __proto__ key return undefined': function (test) { test.expect(1); var fn = async.memoize(function(name, callback) { - async.setImmediate(function(){ + setTimeout(function(){ callback(null, name); - }); + }, 100); }); fn('__proto__', function(error, results) { test.equal(results, '__proto__'); test.done(); }); +}, + + 'allow hasOwnProperty as key': function (test) { + test.expect(1); + var fn = async.memoize(function(name, callback) { + setTimeout(function(){ + callback(null, name); + }, 100); + }); + fn('hasOwnProperty', function(error, results) { + test.equal(results, 'hasOwnProperty'); + test.done(); + }); } }; |