Permalink
Browse files

Merge branch 'master' of git://github.com/tmm1/screw-unit into tmm1/m…

…aster
  • Loading branch information...
2 parents 2b4ae48 + 4e47ead commit f297a72fd336f03045b151cd98d1e86c6a001fa2 Nick Kallen committed May 11, 2008
Showing with 121 additions and 19 deletions.
  1. +121 −19 lib/jquery.print.js
View
@@ -1,24 +1,126 @@
(function($) {
- $.print = function(obj) {
- if (obj instanceof Function) {
- return obj.toString().match(/^([^\{]*) {/)[1];
- } else if(obj instanceof Array) {
- var result = [];
- for (var i = 0; i < obj.length; i++) {
- result.push($.print(obj[i]));
- }
- return "[" + result.join(", ") + "]";
- } else if(obj instanceof HTMLElement) {
- return "<" + obj.tagName + " " + (obj.className != "" ? "class='" + obj.className + "'" : "") +
- (obj.id != "" ? "id='" + obj.id + "'" : "") + ">";
- } else if(obj instanceof Object) {
- var result = [];
- for (var k in obj) {
- result.push(k + ": " + $.print(obj[k]))
+
+ // helper functions
+
+ function print_array(obj, opts){
+ opts.maxString = 25
+ var max = obj.length > opts.maxArray ? opts.maxArray : obj.length
+
+ var result = [];
+ for (var i = 0; i < max; i++) {
+ result.push($.print(obj[i], opts));
+ }
+
+ if (obj.length > max)
+ result.push('... ' + (obj.length - max) + ' more')
+
+ if (result.length == 0) return "[]"
+ return "[ " + result.join(", ") + " ]"
+ }
+
+ function print_element(obj){
+ return "<" + obj.tagName.toLowerCase() +
+ (obj.className != "" ? " class='" + obj.className + "'" : "") +
+ (obj.id != "" ? " id='" + obj.id + "'" : "") +
+ ">"
+ }
+
+ function print_object(obj, opts){
+ opts.maxString = 25
+ if (!opts.seen) opts.seen = []
+
+ var result = []
+ for (var k in obj) {
+ if (opts.seen.indexOf(obj[k]) == -1) {
+ opts.seen.push(obj[k])
+ result.push(k + ": " + $.print(obj[k], opts))
}
- return "{" + result.join(", ") + "}"
- } else {
- return obj.toString().replace(/\n\s*/g, "");
}
+
+ if (result.length == 0) return "{}"
+ return "{ " + result.join(", ") + " }"
+ }
+
+ function print_jquery(obj){
+ var result = []
+ obj.each(function(){ result.push(this) })
+
+ return "jQuery(length="+obj.length+")" + $.print(result)
+ }
+
+ function print_string(value, opts){
+ // adapted from json2.js
+ var m = { // table of character substitutions
+ '\b': '\\b',
+ '\t': '\\t',
+ '\n': '\\n',
+ '\f': '\\f',
+ '\r': '\\r',
+ '"' : '\\"',
+ '\\': '\\\\'
+ }
+
+ var r = /["\\\x00-\x1f\x7f-\x9f]/g
+
+ // If the string contains no control characters, no quote characters, and no
+ // backslash characters, then we can safely slap some quotes around it.
+ // Otherwise we must also replace the offending characters with safe sequences.
+
+ var str = r.test(value) ?
+ '"' + value.replace(r, function (a) {
+ var c = m[a];
+ if (c) {
+ return c;
+ }
+ c = a.charCodeAt();
+ return '\\u00' + Math.floor(c / 16).toString(16) +
+ (c % 16).toString(16);
+ }) + '"' :
+ '"' + value + '"';
+
+
+ if (opts.maxString && str.length > opts.maxString)
+ return str.slice(0, opts.maxString) + '..."'
+ else
+ return str
+ }
+
+ $.print = function(obj, options) {
+ var opts = $.extend({}, $.print.options, options || {})
+
+ if (typeof obj == 'undefined')
+ return "undefined"
+
+ else if (typeof obj == 'boolean')
+ return obj.toString()
+
+ else if (!obj)
+ return "null"
+
+ else if (typeof obj == 'string')
+ return print_string(obj, opts)
+
+ else if ($.isFunction(obj))
+ return obj.toString().match(/^([^\{]*)\s{/)[1]
+
+ else if (obj instanceof Array)
+ return print_array(obj, opts)
+
+ else if (obj instanceof HTMLElement)
+ return print_element(obj)
+
+ else if (obj instanceof jQuery)
+ return print_jquery(obj)
+
+ else if (obj instanceof Object)
+ return print_object(obj, opts)
+
+ else
+ return obj.toString().replace(/\n\s*/g, '')
+ }
+
+ $.print.options = {
+ maxArray: 6,
+ maxString: 0
}
})(jQuery);

0 comments on commit f297a72

Please sign in to comment.