From 433333248b3728bf2768baa5641940f0de656670 Mon Sep 17 00:00:00 2001 From: Aaron Heckmann Date: Tue, 6 Sep 2011 21:56:07 -0400 Subject: [PATCH] allow whitelisting by var name Useful in cases when you do not know ahead of time what the value of the global variable leak will be. --- index.js | 10 +++++++--- test/index.js | 9 ++++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index c682c28..2f8012f 100644 --- a/index.js +++ b/index.js @@ -45,10 +45,14 @@ exports.detect = function detect () { Object.keys(global).forEach(function (key) { var w = whitelist.length , bad = true + , white - while (w--) if (global[key] === whitelist[w]) { - bad = false; - break; + while (w--) { + white = whitelist[w]; + if (global[key] === white || 'string' === typeof white && key === white) { + bad = false; + break; + } } if (bad) ret.push(key); diff --git a/test/index.js b/test/index.js index f0916cc..f54d552 100644 --- a/test/index.js +++ b/test/index.js @@ -57,6 +57,14 @@ exports['detect()'] = function () { assert.equal("haha", gleak.detect()[0]); } +exports['unknown values can be whitelisted by passing strings'] = function () { + ignoreme = 1; + assert.ok(~gleak.detect().indexOf('ignoreme')); + gleak.whitelist.push('ignoreme'); + assert.ok(!~gleak.detect().indexOf('ignoreme')); + delete global.ignoreme; +} + exports['print()'] = function () { var write = console.error; var times = 0; @@ -72,7 +80,6 @@ exports['print()'] = function () { } exports['test middleware'] = function (beforeExit) { - var called = false; var req = {}; var res = { send: function (x) { assert.equal(x, 'yes'); called = true; }};