Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/stefanpenner/js-object-pretty-print

Dumps a javascript object as a JSON string, formatted for either HTML or a text file. It provides proper line breaks and left indentation
https://github.com/stefanpenner/js-object-pretty-print

Last synced: 17 days ago
JSON representation

Dumps a javascript object as a JSON string, formatted for either HTML or a text file. It provides proper line breaks and left indentation

Awesome Lists containing this project

README

        

js-object-pretty-print
======================

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.

## Installation

npm install js-object-pretty-print

## Usage

```
var pretty = require('js-object-pretty-print').pretty,
foo = {
"aString": "ipsum lorem",
"aNumber": 3
"anArray": ["shoes", "socks", "shirts"],
"anObject": {
"aValue": 44,
"isValid": true
}
};

process.stdout.write(pretty(foo));
```

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

Function pretty accepts three arguments:

pretty(object, indentSize, outputTo);

### object (mandatory)
Is the javascript object to serialize. If no object is present the function will return a string with an error.

### indentSize (optional)
Number of spaces in a one level indent. Default 4

### 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