New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

js-object-pretty-print

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js-object-pretty-print - npm Package Compare versions

Comparing version

to
0.1.3

config.json

45

Gruntfile.js
module.exports = function (grunt) {
"use strict";
function getLocalConfig() {
var config,
copy,
fileSystem = require('fs');
copy = {};
fileSystem.readFile('./config.json', 'utf8', function (err, data) {
if (err) {
return;
}
config = JSON.parse(data);
if (config.copyTo) {
config.copyTo.forEach(function (element) {
copy[element.name] = {
'files': [
{
'src': ['index*.js'],
'cwd': '.',
'dest': element.path + '/node_modules/js-object-pretty-print/',
'expand': true,
'filter': 'isFile'
}
]
};
});
}
});
return copy;
}
grunt.initConfig({

@@ -39,3 +72,5 @@ 'jslint': {

}
}
},
'copy': getLocalConfig()
});

@@ -46,2 +81,3 @@

grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-copy');

@@ -53,2 +89,9 @@ grunt.registerTask('default', [

]);
grunt.registerTask('local', [
'jslint',
'uglify',
'mochaTest',
'copy'
]);
};

4

index-min.js

@@ -1,2 +0,2 @@

/*! js-object-pretty-print.js 13-08-2014 */
"use strict";module.exports.pretty=function(a,b,c){var d,e,f,g,h,i,j,k,l,m,n,o;return g=Object.prototype.toString,h={undefined:"undefined",number:"number","boolean":"boolean",string:"string","[object Function]":"function","[object RegExp]":"regexp","[object Array]":"array","[object Date]":"date","[object Error]":"error"},i=function(a){var b=h[typeof a]||h[g.call(a)]||(a?"object":"null");return b},j=function(a,b){var c,d="";for(c=0;b>c;c+=1)d+=a;return d},l=function(a,b){var c,g=[];b+=d;for(c in a)a.hasOwnProperty(c)&&g.push(b+'"'+c+'": '+o(a[c],b));return g.join(f)+e},m=function(a,b){var c,g=[];b+=d;for(c in a)a.hasOwnProperty(c)&&g.push(b+c+": "+o(a[c],b));return g.join(f)+e},n=function(a,b){var c,g=a.length,h=[];for(b+=d,c=0;g>c;c+=1)h.push(o(a[c],b,b));return h.join(f)+e},o=function(a,b,c){var d=i(a);switch(c=c||"",d){case"array":return c+"["+e+n(a,b)+b+"]";case"boolean":return c+(a?"true":"false");case"date":return c+'"'+a.toString()+'"';case"number":return c+a;case"object":return c+"{"+e+k(a,b)+b+"}";case"string":return c+'"'+a+'"';default:return c+d}},a?(void 0===b&&(b=4),c=(c||"print").toLowerCase(),d=j("html"===c?"&nbsp;":" ",b),k="json"===c?l:m,e="html"===c?"<br/>":"\n",f=","+e,o(a,"")+e):"Error: no Javascript object provided"};
/*! 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?"&nbsp;":" ",b),l="json"===c?m:n,f="html"===c?"<br/>":"\n",g=","+f,r=[],q(a,"")+f):"Error: no Javascript object provided"};
"use strict";
module.exports.pretty = function (jsObject, indentLength, outputTo) {
module.exports.pretty = function (jsObject, indentLength, outputTo, fullFunction) {
var indentString,

@@ -15,3 +15,5 @@ newLine,

prettyArray,
pretty;
functionSignature,
pretty,
visited;

@@ -88,29 +90,48 @@ TOSTRING = Object.prototype.toString;

functionSignature = function (element) {
var signatureExpression,
signature;
element = element.toString();
signatureExpression = new RegExp('function\\s*.*\\s*\\(.*\\)');
signature = signatureExpression.exec(element);
signature = signature ? signature[0] : '[object Function]';
return fullFunction ? element : '"' + signature + '"';
};
pretty = function (element, indent, fromArray) {
var type = valueType(element);
var type;
type = valueType(element);
fromArray = fromArray || '';
if (visited.indexOf(element) === -1) {
switch (type) {
case 'array':
visited.push(element);
return fromArray + '[' + newLine + prettyArray(element, indent) + indent + ']';
switch (type) {
case 'array':
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':
return fromArray + '{' + newLine + prettyObject(element, indent) + indent + '}';
case 'string':
return fromArray + '"' + element + '"';
case 'string':
return fromArray + '"' + element + '"';
case 'function':
return fromArray + functionSignature(element);
default:
return fromArray + type;
default:
return fromArray + element.toString();
}
}
return fromArray + 'circular reference to ' + element.toString();
};

@@ -128,2 +149,3 @@

newLineJoin = ',' + newLine;
visited = [];
return pretty(jsObject, '') + newLine;

@@ -130,0 +152,0 @@ }

{
"name": "js-object-pretty-print",
"version": "0.1.2",
"version": "0.1.3",
"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.",

@@ -31,4 +31,6 @@ "main": "index.js",

"grunt-mocha-test": "^0.11.0",
"grunt-contrib-uglify": "~0.2.7",
"grunt-contrib-copy": "^0.5.0",
"mocha": "^1.21.4"
}
}

@@ -14,13 +14,45 @@ js-object-pretty-print

var pretty = require('js-object-pretty-print').pretty,
foo = {
"aString": "ipsum lorem",
"aNumber": 3
"anArray": ["shoes", "socks", "shirts"],
"anObject": {
"aValue": 44,
"isValid": true
}
};
address,
value;
process.stdout.write(pretty(foo));
function onAnother(foo, bar) {
var lola;
lola = foo + bar;
return lola;
}
address = {
'street': 'Callejon de las ranas 128',
'city': 'Falfurrias',
'state':
'Texas',
'zip': '88888-9999'
};
value = {
'name': 'Damaso Infanzon Manzo',
'address': address,
'favorites': {
'music': ['Mozart', 'Beethoven', 'The Beatles'],
'authors': ['John Grisham', 'Isaac Asimov', 'P.L. Travers'],
'books': [ 'Pelican Brief', 'I, Robot', 'Mary Poppins' ]
},
'dates': [ new Date(), new Date("05/25/1954") ],
'numbers': [ 10, 883, 521 ],
'boolean': [ true, false, false, false ],
'isObject': true,
'isDuck': false,
'onWhatever': function (foo, bar) {
var lola;
lola = foo + bar;
return lola;
},
'onAnother': onAnother
};
address.value = value;
console.log(pretty(value));
```

@@ -40,5 +72,5 @@

Function pretty accepts three arguments:
Function pretty accepts four arguments:
pretty(object, indentSize, outputTo);
pretty(object, indentSize, outputTo, fullFunction);

@@ -54,2 +86,33 @@ ### object (mandatory)

### fullFunction
A boolean to determine to expand all the text of a function or to display only the signature. The default value is to display only the signature, that is the word **function** followed by the function name, if any, followed by the arguments of the function in parenthesis.
* **fullFunction == false** Passing the object above will result in
```
{
...
onWhatever: "function (foo, bar)",
onAnother: "function onAnother(foo, bar)"
}
```
* **fullFunction == true** Passing the object above will result in
```
{
...
onWhatever: function (foo, bar) {
var lola;
lola = foo + bar;
return lola;
},
onAnother: function onAnother(foo, bar) {
var lola;
lola = foo + bar;
return lola;
}
}
```
Notice that the program does not attempt to pretty print the function. It is rendered as written.
Expected behavior

@@ -60,2 +123,5 @@ * **PRINT** Indentation is done with the space character, line breaks are done with the newLine character "\n" and the attribute names are not surrounded with quotes. Pretty similar to what you see in the -webkit debugger

## Circular references
Sometimes some objects have circular references. This will can cause a heap overflow if not dealt properly. The code now detects circular references and stop inspecting the object
## Release History

@@ -65,2 +131,3 @@ * 0.1.0 Initial Release

* 0.1.2 Add JSON output, create robust testing with Mocha, add minified version of the code
* 0.1.3 Circular reference detection, option to print only the signature of functions

@@ -7,7 +7,17 @@ /*global describe, it*/

