Skip to content

Instantly share code, notes, and snippets.

@bitifet
Last active November 3, 2015 07:35
Show Gist options
  • Select an option

  • Save bitifet/5087dc1ced686c426858 to your computer and use it in GitHub Desktop.

Select an option

Save bitifet/5087dc1ced686c426858 to your computer and use it in GitHub Desktop.
Circular-references aware inspect() function.
// util.inspect() alternative returning always valid JSON even having circular refernces.
// Only for debugging and inspection purposes.
// Intended to be used in conjunction with underscore. Ex: "underscore print --color"
// Also useful with my lovely javascript inspection vim mappings:
// https://github.com/bitifet/dotfiles/blob/master/.vim/after/syntax/javascript/SyntaxInclude.vim
var inspect = (function(){
var utilInspect = require("util").inspect;
return function inspect (obj) {
var result;
var str = "var Circular = \"(->CIRCULAR<-)\";\n";
str += "result = JSON.stringify(";
str += utilInspect (obj, {showHidden: false, depth: null})
str += ");"
eval(str);
return result;
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment