pretty-var-export
Export any value to valid and equivalent JavaScript code. Uses syntax coloring for debugging from the cli.
npm install pretty-var-export
Usage
const pretty = ; // return string suitable for loggingconsole; // OR prettify and logpretty;
Example output
Customization
Add handler
Add custom handlers for formatting.
For example, show NaN as a different color than numbers:
const pretty = ; // each handler must have a test and format methodprettyhandlers; // reset to default listprettyhandlers;
Remove handler
You can remove handlers by name.
const pretty = ; // see below for list of namesprettyhandlers; // reset to default listprettyhandlers;
Default handler names:
- arguments
- Array
- BigInt
- Boolean
- Date
- Error
- Function
- Map
- null
- Number
- Object
- Promise
- RegExp
- Set
- String
- Symbol
- TypedArray
- undefined
- URL
- URLSearchParams
- WeakMap
- WeakSet
Custom indent
By default, indentation is 2 spaces.
const pretty = ; prettyoptionsindent = 4; // 4 spacesprettyoptionsindent = ' '; // 4 spacesprettyoptionsindent = '\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)
const pretty = ; // existing color functions from the ansi-colors npm packageprettycolorssymbol = prettycolorspaletteblueBright; // use any other functionprettycolorsstring = chalkgreen; // disable all coloringprettycolors; // reset to default colors and re-enable coloringprettycolors;
Custom display options
There are a few options that can change display. Below are defaults.
const pretty = ; // if true, show function bodiesprettyoptionsshowFunctionBody = false; // the max length for stringsprettyoptionsmaxStringLength = 1024 * 4; // max number of items to listprettyoptionsmaxListItems = 100; // output multi-line strings with backticksprettyoptionspreferBackticks = true; // quote style for default strings (single, double or backtick)prettyoptionsquoteStyle = 'double'; // reset to defaultsprettyoptions;
Custom labels
Labels can be changed or translated.
const pretty = ; // defaultsprettylabelscircularReference = 'Circular Reference';prettylabelscodeOmitted = 'Code Omitted';prettylabelsitemsUnknown = 'Items Unknown'; // reset to defaultsprettylabels;
Changelog
See CHANGELOG.md
Contributing
Please open a ticket or PR on GitHub.