js-object-pretty-print
Advanced tools
Comparing version
@@ -0,0 +0,0 @@ { |
@@ -0,0 +0,0 @@ module.exports = function (grunt) { |
/*! 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"}; |
143
index.js
"use strict"; | ||
module.exports.pretty = function (jsObject, indentLength, outputTo, fullFunction) { | ||
module.exports.pretty = function(jsObject, indentLength, outputTo, fullFunction) { | ||
var indentString, | ||
@@ -22,14 +22,58 @@ newLine, | ||
TYPES = { | ||
'undefined' : 'undefined', | ||
'number' : 'number', | ||
'boolean' : 'boolean', | ||
'string' : 'string', | ||
'undefined': 'undefined', | ||
'number': 'number', | ||
'boolean': 'boolean', | ||
'string': 'string', | ||
'[object Function]': 'function', | ||
'[object RegExp]' : 'regexp', | ||
'[object Array]' : 'array', | ||
'[object Date]' : 'date', | ||
'[object Error]' : 'error' | ||
'[object RegExp]': 'regexp', | ||
'[object Array]': 'array', | ||
'[object Date]': 'date', | ||
'[object Error]': 'error' | ||
}; | ||
valueType = function (o) { | ||
if (!Object.keys) { | ||
Object.keys = (function() { | ||
'use strict'; | ||
var hasOwnProperty = Object.prototype.hasOwnProperty, | ||
hasDontEnumBug = !({ | ||
toString: null | ||
}).propertyIsEnumerable('toString'), | ||
dontEnums = [ | ||
'toString', | ||
'toLocaleString', | ||
'valueOf', | ||
'hasOwnProperty', | ||
'isPrototypeOf', | ||
'propertyIsEnumerable', | ||
'constructor' | ||
], | ||
dontEnumsLength = dontEnums.length; | ||
return function(obj) { | ||
if (typeof obj !== 'function' && (typeof obj !== 'object' || obj === null)) { | ||
throw new TypeError('Object.keys called on non-object'); | ||
} | ||
var result = [], | ||
prop, i; | ||
for (prop in obj) { | ||
if (hasOwnProperty.call(obj, prop)) { | ||
result.push(prop); | ||
} | ||
} | ||
if (hasDontEnumBug) { | ||
for (i = 0; i < dontEnumsLength; i++) { | ||
if (hasOwnProperty.call(obj, dontEnums[i])) { | ||
result.push(dontEnums[i]); | ||
} | ||
} | ||
} | ||
return result; | ||
}; | ||
}()); | ||
} | ||
valueType = function(o) { | ||
var type = TYPES[typeof o] || TYPES[TOSTRING.call(o)] || (o ? 'object' : 'null'); | ||
@@ -39,3 +83,3 @@ return type; | ||
repeatString = function (src, length) { | ||
repeatString = function(src, length) { | ||
var dst = '', | ||
@@ -50,3 +94,3 @@ index; | ||
prettyObjectJSON = function (object, indent) { | ||
prettyObjectJSON = function(object, indent) { | ||
var value = [], | ||
@@ -56,7 +100,5 @@ property; | ||
indent += indentString; | ||
for (property in object) { | ||
if (object.hasOwnProperty(property)) { | ||
value.push(indent + '"' + property + '": ' + pretty(object[property], indent)); | ||
} | ||
} | ||
Object.keys(object).forEach(function (property) { | ||
value.push(indent + '"' + property + '": ' + pretty(object[property], indent)); | ||
}); | ||
@@ -66,3 +108,3 @@ return value.join(newLineJoin) + newLine; | ||
prettyObjectPrint = function (object, indent) { | ||
prettyObjectPrint = function(object, indent) { | ||
var value = [], | ||
@@ -72,12 +114,9 @@ property; | ||
indent += indentString; | ||
for (property in object) { | ||
if (object.hasOwnProperty(property)) { | ||
value.push(indent + property + ': ' + pretty(object[property], indent)); | ||
} | ||
} | ||
Object.keys(object).forEach(function (property) { | ||
value.push(indent + property + ': ' + pretty(object[property], indent)); | ||
}); | ||
return value.join(newLineJoin) + newLine; | ||
}; | ||
prettyArray = function (array, indent) { | ||
prettyArray = function(array, indent) { | ||
var index, | ||
@@ -95,3 +134,3 @@ length = array.length, | ||
functionSignature = function (element) { | ||
functionSignature = function(element) { | ||
var signatureExpression, | ||
@@ -107,3 +146,3 @@ signature; | ||
pretty = function (element, indent, fromArray) { | ||
pretty = function(element, indent, fromArray) { | ||
var type; | ||
@@ -115,36 +154,36 @@ | ||
switch (type) { | ||
case 'array': | ||
visited.push(element); | ||
return fromArray + '[' + newLine + prettyArray(element, indent) + indent + ']'; | ||
case 'array': | ||
visited.push(element); | ||
return fromArray + '[' + newLine + prettyArray(element, indent) + indent + ']'; | ||
case 'boolean': | ||
return fromArray + (element ? 'true' : 'false'); | ||
case 'boolean': | ||
return fromArray + (element ? 'true' : 'false'); | ||
case 'date': | ||
return fromArray + '"' + element.toString() + '"'; | ||
case 'date': | ||
return fromArray + '"' + element.toString() + '"'; | ||
case 'number': | ||
return fromArray + element; | ||
case 'number': | ||
return fromArray + element; | ||
case 'object': | ||
visited.push(element); | ||
return fromArray + '{' + newLine + prettyObject(element, indent) + indent + '}'; | ||
case 'object': | ||
visited.push(element); | ||
return fromArray + '{' + newLine + prettyObject(element, indent) + indent + '}'; | ||
case 'string': | ||
return fromArray + JSON.stringify(element); | ||
case 'string': | ||
return fromArray + JSON.stringify(element); | ||
case 'function': | ||
return fromArray + functionSignature(element); | ||
case 'function': | ||
return fromArray + functionSignature(element); | ||
case 'undefined': | ||
return fromArray + 'undefined'; | ||
case 'undefined': | ||
return fromArray + 'undefined'; | ||
case 'null': | ||
return fromArray + 'null'; | ||
case 'null': | ||
return fromArray + 'null'; | ||
default: | ||
if (element.toString) { | ||
return fromArray + '"' + element.toString() + '"'; | ||
} | ||
return fromArray + '<<<ERROR>>> Cannot get the string value of the element'; | ||
default: | ||
if (element.toString) { | ||
return fromArray + '"' + element.toString() + '"'; | ||
} | ||
return fromArray + '<<<ERROR>>> Cannot get the string value of the element'; | ||
} | ||
@@ -151,0 +190,0 @@ } |
{ | ||
"name": "js-object-pretty-print", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"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", |
@@ -0,0 +0,0 @@ js-object-pretty-print |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ /*global describe, it*/ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
36439
1.55%477
7.92%