Created
October 18, 2016 16:12
-
-
Save oleics/d28c0beee49ed6f8f6248d2e20b41dcc to your computer and use it in GitHub Desktop.
nunjucks filter
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
nunjucksEnv.addFilter('debug', function(obj){ | |
return '<pre>'+jsonStringifySafe(obj, ' ')+'</pre>'; | |
}); |
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 jsonStringifySafe(d, space) { | |
var cache = []; | |
d = JSON.stringify(d, function(key, value) { | |
if(typeof value === 'object' && value !== null) { | |
if(cache.indexOf(value) !== -1) { | |
return '[*circular]'; | |
} | |
cache.push(value); | |
} | |
return value; | |
}, space); | |
cache = null; | |
return d; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment