npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

js-object-pretty-print

v0.3.0

Published

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.

Downloads

527,007

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,
    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': 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));

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 four arguments:

pretty(object, indentSize, outputTo, fullFunction);

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"

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

  • 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 JSON. 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

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

null and undefined

Objects and properties with value null or undefined are now properly reported.

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
  • 0.1.3 Circular reference detection, option to print only the signature of functions
  • 0.1.4 Better processing of undefined or null values
  • 0.2.0 Fixed several bugs, including proper treatment of null and undefined values. The output for HTML now has the property names enclosed in quotes.