1.1.0 • Published 9 years ago
nicely-format v1.1.0
@ava/pretty-format 
Stringify any JavaScript value.
- Supports all built-in JavaScript types
- Blazingly fast (similar performance to v8's
JSON.stringifyand significantly faster than Node'sutil.format) - Plugin system for extending with custom types (i.e.
ReactTestComponent)
Installation
$ npm install @ava/pretty-formatUsage
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:
callToJSONType:boolean, default:trueCalltoJSON()on passed object.indentType:number, default:2Number of spaces for indentation.maxDepthType:number, default:InfinityPrint only this number of levels.minType:boolean, default:falsePrint without whitespace.pluginsType:array, default:[]Plugins (see the next section).printFunctionNameType:boolean, default:truePrint function names or just[Function].escapeRegexType:boolean, default:falseEscape special characters in regular expressions.highlightType:boolean, default:falseHighlight syntax for terminal (works only withReactTestComponentandReactElementplugins.themeType:object, default:{tag: 'cyan', content: 'reset'...}Syntax highlight theme. Uses ansi-styles colors +resetfor no color. Available types:tag,content,propandvalue.
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>1.1.0
9 years ago