Code coverage report for master/lib/jsdoc/util/dumper.js

Statements: 91.67% (66 / 72)      Branches: 84.44% (38 / 45)      Functions: 100% (14 / 14)      Lines: 92.96% (66 / 71)      Ignored: none     

All files » master/lib/jsdoc/util/ » dumper.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147                  1   1 1   1 22     1 40   40 40         40     1 39 39               1 39 39               1 136     1 117     1 114       1 40 1     39 39       1 136   136   136     136 2   134 12 12   12 29     12   12     122 2   120 2   118 1   117 3   114 28 27   27 85 85     27   27         86     136           1 21 21 21   21 22 22     21    
/*global Set */
/**
 * Recursively print out all names and values in a data structure.
 * @module jsdoc/util/dumper
 * @author Michael Mathews <micmath@gmail.com>
 * @license Apache License 2.0 - See file 'LICENSE.md' in this project.
 */
'use strict';
 
var util = require('util');
 
var OBJECT_WALKER_KEY = 'hasBeenSeenByWalkerDumper';
var SET_DEFINED = (typeof Set !== 'undefined');
 
function ObjectWalker() {
    this.seenItems = SET_DEFINED ? new Set() : [];
}
 
ObjectWalker.prototype.seen = function(object) {
    var result;
 
    Eif (SET_DEFINED) {
        result = this.seenItems.has(object);
    }
    else {
        result = object[OBJECT_WALKER_KEY];
    }
    return result;
};
 
ObjectWalker.prototype.markAsSeen = function(object) {
    Eif (SET_DEFINED) {
        this.seenItems.add(object);
    }
    else {
        object[OBJECT_WALKER_KEY] = true;
        this.seenItems.push(object);
    }
};
 
ObjectWalker.prototype.removeSeenFlag = function(obj) {
    Eif (SET_DEFINED) {
        this.seenItems.delete(obj);
    }
    else {
        delete obj[OBJECT_WALKER_KEY];
    }
};
 
// some objects are unwalkable, like Java native objects
ObjectWalker.prototype.isUnwalkable = function(o) {
    return (o && typeof o === 'object' && typeof o.constructor === 'undefined');
};
 
ObjectWalker.prototype.isFunction = function(o) {
    return (o && typeof o === 'function' || o instanceof Function);
};
 
ObjectWalker.prototype.isObject = function(o) {
    return o && o instanceof Object ||
        (o && typeof o.constructor !== 'undefined' && o.constructor.name === 'Object');
};
 
ObjectWalker.prototype.checkCircularRefs = function(o, func) {
    if ( this.seen(o) ) {
        return '<CircularRef>';
    }
    else {
        this.markAsSeen(o);
        return func(o);
    }
};
 
ObjectWalker.prototype.walk = function(o) {
    var result;
 
    var self = this;
 
    Iif ( this.isUnwalkable(o) ) {
        result = '<Object>';
    }
    else if ( o === undefined ) {
        result = null;
    }
    else if ( Array.isArray(o) ) {
        result = this.checkCircularRefs(o, function(arr) {
            var newArray = [];
 
            arr.forEach(function(item) {
                newArray.push( self.walk(item) );
            });
 
            self.removeSeenFlag(arr);
 
            return newArray;
        });
    }
    else if ( util.isRegExp(o) ) {
        result = '<RegExp ' + o + '>';
    }
    else if ( util.isDate(o) ) {
        result = '<Date ' + o.toUTCString() + '>';
    }
    else if ( util.isError(o) ) {
        result = { message: o.message };
    }
    else if ( this.isFunction(o) ) {
        result = '<Function' + (o.name ? ' ' + o.name : '') + '>';
    }
    else if ( this.isObject(o) && o !== null ) {
        result = this.checkCircularRefs(o, function(obj) {
            var newObj = {};
 
            Object.keys(obj).forEach(function(key) {
                Iif (!SET_DEFINED && key === OBJECT_WALKER_KEY) { return; }
                newObj[key] = self.walk(obj[key]);
            });
 
            self.removeSeenFlag(obj);
 
            return newObj;
        });
    }
    // should be safe to JSON.stringify() everything else
    else {
        result = o;
    }
 
    return result;
};
 
/**
 * @param {*} object
 */
exports.dump = function() {
    var args = Array.prototype.slice.call(arguments, 0);
    var result = [];
    var walker;
 
    for (var i = 0, l = args.length; i < l; i++) {
        walker = new ObjectWalker();
        result.push( JSON.stringify(walker.walk(args[i]), null, 4) );
    }
 
    return result.join('\n');
};