prettyMin = require('../index-min').pretty,
address,
value;
function onAnother(foo, bar) {
var lola;
lola = foo + bar;
return lola;
}
address = { 'street': 'Callejon de las ranas 128', 'city': 'Falfurrias', 'state': 'Texas', 'zip': '88888-9999' };
value = {
'name': 'Damaso Infanzon Manzo',
'address': { 'street': 'Callejon de las ranas 128', 'city': 'Falfurrias', 'state': 'Texas', 'zip': '88888-9999' },
'address': address,
'favorites': { 'music': ['Mozart', 'Beethoven', 'The Beatles'], 'authors': ['John Grisham', 'Isaac Asimov', 'P.L. Travers'], 'books': [ 'Pelican Brief', 'I, Robot', 'Mary Poppins' ] },

@@ -18,7 +28,18 @@ 'dates': [ new Date(), new Date("05/25/1954") ],

'isObject': true,
'isDuck': false
'isDuck': false,
'onWhatever': function (foo, bar) {
var lola;
lola = foo + bar;
return lola;
},
'onAnother': onAnother
};
address.value = value;
describe('Object serialized for print', function () {
var serialized = pretty(value, 4, 'print');
var serialized, index;
serialized = pretty(value, 4, 'print');
console.log(serialized);
it('Serialized object', function () {

@@ -30,11 +51,30 @@ assert.isNotNull(serialized, 'This should never be null');

index = 0;
it('Testing indentation', function () {
assert.notEqual(serialized.indexOf(' name: "Damaso Infanzon Manzo"', 0), -1);
assert.notEqual(serialized.indexOf(' address:'), -1);
assert.notEqual(serialized.indexOf(' street:'), -1);
assert.notEqual(serialized.indexOf(' city:'), -1);
assert.notEqual(serialized.indexOf(' state:'), -1);
assert.notEqual(serialized.indexOf(' zip:'), -1);
assert.notEqual(serialized.indexOf(' true,'), -1);
assert.notEqual(serialized.indexOf(' false'), -1);
index = serialized.indexOf(' name: "Damaso Infanzon Manzo"', index);
assert.notEqual(index, -1);
index = serialized.indexOf(' address:', index);
assert.notEqual(index, -1);
index = serialized.indexOf(' street:', index);
assert.notEqual(index, -1);
index = serialized.indexOf(' city:', index);
assert.notEqual(index, -1);
index = serialized.indexOf(' state:', index);
assert.notEqual(index, -1);
index = serialized.indexOf(' zip:', index);
assert.notEqual(index, -1);
index = serialized.indexOf(' true,', index);
assert.notEqual(index, -1);
index = serialized.indexOf(' false,', index);
assert.notEqual(index, -1);
index = serialized.indexOf(' false', index);
assert.notEqual(index, -1);
});

@@ -45,4 +85,39 @@

});
it('Functions found', function () {
assert.notEqual(serialized.indexOf('onWhatever: "function (foo, bar)"'), -1);
assert.notEqual(serialized.indexOf('onAnother: "function onAnother(foo, bar)"'), -1);
});
});
describe('Object serialized with full function expansion', function () {
var serialized,
functionLiteral,
row,
index;
functionLiteral = [
"onWhatever: function (foo, bar) {",
"var lola;",
"lola = foo + bar;",
"return lola;",
"},",
"onAnother: function onAnother(foo, bar) {",
"var lola;",
"lola = foo + bar;",
"return lola;",
"}"
];
serialized = pretty(value, 4, 'print', true);
console.log(serialized);
it('Full function expand', function () {
index = 0;
for (row = 0; row < functionLiteral.length; row += 1) {
index = serialized.indexOf(functionLiteral[row], index);
assert.notEqual(index, -1);
}
});
});
describe('Object serialized for JSON', function () {

@@ -49,0 +124,0 @@ var serialized = pretty(value, 4, 'JSON');