Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

even better pretty printing

  • Loading branch information...
commit e9d806c0b4704d36d2fd14b9b9bc6b893c5b69be 1 parent 6504f7a
Nick Kallen authored
Showing with 28 additions and 10 deletions.
  1. +28 −10 lib/jquery.print.js
View
38 lib/jquery.print.js
@@ -3,7 +3,7 @@
function print_array(obj, opts) {
var result = [];
for (var i = 0; i < Math.min(opts.max_array, obj.length); i++)
- result.push($.print(obj[i], $.extend({}, opts, { max_array: 3, max_string: 25 })));
+ result.push($.print(obj[i], $.extend({}, opts, { max_array: 3, max_string: 40 })));
if (obj.length > opts.max_array)
result.push((obj.length - opts.max_array) + ' more...');
@@ -12,10 +12,21 @@
}
function print_element(obj) {
- return "<" + obj.tagName.toLowerCase() +
- (obj.className != "" ? " class='" + obj.className + "'" : "") +
- (obj.id != "" ? " id='" + obj.id + "'" : "") +
- ">";
+ if (obj.nodeType == 1) {
+ var result = [];
+ var properties = [ 'className', 'id' ];
+ var extra = {
+ 'input': ['type', 'name', 'value'],
+ 'a': ['href', 'target'],
+ 'form': ['method', 'action']
+ };
+ $.each(properties.concat(extra[obj.tagName.toLowerCase()] || []), function() {
+ if (obj[this])
+ result.push(' ' + this.replace('className', 'class') + "=" + $.print(obj[this]))
+ });
+ return "<" + obj.tagName.toLowerCase()
+ + result.join('') + ">";
+ }
}
function print_object(obj, opts) {
@@ -23,9 +34,9 @@
var result = [], key, value;
for (var k in obj) {
- if (seen.indexOf(obj[k]) < 0) {
+ if (obj.hasOwnProperty(k) && $.inArray(obj[k], seen) < 0) {
seen.push(obj[k]);
- value = $.print(obj[k], $.extend({}, opts, { max_array: 3, max_string: 25, seen: seen }));
+ value = $.print(obj[k], $.extend({}, opts, { max_array: 6, max_string: 40, seen: seen }));
} else
value = "...";
result.push(k + ": " + value);
@@ -64,27 +75,34 @@
}
$.print = function(obj, options) {
- var opts = $.extend({}, { max_array: 6, max_string: 100 }, options);
+ var opts = $.extend({}, { max_array: 10, max_string: 100 }, options);
if (typeof obj == 'undefined')
return "undefined";
else if (typeof obj == 'boolean')
return obj.toString();
+ else if (!obj && typeof obj == 'number')
+ return 'NaN';
else if (!obj)
return "null";
else if (typeof obj == 'string')
return print_string(obj, opts);
- else if ($.isFunction(obj))
+ else if (obj instanceof RegExp)
+ return obj.toString();
+ else if (typeof obj == 'function' || obj instanceof Function)
return obj.toString().match(/^([^)]*\))/)[1];
else if (obj instanceof Array)
return print_array(obj, opts);
- else if (obj instanceof HTMLElement)
+ else if (obj.nodeType)
return print_element(obj);
else if (obj instanceof jQuery)
return "$(" + $.print(obj.get()) + ")";
+ else if (obj instanceof Error)
+ return print_object(obj, $.extend({}, options, { max_string: 200 }));
else if (obj instanceof Object)
return print_object(obj, opts);
else
return obj.toString().replace(/\n\s*/g, '');
}
+
})(jQuery);
Please sign in to comment.
Something went wrong with that request. Please try again.