Skip to content

Commit

Permalink
Allow "hasOwnProperty" as key for memoize method.
Browse files Browse the repository at this point in the history
  • Loading branch information
dacoozheng committed Jan 3, 2016
1 parent 4a6ac52 commit 16d61f4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
5 changes: 3 additions & 2 deletions lib/async.js
Original file line number Diff line number Diff line change
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 (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 {
Expand Down
21 changes: 17 additions & 4 deletions test/test-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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();
});
}

};
Expand Down

0 comments on commit 16d61f4

Please sign in to comment.