js-object-pretty-print
Advanced tools
Comparing version
@@ -7,3 +7,4 @@ module.exports = function (grunt) { | ||
'src': [ | ||
'*.js', | ||
'index.js', | ||
'Gruntfile.js', | ||
'*.json', | ||
@@ -28,2 +29,13 @@ 'test/*.js' | ||
} | ||
}, | ||
'uglify': { | ||
'options': { | ||
// the banner is inserted at the top of the output | ||
'banner': '/*! js-object-pretty-print.js <%= grunt.template.today("dd-mm-yyyy") %> */\n' | ||
}, | ||
'dist': { | ||
'files': { | ||
'index-min.js': ['index.js'] | ||
} | ||
} | ||
} | ||
@@ -34,7 +46,9 @@ }); | ||
grunt.loadNpmTasks('grunt-mocha-test'); | ||
grunt.loadNpmTasks('grunt-contrib-uglify'); | ||
grunt.registerTask('default', [ | ||
'jslint', | ||
'uglify', | ||
'mochaTest' | ||
]); | ||
}; |
23
index.js
@@ -12,2 +12,4 @@ "use strict"; | ||
prettyObject, | ||
prettyObjectJSON, | ||
prettyObjectPrint, | ||
prettyArray, | ||
@@ -45,3 +47,3 @@ pretty; | ||
prettyObject = function (object, indent) { | ||
prettyObjectJSON = function (object, indent) { | ||
var value = [], | ||
@@ -53,2 +55,16 @@ property; | ||
if (object.hasOwnProperty(property)) { | ||
value.push(indent + '"' + property + '": ' + pretty(object[property], indent)); | ||
} | ||
} | ||
return value.join(newLineJoin) + newLine; | ||
}; | ||
prettyObjectPrint = function (object, indent) { | ||
var value = [], | ||
property; | ||
indent += indentString; | ||
for (property in object) { | ||
if (object.hasOwnProperty(property)) { | ||
value.push(indent + property + ': ' + pretty(object[property], indent)); | ||
@@ -87,3 +103,3 @@ } | ||
case 'date': | ||
return fromArray + element.toString(); | ||
return fromArray + '"' + element.toString() + '"'; | ||
@@ -109,4 +125,5 @@ case 'number': | ||
outputTo = outputTo || 'text'; | ||
outputTo = (outputTo || 'print').toLowerCase(); | ||
indentString = repeatString(outputTo === 'html' ? ' ' : ' ', indentLength); | ||
prettyObject = outputTo === 'json' ? prettyObjectJSON : prettyObjectPrint; | ||
newLine = outputTo === 'html' ? '<br/>' : '\n'; | ||
@@ -113,0 +130,0 @@ newLineJoin = ',' + newLine; |
{ | ||
"name": "js-object-pretty-print", | ||
"version": "0.1.1", | ||
"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. **js-object-pretty-print** is not a JSON serializer", | ||
"version": "0.1.2", | ||
"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.", | ||
"main": "index.js", | ||
@@ -28,2 +28,3 @@ "scripts": { | ||
"grunt": "^0.4.5", | ||
"grunt-contrib-compress": "^0.10.0", | ||
"grunt-jslint": "^1.1.12", | ||
@@ -30,0 +31,0 @@ "grunt-mocha-test": "^0.11.0", |
js-object-pretty-print | ||
====================== | ||
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. **js-object-pretty-print** is not a JSON serializer | ||
Serializes a javascript object to a printable string. String is formatted to be used in a pure text environments, like a console log, as an HTML output, or to create a JSON string. | ||
@@ -27,2 +27,12 @@ ## Installation | ||
It is also possible to use a minified version of the code | ||
``` | ||
... | ||
var prettyMin = require('js0object-pretty-print/index-min.js').pretty; | ||
... | ||
``` | ||
Either the full or the minified versions render the same. Both are unit tested with Mocha and Chai | ||
## Options | ||
@@ -32,15 +42,22 @@ | ||
pretty(*object*, *indentSize*, *outputTo*); | ||
pretty(object, indentSize, outputTo); | ||
### object | ||
Is the javascript object to serialize | ||
### object (mandatory) | ||
Is the javascript object to serialize. If no object is present the function will return a string with an error. | ||
### indentSize | ||
### indentSize (optional) | ||
Number of spaces in a one level indent. Default 4 | ||
### outputTo | ||
Whether to serialize for text or for HTML. Default is 'text' | ||
### outputTo (optional) | ||
String to determine the formatting of the output. One of "PRINT", "HTML" or "JSON". This argument is case insensitive. Default value is "PRINT" | ||
Expected behavior | ||
* **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 | ||
* **HTML** Indentation is done with non breakable spaces &nbsp; line breaks are done with <br/>. Otherwise identical to print. Handy to dump into a div inside a page and get a decent formatting | ||
* **JSON** Only difference with PRINT is that attribute names are surrounded with quotes | ||
## Release History | ||
* 0.1.0 Initial Release | ||
* 0.1.1 Bug fixes | ||
* 0.1.2 Add JSON output, create robust testing with Mocha, add minified version of the code | ||
@@ -6,4 +6,4 @@ /*global describe, it*/ | ||
pretty = require('../index').pretty, | ||
value, | ||
serialized; | ||
prettyMin = require('../index-min').pretty, | ||
value; | ||
@@ -21,6 +21,29 @@ value = { | ||
serialized = pretty(value, 4, 'text'); | ||
describe('Object serialized for print', function () { | ||
var serialized = pretty(value, 4, 'print'); | ||
it('Serialized object', function () { | ||
assert.isNotNull(serialized, 'This should never be null'); | ||
assert.equal(typeof serialized, 'string'); | ||
assert.notEqual(serialized, 'Error: no Javascript object provided'); | ||
}); | ||
describe('Object serialized', function () { | ||
it('Serialized object:\n' + serialized, function () { | ||
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); | ||
}); | ||
it('Dates created', function () { | ||
assert.notEqual(serialized.indexOf('Tue May 25 1954 00:00:00'), -1); | ||
}); | ||
}); | ||
describe('Object serialized for JSON', function () { | ||
var serialized = pretty(value, 4, 'JSON'); | ||
it('Serialized object', function () { | ||
assert.isNotNull(serialized, 'This should never be null'); | ||
@@ -31,16 +54,82 @@ assert.equal(typeof serialized, 'string'); | ||
it('JSON Serialized object', function () { | ||
assert.notEqual(serialized.indexOf(' "name": "Damaso Infanzon Manzo"'), -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); | ||
}); | ||
}); | ||
describe('Object serialized for HTML', function () { | ||
var serialized = pretty(value, 4, 'HTML'); | ||
it('Serialized object', function () { | ||
assert.isNotNull(serialized, 'This should never be null'); | ||
assert.equal(typeof serialized, 'string'); | ||
assert.notEqual(serialized, 'Error: no Javascript object provided'); | ||
}); | ||
it('Indentation for HTML', function () { | ||
assert.notEqual(serialized.indexOf(' name: "Damaso Infanzon Manzo"'), -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); | ||
}); | ||
it('Line breaks for HTML', function () { | ||
assert.notEqual(serialized.indexOf('<br/>'), -1); | ||
}); | ||
}); | ||
describe('Using minified version', function () { | ||
var serialized = prettyMin(value, 4, 'print'); | ||
it('Serialized object', function () { | ||
assert.isNotNull(serialized, 'This should never be null'); | ||
assert.equal(typeof serialized, 'string'); | ||
assert.notEqual(serialized, 'Error: no Javascript object provided'); | ||
}); | ||
it('Testing indentation', function () { | ||
assert.notEqual(serialized.indexOf(' name: \'Damaso Infanzon Manzo\''), 0); | ||
assert.notEqual(serialized.indexOf(' address:'), 0); | ||
assert.notEqual(serialized.indexOf(' street:'), 0); | ||
assert.notEqual(serialized.indexOf(' city:'), 0); | ||
assert.notEqual(serialized.indexOf(' state:'), 0); | ||
assert.notEqual(serialized.indexOf(' zip:'), 0); | ||
assert.notEqual(serialized.indexOf(' true,'), 0); | ||
assert.notEqual(serialized.indexOf(' false'), 0); | ||
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); | ||
}); | ||
it('Dates created', function () { | ||
assert.notEqual(serialized.indexOf('Tue May 25 1954 00:00:00'), 0); | ||
assert.notEqual(serialized.indexOf('Tue May 25 1954 00:00:00'), -1); | ||
}); | ||
}); | ||
describe('Object serialized with default arguments', function () { | ||
var serialized = pretty(value); | ||
it('Serialized object', function () { | ||
assert.isNotNull(serialized, 'This should never be null'); | ||
assert.equal(typeof serialized, 'string'); | ||
assert.notEqual(serialized, 'Error: no Javascript object provided'); | ||
}); | ||
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); | ||
}); | ||
it('Dates created', function () { | ||
assert.notEqual(serialized.indexOf('Tue May 25 1954 00:00:00'), -1); | ||
}); | ||
}); |
27234
41.15%8
14.29%269
67.08%62
37.78%6
20%