Skip to content

Commit

Permalink
Merge pull request #998 from dacoozheng/master
Browse files Browse the repository at this point in the history
prevent return "undefined" immediately when key is "constructor" for memoize method
  • Loading branch information
aearly committed Jan 7, 2016
2 parents fc57bd0 + 16d61f4 commit ed30825
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/async.js
Expand Up @@ -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 {
Expand Down
39 changes: 39 additions & 0 deletions test/test-async.js
Expand Up @@ -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();
});
}

};
Expand Down

0 comments on commit ed30825

Please sign in to comment.