dump.js revision bf84ae48e272b3ddc103e8da28cc9ed7f30a9ba2
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync/**
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * Returns a simple string representation of the object or array.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * Other types of objects will be returned unprocessed. Arrays
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * are expected to be indexed. Use object notation for
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * associative arrays.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync *
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * If included, the dump method is added to the YUI instance.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync *
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * @module dump
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync */
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync var L=Y.Lang, OBJ='{...}', FUN='f(){...}', COMMA=', ', ARROW=' => ',
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync /**
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * The following methods are added to the YUI instance
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * @class YUI~dump
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync */
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync /**
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * Returns a simple string representation of the object or array.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * Other types of objects will be returned unprocessed. Arrays
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * are expected to be indexed. Use object notation for
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * associative arrays.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync *
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * @TODO dumping a window is causing an unhandled exception in
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * FireFox.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync *
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * This method is in the 'dump' module, which is not bundled with
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * the core YUI object
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync *
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * @method dump
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * @param o {object} The object to dump
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * @param d {int} How deep to recurse child objects, default 3
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * @return {string} the dump result
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync */
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync dump = function(o, d) {
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync var i, len, s = [], type = L.type(o);
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync // Cast non-objects to string
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync // Skip dates because the std toString is what we want
// Skip HTMLElement-like objects because trying to dump
// an element will cause an unhandled exception in FF 2.x
if (!L.isObject(o)) {
return o + "";
} else if (type == "date" || ("nodeType" in o && "tagName" in o)) {
return o;
} else if (type == "function") {
return FUN;
}
// dig into child objects the depth specifed. Default 3
d = (L.isNumber(d)) ? d : 3;
// arrays [1, 2, 3]
if (type == "array") {
s.push("[");
for (i=0,len=o.length;i<len;i=i+1) {
if (L.isObject(o[i])) {
s.push((d > 0) ? L.dump(o[i], d-1) : OBJ);
} else {
s.push(o[i]);
}
s.push(COMMA);
}
if (s.length > 1) {
s.pop();
}
s.push("]");
// regexp /foo/
} else if (type == "regexp") {
s.push(o.toString());
// objects {k1 => v1, k2 => v2}
} else {
s.push("{");
for (i in o) {
if (o.hasOwnProperty(i)) {
try {
s.push(i + ARROW);
if (L.isObject(o[i])) {
s.push((d > 0) ? L.dump(o[i], d-1) : OBJ);
} else {
s.push(o[i]);
}
s.push(COMMA);
} catch(e) {
s.push('Error: ' + e.message);
}
}
}
if (s.length > 1) {
s.pop();
}
s.push("}");
}
return s.join("");
};
Y.dump = dump;
L.dump = dump;