pretty-format
Advanced tools
Comparing version 21.3.0-beta.8 to 21.3.0-beta.9
{ | ||
"name": "pretty-format", | ||
"version": "21.3.0-beta.8", | ||
"version": "21.3.0-beta.9", | ||
"repository": { | ||
@@ -5,0 +5,0 @@ "type": "git", |
181
README.md
@@ -5,13 +5,17 @@ # pretty-format | ||
- 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 | ||
* 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 | ||
@@ -78,15 +82,16 @@ ## Installation | ||
| 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 | | ||
| 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) | ||
Property values of `theme` are from | ||
[ansi-styles colors](https://github.com/chalk/ansi-styles#colors) | ||
@@ -152,7 +157,11 @@ ```js | ||
For snapshot tests, Jest uses `pretty-format` with options that include some of its built-in plugins. For this purpose, plugins are also known as **snapshot serializers**. | ||
For snapshot tests, Jest uses `pretty-format` with options that include some of | ||
its built-in plugins. For this purpose, plugins are also known as **snapshot | ||
serializers**. | ||
To serialize application-specific data types, you can add modules to `devDependencies` of a project, and then: | ||
To serialize application-specific data types, you can add modules to | ||
`devDependencies` of a project, and then: | ||
In an **individual** test file, you can add a module as follows. It precedes any modules from Jest configuration. | ||
In an **individual** test file, you can add a module as follows. It precedes any | ||
modules from Jest configuration. | ||
@@ -166,3 +175,5 @@ ```js | ||
For **all** test files, you can specify modules in Jest configuration. They precede built-in plugins for React, HTML, and Immutable.js data types. For example, in a `package.json` file: | ||
For **all** test files, you can specify modules in Jest configuration. They | ||
precede built-in plugins for React, HTML, and Immutable.js data types. For | ||
example, in a `package.json` file: | ||
@@ -181,10 +192,16 @@ ```json | ||
If `options` has a `plugins` array: for the first plugin whose `test(val)` method returns a truthy value, then `prettyFormat(val, options)` returns the result from either: | ||
If `options` has a `plugins` array: for the first plugin whose `test(val)` | ||
method returns a truthy value, then `prettyFormat(val, options)` returns the | ||
result from either: | ||
* `serialize(val, …)` method of the **improved** interface (available in **version 21** or later) | ||
* `print(val, …)` method of the **original** interface (if plugin does not have `serialize` method) | ||
* `serialize(val, …)` method of the **improved** interface (available in | ||
**version 21** or later) | ||
* `print(val, …)` method of the **original** interface (if plugin does not have | ||
`serialize` method) | ||
### test | ||
Write `test` so it can receive `val` argument of any type. To serialize **objects** which have certain properties, then a guarded expression like `val != null && …` or more concise `val && …` prevents the following errors: | ||
Write `test` so it can receive `val` argument of any type. To serialize | ||
**objects** which have certain properties, then a guarded expression like `val | ||
!= null && …` or more concise `val && …` prevents the following errors: | ||
@@ -218,19 +235,23 @@ * `TypeError: Cannot read property 'whatever' of null` | ||
| key | type | description | | ||
| :--- | :--- | :--- | | ||
| `callToJSON` | `boolean` | call `toJSON` method (if it exists) on objects | | ||
| `colors` | `Object` | escape codes for colors to highlight syntax | | ||
| `escapeRegex` | `boolean` | escape special characters in regular expressions | | ||
| `indent` | `string` | spaces in each level of indentation | | ||
| `maxDepth` | `number` | levels to print in arrays, objects, elements, and so on | | ||
| `min` | `boolean` | minimize added space: no indentation nor line breaks | | ||
| `plugins` | `array` | plugins to serialize application-specific data types | | ||
| `printFunctionName` | `boolean` | include or omit the name of a function | | ||
| `spacingInner` | `strong` | spacing to separate items in a list | | ||
| `spacingOuter` | `strong` | spacing to enclose a list of items | | ||
| key | type | description | | ||
| :------------------ | :-------- | :------------------------------------------------------ | | ||
| `callToJSON` | `boolean` | call `toJSON` method (if it exists) on objects | | ||
| `colors` | `Object` | escape codes for colors to highlight syntax | | ||
| `escapeRegex` | `boolean` | escape special characters in regular expressions | | ||
| `indent` | `string` | spaces in each level of indentation | | ||
| `maxDepth` | `number` | levels to print in arrays, objects, elements, and so on | | ||
| `min` | `boolean` | minimize added space: no indentation nor line breaks | | ||
| `plugins` | `array` | plugins to serialize application-specific data types | | ||
| `printFunctionName` | `boolean` | include or omit the name of a function | | ||
| `spacingInner` | `strong` | spacing to separate items in a list | | ||
| `spacingOuter` | `strong` | spacing to enclose a list of items | | ||
Each property of `colors` in `config` corresponds to a property of `theme` in `options`: | ||
Each property of `colors` in `config` corresponds to a property of `theme` in | ||
`options`: | ||
* the key is the same (for example, `tag`) | ||
* the value in `colors` is a object with `open` and `close` properties whose values are escape codes from [ansi-styles](https://github.com/chalk/ansi-styles) for the color value in `theme` (for example, `'cyan'`) | ||
* the value in `colors` is a object with `open` and `close` properties whose | ||
values are escape codes from | ||
[ansi-styles](https://github.com/chalk/ansi-styles) for the color value in | ||
`theme` (for example, `'cyan'`) | ||
@@ -240,7 +261,9 @@ Some properties in `config` are derived from `min` in `options`: | ||
* `spacingInner` and `spacingOuter` are **newline** if `min` is `false` | ||
* `spacingInner` is **space** and `spacingOuter` is **empty string** if `min` is `true` | ||
* `spacingInner` is **space** and `spacingOuter` is **empty string** if `min` is | ||
`true` | ||
### Example of serialize and test | ||
This plugin is a pattern you can apply to serialize composite data types. Of course, `pretty-format` does not need a plugin to serialize arrays :) | ||
This plugin is a pattern you can apply to serialize composite data types. Of | ||
course, `pretty-format` does not need a plugin to serialize arrays :) | ||
@@ -280,5 +303,5 @@ ```js | ||
: (config.min ? '' : name + ' ') + | ||
'[' + | ||
serializeItems(array, config, indentation, depth, refs, printer) + | ||
']'; | ||
'[' + | ||
serializeItems(array, config, indentation, depth, refs, printer) + | ||
']'; | ||
}, | ||
@@ -305,5 +328,7 @@ }; | ||
```js | ||
console.log(prettyFormat(val, { | ||
plugins: [plugin], | ||
})); | ||
console.log( | ||
prettyFormat(val, { | ||
plugins: [plugin], | ||
}), | ||
); | ||
/* | ||
@@ -327,6 +352,8 @@ Object { | ||
```js | ||
console.log(prettyFormat(val, { | ||
indent: 4, | ||
plugins: [plugin], | ||
})); | ||
console.log( | ||
prettyFormat(val, { | ||
indent: 4, | ||
plugins: [plugin], | ||
}), | ||
); | ||
/* | ||
@@ -350,6 +377,8 @@ Object { | ||
```js | ||
console.log(prettyFormat(val, { | ||
maxDepth: 1, | ||
plugins: [plugin], | ||
})); | ||
console.log( | ||
prettyFormat(val, { | ||
maxDepth: 1, | ||
plugins: [plugin], | ||
}), | ||
); | ||
/* | ||
@@ -364,6 +393,8 @@ Object { | ||
```js | ||
console.log(prettyFormat(val, { | ||
min: true, | ||
plugins: [plugin], | ||
})); | ||
console.log( | ||
prettyFormat(val, { | ||
min: true, | ||
plugins: [plugin], | ||
}), | ||
); | ||
/* | ||
@@ -381,4 +412,5 @@ {"filter": "completed", "items": [{"completed": true, "text": "Write test"}, {"completed": true, "text": "Write serialize"}]} | ||
* if values either | ||
* do **not** require indentation, or | ||
* do **not** occur as children of JavaScript data structures (for example, array) | ||
* do **not** require indentation, or | ||
* do **not** occur as children of JavaScript data structures (for example, | ||
array) | ||
@@ -396,3 +428,4 @@ Write `print` to return a string, given the arguments: | ||
* `spacing` and `edgeSpacing` are **newline** if `min` is `false` | ||
* `spacing` is **space** and `edgeSpacing` is **empty string** if `min` is `true` | ||
* `spacing` is **space** and `edgeSpacing` is **empty string** if `min` is | ||
`true` | ||
@@ -402,7 +435,11 @@ Each property of `colors` corresponds to a property of `theme` in `options`: | ||
* the key is the same (for example, `tag`) | ||
* the value in `colors` is a object with `open` and `close` properties whose values are escape codes from [ansi-styles](https://github.com/chalk/ansi-styles) for the color value in `theme` (for example, `'cyan'`) | ||
* the value in `colors` is a object with `open` and `close` properties whose | ||
values are escape codes from | ||
[ansi-styles](https://github.com/chalk/ansi-styles) for the color value in | ||
`theme` (for example, `'cyan'`) | ||
### Example of print and test | ||
This plugin prints functions with the **number of named arguments** excluding rest argument. | ||
This plugin prints functions with the **number of named arguments** excluding | ||
rest argument. | ||
@@ -445,3 +482,5 @@ ```js | ||
This plugin **ignores** the `printFunctionName` option. That limitation of the original `print` interface is a reason to use the improved `serialize` interface, described above. | ||
This plugin **ignores** the `printFunctionName` option. That limitation of the | ||
original `print` interface is a reason to use the improved `serialize` | ||
interface, described above. | ||
@@ -448,0 +487,0 @@ ```js |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
426489
493
15
5641