diff options
author | Alexander Early <alexander.early@gmail.com> | 2016-01-07 14:56:03 -0800 |
---|---|---|
committer | Alexander Early <alexander.early@gmail.com> | 2016-01-07 14:56:03 -0800 |
commit | ed308256f5810ea015bb7673bcfe3d29545786ff (patch) | |
tree | dcec32a3b05c6416b5b27a524e2331d5b2654ad1 | |
parent | fc57bd062a04c8128db2f133559564cbc318be29 (diff) | |
parent | 16d61f49c88b4c3167a0c592f08216cbd23ade8d (diff) | |
download | async-ed308256f5810ea015bb7673bcfe3d29545786ff.tar.gz |
Merge pull request #998 from dacoozheng/master
prevent return "undefined" immediately when key is "constructor" for memoize method
-rw-r--r-- | lib/async.js | 5 | ||||
-rwxr-xr-x | test/test-async.js | 39 |
2 files changed, 42 insertions, 2 deletions
diff --git a/lib/async.js b/lib/async.js index 3b212cc..31e7620 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 (key in memo) { + if (has.call(memo, key)) { async.setImmediate(function () { callback.apply(null, memo[key]); }); } - else if (key in queues) { + else if (has.call(queues, key)) { queues[key].push(callback); } else { diff --git a/test/test-async.js b/test/test-async.js index 05c0228..e6fe8ef 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -4309,6 +4309,45 @@ exports['memoize'] = { test.equal(val, "bar"); test.done(); }); +}, + + 'avoid constructor key return undefined': function (test) { + test.expect(1); + var fn = async.memoize(function(name, callback) { + setTimeout(function(){ + callback(null, name); + }, 100); + }); + fn('constructor', function(error, results) { + test.equal(results, 'constructor'); + test.done(); + }); +}, + + 'avoid __proto__ key return undefined': function (test) { + test.expect(1); + var fn = async.memoize(function(name, callback) { + 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(); + }); } }; |