dump.js revision 0fe0f31907b611916873c8eb6ac7b483b4c90d5e
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson// requires lang
c3a558e7c77127215b010652905be7916ec5a080Sue GleesonYUI.add("dump", function(Y) {
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson var L=Y.Lang, OBJ="{...}", FUN="f(){...}", COMMA=', ', ARROW=' => ',
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson /**
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson * Returns a simple string representation of the object or array.
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson * Other types of objects will be returned unprocessed. Arrays
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson * are expected to be indexed. Use object notation for
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson * associative arrays.
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson *
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson * @todo dumping a window is causing an unhandled exception in
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson * FireFox. Trying to account for it is hanging FireFox.
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson * Could be a FireBug interaction.
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson *
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson * @method dump
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson * @param o {Object} The object to dump
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson * @param d {int} How deep to recurse child objects, default 3
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson * @return {String} the dump result
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson */
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson dump = function(o, d) {
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson var i, len, s = [];
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson // Cast non-objects to string
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson // Skip dates because the std toString is what we want
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson // Skip HTMLElement-like objects because trying to dump
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson // an element will cause an unhandled exception in FF 2.x
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson if (!L.isObject(o)) {
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson return o + "";
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson } else if (o instanceof Date || ("nodeType" in o && "tagName" in o)) {
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson return o;
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson } else if (L.isFunction(o)) {
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson return FUN;
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson }
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson // dig into child objects the depth specifed. Default 3
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson d = (L.isNumber(d)) ? d : 3;
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson // arrays [1, 2, 3]
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson if (L.isArray(o)) {
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson s.push("[");
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson for (i=0,len=o.length;i<len;i=i+1) {
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson if (L.isObject(o[i])) {
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson s.push((d > 0) ? L.dump(o[i], d-1) : OBJ);
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson } else {
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson s.push(o[i]);
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson }
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson s.push(COMMA);
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson }
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson if (s.length > 1) {
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson s.pop();
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson }
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson s.push("]");
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson // objects {k1 => v1, k2 => v2}
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson } else {
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson s.push("{");
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson for (i in o) {
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson if (Y.Object.owns(o, i)) {
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson s.push(i + ARROW);
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson if (L.isObject(o[i])) {
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson s.push((d > 0) ? L.dump(o[i], d-1) : OBJ);
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson } else {
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson s.push(o[i]);
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson }
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson s.push(COMMA);
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson }
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson }
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson if (s.length > 1) {
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson s.pop();
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson }
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson s.push("}");
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson }
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson return s.join("");
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson };
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson Y.dump = dump;
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson L.dump = dump;
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson}, "3.0.0");
c3a558e7c77127215b010652905be7916ec5a080Sue Gleeson