API Docs for: 3.8.0
Show:

File: dump/js/dump.js

  1. /**
  2.  * Returns a simple string representation of the object or array.
  3.  * Other types of objects will be returned unprocessed.  Arrays
  4.  * are expected to be indexed.  Use object notation for
  5.  * associative arrays.
  6.  *
  7.  * If included, the dump method is added to the YUI instance.
  8.  *
  9.  * @module dump
  10.  */

  11.     var L = Y.Lang,
  12.         OBJ = '{...}',
  13.         FUN = 'f(){...}',
  14.         COMMA = ', ',
  15.         ARROW = ' => ',

  16.     /**
  17.      * Returns a simple string representation of the object or array.
  18.      * Other types of objects will be returned unprocessed.  Arrays
  19.      * are expected to be indexed.
  20.      *
  21.      * @method dump
  22.      * @param {Object} o The object to dump.
  23.      * @param {Number} d How deep to recurse child objects, default 3.
  24.      * @return {String} the dump result.
  25.      * @for YUI
  26.      */
  27.     dump = function(o, d) {
  28.         var i, len, s = [], type = L.type(o);

  29.         // Cast non-objects to string
  30.         // Skip dates because the std toString is what we want
  31.         // Skip HTMLElement-like objects because trying to dump
  32.         // an element will cause an unhandled exception in FF 2.x
  33.         if (!L.isObject(o)) {
  34.             return o + '';
  35.         } else if (type == 'date') {
  36.             return o;
  37.         } else if (o.nodeType && o.tagName) {
  38.             return o.tagName + '#' + o.id;
  39.         } else if (o.document && o.navigator) {
  40.             return 'window';
  41.         } else if (o.location && o.body) {
  42.             return 'document';
  43.         } else if (type == 'function') {
  44.             return FUN;
  45.         }

  46.         // dig into child objects the depth specifed. Default 3
  47.         d = (L.isNumber(d)) ? d : 3;

  48.         // arrays [1, 2, 3]
  49.         if (type == 'array') {
  50.             s.push('[');
  51.             for (i = 0, len = o.length; i < len; i = i + 1) {
  52.                 if (L.isObject(o[i])) {
  53.                     s.push((d > 0) ? L.dump(o[i], d - 1) : OBJ);
  54.                 } else {
  55.                     s.push(o[i]);
  56.                 }
  57.                 s.push(COMMA);
  58.             }
  59.             if (s.length > 1) {
  60.                 s.pop();
  61.             }
  62.             s.push(']');
  63.         // regexp /foo/
  64.         } else if (type == 'regexp') {
  65.             s.push(o.toString());
  66.         // objects {k1 => v1, k2 => v2}
  67.         } else {
  68.             s.push('{');
  69.             for (i in o) {
  70.                 if (o.hasOwnProperty(i)) {
  71.                     try {
  72.                         s.push(i + ARROW);
  73.                         if (L.isObject(o[i])) {
  74.                             s.push((d > 0) ? L.dump(o[i], d - 1) : OBJ);
  75.                         } else {
  76.                             s.push(o[i]);
  77.                         }
  78.                         s.push(COMMA);
  79.                     } catch (e) {
  80.                         s.push('Error: ' + e.message);
  81.                     }
  82.                 }
  83.             }
  84.             if (s.length > 1) {
  85.                 s.pop();
  86.             }
  87.             s.push('}');
  88.         }

  89.         return s.join('');
  90.     };

  91.     Y.dump = dump;
  92.     L.dump = dump;


  93.