GithubHelp home page GithubHelp logo

isabella232 / pretty-format Goto Github PK

View Code? Open in Web Editor NEW

This project forked from avajs/pretty-format

0.0 0.0 0.0 243 KB

:sparkles: Stringify any JavaScript value

License: ISC License

JavaScript 100.00%

pretty-format's Introduction

DEPRECATED

AVA no longer uses this fork. The repository is retained for posterity.


@ava/pretty-format Travis build status

Stringify any JavaScript value.

Installation

$ npm install @ava/pretty-format

Usage

var prettyFormat = require('pretty-format');

var obj = { property: {} };
obj.circularReference = obj;
obj[Symbol('foo')] = 'foo';
obj.map = new Map();
obj.map.set('prop', 'value');
obj.array = [1, NaN, Infinity];

console.log(prettyFormat(obj));

Result:

Object {
  "property": Object {},
  "circularReference": [Circular],
  "map": Map {
    "prop" => "value"
  },
  "array": Array [
    1,
    NaN,
    Infinity
  ],
  Symbol(foo): "foo"
}

Type Support

Object, Array, ArrayBuffer, DataView, Float32Array, Float64Array, Int8Array, Int16Array, Int32Array, Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array, arguments, Boolean, Date, Error, Function, Infinity, Map, NaN, null, Number, RegExp, Set, String, Symbol, undefined, WeakMap, WeakSet

API

console.log(prettyFormat(object));
console.log(prettyFormat(object, options));

Options:

  • callToJSON
    Type: boolean, default: true
    Call toJSON() on passed object.
  • indent
    Type: number, default: 2
    Number of spaces for indentation.
  • maxDepth
    Type: number, default: Infinity
    Print only this number of levels.
  • min
    Type: boolean, default: false
    Print without whitespace.
  • plugins
    Type: array, default: []
    Plugins (see the next section).
  • printFunctionName
    Type: boolean, default: true
    Print function names or just [Function].
  • escapeRegex
    Type: boolean, default: false
    Escape special characters in regular expressions.
  • highlight
    Type: boolean, default: false
    Highlight syntax for terminal (works only with ReactTestComponent and ReactElement plugins.
  • theme
    Type: object, default: {tag: 'cyan', content: 'reset'...}
    Syntax highlight theme.
    Uses ansi-styles colors + reset for no color.
    Available types: tag, content, prop and value.

Plugins

Pretty format also supports adding plugins:

var fooPlugin = {
  test: function(val) {
    return val && val.hasOwnProperty('foo');
  },
  print: function(val, print, indent) {
    return 'Foo: ' + print(val.foo);
  }
};

var obj = { foo: { bar: {} } };

prettyFormat(obj, {
  plugins: [fooPlugin]
});
// Foo: Object {
//   "bar": Object {}
// }

ReactTestComponent and ReactElement plugins

var prettyFormat = require('pretty-format');
var reactTestPlugin = require('pretty-format/plugins/ReactTestComponent');
var reactElementPlugin = require('pretty-format/plugins/ReactElement');

var React = require('react');
var renderer = require('react-test-renderer');

var jsx = React.createElement('h1', null, 'Hello World');

prettyFormat(renderer.create(jsx).toJSON(), {
  plugins: [reactTestPlugin, reactElementPlugin]
});
// <h1>
//   Hello World
// </h1>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.