pretty-format
Advanced tools
Comparing version
@@ -1,2 +0,2 @@ | ||
'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.print = exports.test = undefined; | ||
'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.serialize = exports.test = undefined; | ||
@@ -13,8 +13,2 @@ | ||
var _ansiRegex = require('ansi-regex');var _ansiRegex2 = _interopRequireDefault(_ansiRegex); | ||
@@ -51,7 +45,8 @@ var _ansiStyles = require('ansi-styles');var _ansiStyles2 = _interopRequireDefault(_ansiStyles);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };} | ||
* | ||
*/const test = exports.test = value => typeof value === 'string' && value.match((0, _ansiRegex2.default)());const print = exports.print = (val, print, indent, | ||
opts, | ||
colors) => | ||
print(toHumanReadableAnsi(val));exports.default = | ||
*/const test = exports.test = val => typeof val === 'string' && val.match((0, _ansiRegex2.default)());const serialize = exports.serialize = (val, config, indentation, | ||
depth, | ||
refs, | ||
printer) => | ||
printer(toHumanReadableAnsi(val), config, indentation, depth, refs);exports.default = | ||
{ print, test }; | ||
{ serialize, test }; |
{ | ||
"name": "pretty-format", | ||
"version": "20.1.0-echo.1", | ||
"version": "21.0.0-alpha.1", | ||
"repository": { | ||
@@ -5,0 +5,0 @@ "type": "git", |
192
README.md
@@ -5,7 +5,14 @@ # pretty-format | ||
- Supports [all built-in JavaScript types](#type-support) | ||
- [Blazingly fast](https://gist.github.com/thejameskyle/2b04ffe4941aafa8f970de077843a8fd) (similar performance to v8's `JSON.stringify` and significantly faster than Node's `util.format`) | ||
- Plugin system for extending with custom types (i.e. [`ReactTestComponent`](#reacttestcomponent-plugin)) | ||
- Supports all built-in JavaScript types | ||
* primitive types: `Boolean`, `null`, `Number`, `String`, `Symbol`, `undefined` | ||
* other non-collection types: `Date`, `Error`, `Function`, `RegExp` | ||
* collection types: | ||
* `arguments`, `Array`, `ArrayBuffer`, `DataView`, `Float32Array`, `Float64Array`, `Int8Array`, `Int16Array`, `Int32Array`, `Uint8Array`, `Uint8ClampedArray`, `Uint16Array`, `Uint32Array`, | ||
* `Map`, `Set`, `WeakMap`, `WeakSet` | ||
* `Object` | ||
- [Blazingly fast](https://gist.github.com/thejameskyle/2b04ffe4941aafa8f970de077843a8fd) | ||
* similar performance to `JSON.stringify` in v8 | ||
* significantly faster than `util.format` in Node.js | ||
- Serialize application-specific data types with built-in or user-defined plugins | ||
## Installation | ||
@@ -20,75 +27,124 @@ | ||
```js | ||
const prettyFormat = require('pretty-format'); | ||
const prettyFormat = require('pretty-format'); // CommonJS | ||
``` | ||
const 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)); | ||
```js | ||
import prettyFormat from 'pretty-format'; // ES2015 modules | ||
``` | ||
**Result:** | ||
```js | ||
const val = {object: {}}; | ||
val.circularReference = val; | ||
val[Symbol('foo')] = 'foo'; | ||
val.map = new Map([['prop', 'value']]); | ||
val.array = [-0, Infinity, NaN]; | ||
``` | ||
console.log(prettyFormat(val)); | ||
/* | ||
Object { | ||
"property": Object {}, | ||
"array": Array [ | ||
-0, | ||
Infinity, | ||
NaN, | ||
], | ||
"circularReference": [Circular], | ||
"map": Map { | ||
"prop" => "value" | ||
"prop" => "value", | ||
}, | ||
"array": Array [ | ||
1, | ||
NaN, | ||
Infinity | ||
], | ||
Symbol(foo): "foo" | ||
"object": Object {}, | ||
Symbol(foo): "foo", | ||
} | ||
*/ | ||
``` | ||
#### Type Support | ||
## Usage with options | ||
`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` | ||
```js | ||
function onClick() {} | ||
### API | ||
console.log(prettyFormat(onClick)); | ||
/* | ||
[Function onClick] | ||
*/ | ||
const options = { | ||
printFunctionName: false, | ||
}; | ||
console.log(prettyFormat(onClick, options)); | ||
/* | ||
[Function] | ||
*/ | ||
``` | ||
| key | type | default | description | | ||
| :--- | :--- | :--- | :--- | | ||
| `callToJSON` | `boolean` | `true` | call `toJSON` method (if it exists) on objects | | ||
| `escapeRegex` | `boolean` | `false` | escape special characters in regular expressions | | ||
| `highlight` | `boolean` | `false` | highlight syntax with colors in terminal (some plugins) | | ||
| `indent` | `number` | `2` | spaces in each level of indentation | | ||
| `maxDepth` | `number` | `Infinity` | levels to print in arrays, objects, elements, and so on | | ||
| `min` | `boolean` | `false` | minimize added space: no indentation nor line breaks | | ||
| `plugins` | `array` | `[]` | plugins to serialize application-specific data types | | ||
| `printFunctionName` | `boolean` | `true` | include or omit the name of a function | | ||
| `theme` | `object` | | colors to highlight syntax in terminal | | ||
Property values of `theme` are from [ansi-styles colors](https://github.com/chalk/ansi-styles#colors) | ||
```js | ||
console.log(prettyFormat(object)); | ||
console.log(prettyFormat(object, options)); | ||
const DEFAULT_THEME = { | ||
comment: 'gray', | ||
content: 'reset', | ||
prop: 'yellow', | ||
tag: 'cyan', | ||
value: 'green', | ||
}; | ||
``` | ||
Options: | ||
## Usage with plugins | ||
* **`callToJSON`**<br> | ||
Type: `boolean`, default: `true`<br> | ||
Call `toJSON()` on passed object. | ||
* **`indent`**<br> | ||
Type: `number`, default: `2`<br> | ||
Number of spaces for indentation. | ||
* **`maxDepth`**<br> | ||
Type: `number`, default: `Infinity`<br> | ||
Print only this number of levels. | ||
* **`min`**<br> | ||
Type: `boolean`, default: `false`<br> | ||
Print without whitespace. | ||
* **`plugins`**<br> | ||
Type: `array`, default: `[]`<br> | ||
Plugins (see the next section). | ||
* **`printFunctionName`**<br> | ||
Type: `boolean`, default: `true`<br> | ||
Print function names or just `[Function]`. | ||
* **`escapeRegex`**<br> | ||
Type: `boolean`, default: `false`<br> | ||
Escape special characters in regular expressions. | ||
* **`highlight`**<br> | ||
Type: `boolean`, default: `false`<br> | ||
Highlight syntax for terminal (works only with `ReactTestComponent` and `ReactElement` plugins. | ||
* **`theme`**<br> | ||
Type: `object`, default: `{tag: 'cyan', content: 'reset'...}`<br> | ||
Syntax highlight theme.<br> | ||
Uses [ansi-styles colors](https://github.com/chalk/ansi-styles#colors) + `reset` for no color.<br> | ||
Available types: `tag`, `content`, `prop` and `value`. | ||
The `pretty-format` package provides some built-in plugins, including: | ||
* `ReactElement` for elements from `react` | ||
* `ReactTestComponent` for test objects from `react-test-renderer` | ||
```js | ||
// CommonJS | ||
const prettyFormat = require('pretty-format'); | ||
const ReactElement = prettyFormat.plugins.ReactElement; | ||
const ReactTestComponent = prettyFormat.plugins.ReactTestComponent; | ||
const React = require('react'); | ||
const renderer = require('react-test-renderer'); | ||
``` | ||
```js | ||
// ES2015 modules and destructuring assignment | ||
import prettyFormat from 'pretty-format'; | ||
const {ReactElement, ReactTestComponent} = prettyFormat.plugins; | ||
import React from 'react'; | ||
import renderer from 'react-test-renderer'; | ||
``` | ||
```js | ||
const onClick = () => {}; | ||
const element = React.createElement('button', {onClick}, 'Hello World'); | ||
const formatted1 = prettyFormat(element, { | ||
plugins: [ReactElement], | ||
printFunctionName: false, | ||
}); | ||
const formatted2 = prettyFormat(renderer.create(element).toJSON(), { | ||
plugins: [ReactTestComponent], | ||
printFunctionName: false, | ||
}); | ||
/* | ||
<button | ||
onClick=[Function] | ||
> | ||
Hello World | ||
</button> | ||
*/ | ||
``` | ||
### Plugins | ||
@@ -117,21 +173,1 @@ | ||
``` | ||
#### `ReactTestComponent` and `ReactElement` plugins | ||
```js | ||
const prettyFormat = require('pretty-format'); | ||
const reactTestPlugin = require('pretty-format').plugins.ReactTestComponent; | ||
const reactElementPlugin = require('pretty-format').plugins.ReactElement; | ||
const React = require('react'); | ||
const renderer = require('react-test-renderer'); | ||
const element = React.createElement('h1', null, 'Hello World'); | ||
prettyFormat(renderer.create(element).toJSON(), { | ||
plugins: [reactTestPlugin, reactElementPlugin], | ||
}); | ||
// <h1> | ||
// Hello World | ||
// </h1> | ||
``` |
Sorry, the diff of this file is too big to display
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
419829
6.54%25
56.25%5132
3.3%171
26.67%1
-50%