js-object-pretty-print
Advanced tools
Comparing version
@@ -1,2 +0,2 @@ | ||
/*! js-object-pretty-print.js 06-01-2015 */ | ||
"use strict";module.exports.pretty=function(a,b,c,d){var e,f,g,h,i,j,k,l,m,n,o,p,q,r;return h=Object.prototype.toString,i={undefined:"undefined",number:"number","boolean":"boolean",string:"string","[object Function]":"function","[object RegExp]":"regexp","[object Array]":"array","[object Date]":"date","[object Error]":"error"},j=function(a){var b=i[typeof a]||i[h.call(a)]||(a?"object":"null");return b},k=function(a,b){var c,d="";for(c=0;b>c;c+=1)d+=a;return d},m=function(a,b){var c,d=[];b+=e;for(c in a)a.hasOwnProperty(c)&&d.push(b+'"'+c+'": '+q(a[c],b));return d.join(g)+f},n=function(a,b){var c,d=[];b+=e;for(c in a)a.hasOwnProperty(c)&&d.push(b+c+": "+q(a[c],b));return d.join(g)+f},o=function(a,b){var c,d=a.length,h=[];for(b+=e,c=0;d>c;c+=1)h.push(q(a[c],b,b));return h.join(g)+f},p=function(a){var b,c;return a=a.toString(),b=new RegExp("function\\s*.*\\s*\\(.*\\)"),c=b.exec(a),c=c?c[0]:"[object Function]",d?a:'"'+c+'"'},q=function(a,b,c){var d;if(d=j(a),c=c||"",-1===r.indexOf(a))switch(d){case"array":return r.push(a),c+"["+f+o(a,b)+b+"]";case"boolean":return c+(a?"true":"false");case"date":return c+'"'+a.toString()+'"';case"number":return c+a;case"object":return r.push(a),c+"{"+f+l(a,b)+b+"}";case"string":return c+'"'+a+'"';case"function":return c+p(a);default:return c+a.toString()}return c+"circular reference to "+a.toString()},a?(void 0===b&&(b=4),c=(c||"print").toLowerCase(),e=k("html"===c?" ":" ",b),l="json"===c?m:n,f="html"===c?"<br/>":"\n",g=","+f,r=[],q(a,"")+f):"Error: no Javascript object provided"}; | ||
/*! js-object-pretty-print.js 09-02-2015 */ | ||
"use strict";module.exports.pretty=function(a,b,c,d){var e,f,g,h,i,j,k,l,m,n,o,p,q,r;return h=Object.prototype.toString,i={undefined:"undefined",number:"number","boolean":"boolean",string:"string","[object Function]":"function","[object RegExp]":"regexp","[object Array]":"array","[object Date]":"date","[object Error]":"error"},j=function(a){var b=i[typeof a]||i[h.call(a)]||(a?"object":"null");return b},k=function(a,b){var c,d="";for(c=0;b>c;c+=1)d+=a;return d},m=function(a,b){var c,d=[];b+=e;for(c in a)a.hasOwnProperty(c)&&d.push(b+'"'+c+'": '+q(a[c],b));return d.join(g)+f},n=function(a,b){var c,d=[];b+=e;for(c in a)a.hasOwnProperty(c)&&d.push(b+c+": "+q(a[c],b));return d.join(g)+f},o=function(a,b){var c,d=a.length,h=[];for(b+=e,c=0;d>c;c+=1)h.push(q(a[c],b,b));return h.join(g)+f},p=function(a){var b,c;return a=a.toString(),b=new RegExp("function\\s*.*\\s*\\(.*\\)"),c=b.exec(a),c=c?c[0]:"[object Function]",d?a:'"'+c+'"'},q=function(a,b,c){var d;if(d=j(a),c=c||"",-1===r.indexOf(a))switch(d){case"array":return r.push(a),c+"["+f+o(a,b)+b+"]";case"boolean":return c+(a?"true":"false");case"date":return c+'"'+a.toString()+'"';case"number":return c+a;case"object":return r.push(a),c+"{"+f+l(a,b)+b+"}";case"string":return c+'"'+a+'"';case"function":return c+p(a);case"undefined":return c+'"undefined"';default:return a.toString?c+'"'+a.toString()+'"':c+"<<<ERROR>>> Cannot get the string value of the element"}return c+"circular reference to "+a.toString()},a?(void 0===b&&(b=4),c=(c||"print").toLowerCase(),e=k("html"===c?" ":" ",b),l="json"===c?m:n,f="html"===c?"<br/>":"\n",g=","+f,r=[],q(a,"")+f):"Error: no Javascript object provided"}; |
10
index.js
@@ -125,3 +125,3 @@ "use strict"; | ||
case 'string': | ||
return fromArray + '"' + element + '"'; | ||
return fromArray + JSON.stringify(element); | ||
@@ -131,4 +131,10 @@ case 'function': | ||
case 'undefined': | ||
return fromArray + '"undefined"'; | ||
default: | ||
return fromArray + element.toString(); | ||
if (element.toString) { | ||
return fromArray + '"' + element.toString() + '"'; | ||
} | ||
return fromArray + '<<<ERROR>>> Cannot get the string value of the element'; | ||
} | ||
@@ -135,0 +141,0 @@ } |
{ | ||
"name": "js-object-pretty-print", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Serializes a javascript object to a printable string. String is formatted to be used in either pure text environments, like a console log or in HTML or to create a JSON output.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -128,2 +128,2 @@ js-object-pretty-print | ||
* 0.1.3 Circular reference detection, option to print only the signature of functions | ||
* 0.1.4 Better processing of undefined or null values |
@@ -22,2 +22,3 @@ /*global describe, it*/ | ||
'address': address, | ||
'longString': "one\ntwo\n\"three\"", | ||
'favorites': { 'music': ['Mozart', 'Beethoven', 'The Beatles'], 'authors': ['John Grisham', 'Isaac Asimov', 'P.L. Travers'], 'books': [ 'Pelican Brief', 'I, Robot', 'Mary Poppins' ] }, | ||
@@ -35,3 +36,5 @@ 'dates': [ new Date(), new Date("05/25/1954") ], | ||
}, | ||
'onAnother': onAnother | ||
'onAnother': onAnother, | ||
'foo': undefined, | ||
err: new Error('This is an error') | ||
}; | ||
@@ -89,2 +92,6 @@ | ||
}); | ||
it('renders strings with escapes', function () { | ||
assert.notEqual(serialized.indexOf('longString: "one\\ntwo\\n\\"three\\""'), -1); | ||
}); | ||
}); | ||
@@ -91,0 +98,0 @@ |
38618
14.85%11
22.22%493
24.81%