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

print

v1.2.0

Published

Generate a human-readable representation of a value.

Downloads

622

Readme

Print

[![Build status: TravisCI][TravisCI-badge]][TravisCI-link] [![Build status: AppVeyor][AppVeyor-badge]][AppVeyor-link] [![Coverage status][Coverage-badge]][Coverage-link] [![Latest release][NPM-badge]][NPM-link]

Generate a human-readable representation of a value. Focussed on producing clean and accurate depictions of data. Suitable for debugging and generating diffs.

Usage

const print = require("print");
let output = print({
    foo: "bar",
    baz: "quux",
    list: [1, 2, 3, 4, "five"]
});
console.log(output);

print.out(obj); // Shortcut for console.log(print(obj));

Comparison with built-in functions

print                JSON.stringify          util.inspect

{                    {                       { foo: 'bar',
    baz: "quux"          "foo": "bar",         baz: 'quux',
    foo: "bar"           "baz": "quux",        list:
    list: [              "list": [             [ 1,
        1                    1,                  2,
        2                    2,                  3,
        3                    3,                  4,
        4                    4,                  'five' ] }
        "five"               "five"
    ]                    ]
}                    }

print also handles circular references by showing a -> pointing to where the object was first mentioned. For example, the following code:

const A = {};
const B = {foo: A};
print({A, B});

Will produce:

{
    A: {}
    B: {
        foo: -> A
    }
}

Options

An optional second parameter can be passed to refine print's output.

Available options and their default values are listed below:

print(input, {
    ampedSymbols:     true,
    escapeChars:      /(?!\x20)\s|\\/g,
    invokeGetters:    false,
    maxArrayLength:   100,
    showAll:          false,
    showArrayIndices: false,
    showArrayLength:  false,
    sortProps:        true
});

ampedSymbols

[Boolean]
Prefix [Symbol]-keyed properties with @@. Disable to show Symbol(…) instead.

escapeChars

[RegExp] | [Function]
What characters, if any, are escaped in string values.

By default, anything that alters the output's meaning or layout is escaped:

\f \n \r \t \v \\

This can be overridden with a custom expression or callback, the latter of which receives the entire string as an argument.

Passing falsey values to escapeChars disables escaping altogether, which isn't recommended if your input contains line-breaks or tabulation.

invokeGetters

[Boolean]
Permit print to call a property getter to display its computed value.

Invoking a getter can have unwanted side-effects, so this option is disabled by default.

maxArrayLength

[Number]
Maximum number of array values to show before truncating them:

[
    1
    2
    3

    … 7 more values not shown
]

Note this excludes any named properties stored on the Array object:

const input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
input.foo = "bar";
print(input, { maxArrayLength: 3 })
[
    1
    2
    3
	
    … 7 more values not shown

    foo: "bar"
]

showAll

[Boolean]
Include non-enumerable properties when printing an object.

Note that inherited properties are always hidden.

showArrayIndices

[Boolean]
Show the index of each element in an array.

[                  [
    "A"                0: "A"
    "B"      ->        1: "B"
    "C"                2: "C"
]                  ]

showArrayLength

[Boolean]
Display an array's length property after its values:

[
    "A"
    "B"
    "C"
    length: 3
]

sortProps

[Boolean]
Alphabetise the properties of printed objects.

To display properties in the order they were assigned, set this to false.

Note that alphabetisation is case-insensitive.