Skip to content

Instantly share code, notes, and snippets.

@felthy
Created April 21, 2015 06:27

Revisions

  1. felthy renamed this gist Apr 21, 2015. 1 changed file with 0 additions and 0 deletions.
  2. felthy created this gist Apr 21, 2015.
    35 changes: 35 additions & 0 deletions javascript-debug-printObject-function
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    /**
    * When investigating a selenium test failure on a remote headless browser that couldn't be reproduced
    * locally, I wanted to add some javascript to the site under test that would dump some state to the
    * page (so it could be captured by Selenium as a screenshot when the test failed). JSON.stringify()
    * didn't work because the object declared a toJSON() method, and JSON.stringify() just calls that
    * method if it's present. This was a Moment object, so toJSON() returned a string but I wanted to see
    * the internal state of the object instead.
    *
    * So, this is a rough and ready function that recursively dumps any old javascript object.
    */
    function printObject(o, indent) {
    var out = '';
    if (typeof indent === 'undefined') {
    indent = 0;
    }
    for (var p in o) {
    if (o.hasOwnProperty(p)) {
    var val = o[p];
    out += new Array(4 * indent + 1).join(' ') + p + ': ';
    if (typeof val === 'object') {
    if (val instanceof Date) {
    out += 'Date "' + val.toISOString() + '"';
    } else {
    out += '{\n' + printObject(val, indent + 1) + new Array(4 * indent + 1).join(' ') + '}';
    }
    } else if (typeof val === 'function') {

    } else {
    out += '"' + val + '"';
    }
    out += ',\n';
    }
    }
    return out;
    }