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

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.

0.1.2
Source
npm
Version published
Weekly downloads
8.8K
-54.62%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

Javascript

FAQs

Package last updated on 13 Aug 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts