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

pretty-var-export

v2.0.2

Published

Export any value to valid and equivalent JavaScript code

Downloads

3,595

Readme

pretty-var-export

Build Status Code Coverage MIT License

Export any value to valid and equivalent JavaScript code. Uses syntax coloring for debugging from the cli.

npm install pretty-var-export

Usage

import pretty from 'pretty-var-export';

// return string suitable for logging
console.log(pretty(myValue));

// OR prettify and log
pretty.log(myValue1, myValue2);

Note that coloring can be disabled with pretty.colors.disable().

Example output

Example

Customization

Add handler

Add custom handlers for formatting.

For example, to show NaN as a different color than numbers:

import pretty from 'pretty-var-export';

// each handler must have a test and format method
pretty.handlers.add('nan', {
	test: isNaN,
	format: () => pretty.colors.palette.cyan('NaN'),
});

// if you want to reset to default list
pretty.handlers.reset();

Remove handler

You can remove handlers by name.

import pretty from 'pretty-var-export';

// see below for list of names
pretty.handlers.remove('nan');

// or you can reset all handlers
pretty.handlers.reset();

Default handler names:

  • arguments
  • Array
  • BigInt
  • Boolean
  • Date
  • Error
  • Entries
  • Function
  • Map
  • null
  • Number
  • Object
  • Promise
  • RegExp
  • Set
  • String
  • Symbol
  • TypedArray
  • undefined
  • URL
  • URLSearchParams
  • WeakMap
  • WeakSet

Custom indent

By default, indentation is 2 spaces.

import pretty from 'pretty-var-export';

pretty.options.indent = 4; // 4 spaces
pretty.options.indent = '    '; // 4 spaces
pretty.options.indent = '\t'; // tab

Custom colors

Colors come from the ansi-colors npm module.

Formats include the following:

  • boolean for true and false (default yellow)
  • comment for comments (default gray)
  • constructor for instantiation (default blue)
  • escape for string escapes (default yellowBright)
  • null for null (default yellow)
  • number for numbers, NaN, Infinity (default red)
  • property for object property names (default cyan)
  • regexp for bodies of regular expressions (default blue)
  • string for strings (default green)
  • symbol for quotes, braces, parens, commas, colons (default white)
  • undefined for undefined (default yellow)
import pretty from 'pretty-var-export';

// existing color functions from the ansi-colors npm package
pretty.colors.symbol = pretty.colors.palette.blueBright;

// use any other function
pretty.colors.string = chalk.green;

// disable all coloring
pretty.colors.disable();

// reset to default colors and re-enable coloring
pretty.colors.reset();

Custom display options

There are a few options that can change display. Below are defaults.

import pretty from 'pretty-var-export';

// if true, show function bodies
pretty.options.showFunctionBody = false;

// the max length for strings
pretty.options.maxStringLength = 1024 * 4;

// max number of items to list
pretty.options.maxListItems = 100;

// output multi-line strings with backticks
pretty.options.preferBackticks = true;

// quote style for default strings (single, double or backtick)
pretty.options.quoteStyle = 'double';

// reset to defaults
pretty.options.reset();

Custom labels

Labels can be changed or translated.

import pretty from 'pretty-var-export';

// defaults
pretty.labels.circularReference = 'Circular Reference';
pretty.labels.codeOmitted = 'Code Omitted';
pretty.labels.itemsUnknown = 'Items Unknown';

// reset to defaults
pretty.labels.reset();

Changelog

See CHANGELOG.md

Contributing

Please open a ticket or PR on GitHub.

License

MIT License