Skip to content

Instantly share code, notes, and snippets.

@carymrobbins
Forked from jacob414/repr.coffee
Last active August 29, 2015 14:00
Show Gist options
  • Save carymrobbins/720fbf32d84491d2fc36 to your computer and use it in GitHub Desktop.
Save carymrobbins/720fbf32d84491d2fc36 to your computer and use it in GitHub Desktop.
var repr = function (o, depth, max) {
var result, i;
depth = depth === undefined ? 0 : depth;
max = max === undefined ? 2 : max;
if (depth > max) {
return '<..>';
}
switch (typeof o) {
case 'string': return '"' + o.replace(/"/g, '\\"') + '"';
case 'function': return o.toString();
case 'object':
if (o === null) {
return null;
}
if (Object.prototype.toString.call(o) === '[object Array]') {
result = [];
for (i in o) {
if (o.hasOwnProperty(i)) {
result.push(repr(o[i], depth + 1, max));
}
}
return '[' + result + ']';
}
result = [];
for (i in o) {
if (o.hasOwnProperty(i)) {
result.push(i + ': ' + repr(o[i], depth + 1, max));
}
}
return '{' + result + '}';
case 'undefined': return 'undefined';
default: return o;
}
};
var type = typeof exports;
if (type !== 'undefined') {
exports.repr = repr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment