Created
March 28, 2013 15:11
-
-
Save AutoSponge/5263915 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (global, known) { | |
global.dev = global.dev || {}; | |
function register(str) { | |
known[str] = str; | |
} | |
function raiseError(leaks) { | |
if (leaks && leaks.length) { | |
throw new Error("global leak" + | |
(leaks.length > 1 ? "s" : "") + | |
" detected: " + leaks.join(", ")); | |
} | |
} | |
function fromKnown(key) { | |
return key !== known[key]; | |
} | |
global.dev.checkGlobals = function (debug) { | |
var obj = {}, type, val, leaks = Object.keys(global).filter(fromKnown); | |
if (debug) { | |
leaks.forEach(function (leak) { | |
val = global[leak]; | |
type = typeof val; | |
obj[type] = obj[type] || {}; | |
obj[type][leak] = val; | |
}); | |
console.log("global leak detected:"); | |
console.dir(obj); | |
} else { | |
raiseError(leaks); | |
} | |
}; | |
global.dev.registerAllGlobals = function () { | |
Object.keys(global).forEach(register); | |
}; | |
global.dev.registerGlobal = register; | |
}(this, {})); | |
if (Object.keys && Array.prototype.forEach) { | |
dev.registerAllGlobals(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment