版博士V2.0程序
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

README.md 13 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. # pretty-format
  2. Stringify any JavaScript value.
  3. - Serialize built-in JavaScript types.
  4. - Serialize application-specific data types with built-in or user-defined plugins.
  5. ## Installation
  6. ```sh
  7. $ yarn add pretty-format
  8. ```
  9. ## Usage
  10. ```js
  11. const {format: prettyFormat} = require('pretty-format'); // CommonJS
  12. ```
  13. ```js
  14. import {format as prettyFormat} from 'pretty-format'; // ES2015 modules
  15. ```
  16. ```js
  17. const val = {object: {}};
  18. val.circularReference = val;
  19. val[Symbol('foo')] = 'foo';
  20. val.map = new Map([['prop', 'value']]);
  21. val.array = [-0, Infinity, NaN];
  22. console.log(prettyFormat(val));
  23. /*
  24. Object {
  25. "array": Array [
  26. -0,
  27. Infinity,
  28. NaN,
  29. ],
  30. "circularReference": [Circular],
  31. "map": Map {
  32. "prop" => "value",
  33. },
  34. "object": Object {},
  35. Symbol(foo): "foo",
  36. }
  37. */
  38. ```
  39. ## Usage with options
  40. ```js
  41. function onClick() {}
  42. console.log(prettyFormat(onClick));
  43. /*
  44. [Function onClick]
  45. */
  46. const options = {
  47. printFunctionName: false,
  48. };
  49. console.log(prettyFormat(onClick, options));
  50. /*
  51. [Function]
  52. */
  53. ```
  54. <!-- prettier-ignore -->
  55. | key | type | default | description |
  56. | :-------------------- | :-------- | :--------- | :------------------------------------------------------ |
  57. | `callToJSON` | `boolean` | `true` | call `toJSON` method (if it exists) on objects |
  58. | `compareKeys` | `function`| `undefined`| compare function used when sorting object keys |
  59. | `escapeRegex` | `boolean` | `false` | escape special characters in regular expressions |
  60. | `escapeString` | `boolean` | `true` | escape special characters in strings |
  61. | `highlight` | `boolean` | `false` | highlight syntax with colors in terminal (some plugins) |
  62. | `indent` | `number` | `2` | spaces in each level of indentation |
  63. | `maxDepth` | `number` | `Infinity` | levels to print in arrays, objects, elements, and so on |
  64. | `min` | `boolean` | `false` | minimize added space: no indentation nor line breaks |
  65. | `plugins` | `array` | `[]` | plugins to serialize application-specific data types |
  66. | `printBasicPrototype` | `boolean` | `false` | print the prototype for plain objects and arrays |
  67. | `printFunctionName` | `boolean` | `true` | include or omit the name of a function |
  68. | `theme` | `object` | | colors to highlight syntax in terminal |
  69. Property values of `theme` are from [ansi-styles colors](https://github.com/chalk/ansi-styles#colors)
  70. ```js
  71. const DEFAULT_THEME = {
  72. comment: 'gray',
  73. content: 'reset',
  74. prop: 'yellow',
  75. tag: 'cyan',
  76. value: 'green',
  77. };
  78. ```
  79. ## Usage with plugins
  80. The `pretty-format` package provides some built-in plugins, including:
  81. - `ReactElement` for elements from `react`
  82. - `ReactTestComponent` for test objects from `react-test-renderer`
  83. ```js
  84. // CommonJS
  85. const React = require('react');
  86. const renderer = require('react-test-renderer');
  87. const {format: prettyFormat, plugins} = require('pretty-format');
  88. const {ReactElement, ReactTestComponent} = plugins;
  89. ```
  90. ```js
  91. // ES2015 modules and destructuring assignment
  92. import React from 'react';
  93. import renderer from 'react-test-renderer';
  94. import {plugins, format as prettyFormat} from 'pretty-format';
  95. const {ReactElement, ReactTestComponent} = plugins;
  96. ```
  97. ```js
  98. const onClick = () => {};
  99. const element = React.createElement('button', {onClick}, 'Hello World');
  100. const formatted1 = prettyFormat(element, {
  101. plugins: [ReactElement],
  102. printFunctionName: false,
  103. });
  104. const formatted2 = prettyFormat(renderer.create(element).toJSON(), {
  105. plugins: [ReactTestComponent],
  106. printFunctionName: false,
  107. });
  108. /*
  109. <button
  110. onClick=[Function]
  111. >
  112. Hello World
  113. </button>
  114. */
  115. ```
  116. ## Usage in Jest
  117. 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**.
  118. To serialize application-specific data types, you can add modules to `devDependencies` of a project, and then:
  119. In an **individual** test file, you can add a module as follows. It precedes any modules from Jest configuration.
  120. ```js
  121. import serializer from 'my-serializer-module';
  122. expect.addSnapshotSerializer(serializer);
  123. // tests which have `expect(value).toMatchSnapshot()` assertions
  124. ```
  125. 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:
  126. ```json
  127. {
  128. "jest": {
  129. "snapshotSerializers": ["my-serializer-module"]
  130. }
  131. }
  132. ```
  133. ## Writing plugins
  134. A plugin is a JavaScript object.
  135. 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:
  136. - `serialize(val, …)` method of the **improved** interface (available in **version 21** or later)
  137. - `print(val, …)` method of the **original** interface (if plugin does not have `serialize` method)
  138. ### test
  139. 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:
  140. - `TypeError: Cannot read property 'whatever' of null`
  141. - `TypeError: Cannot read property 'whatever' of undefined`
  142. For example, `test` method of built-in `ReactElement` plugin:
  143. ```js
  144. const elementSymbol = Symbol.for('react.element');
  145. const test = val => val && val.$$typeof === elementSymbol;
  146. ```
  147. Pay attention to efficiency in `test` because `pretty-format` calls it often.
  148. ### serialize
  149. The **improved** interface is available in **version 21** or later.
  150. Write `serialize` to return a string, given the arguments:
  151. - `val` which “passed the test”
  152. - unchanging `config` object: derived from `options`
  153. - current `indentation` string: concatenate to `indent` from `config`
  154. - current `depth` number: compare to `maxDepth` from `config`
  155. - current `refs` array: find circular references in objects
  156. - `printer` callback function: serialize children
  157. ### config
  158. <!-- prettier-ignore -->
  159. | key | type | description |
  160. | :------------------ | :-------- | :------------------------------------------------------ |
  161. | `callToJSON` | `boolean` | call `toJSON` method (if it exists) on objects |
  162. | `compareKeys` | `function`| compare function used when sorting object keys |
  163. | `colors` | `Object` | escape codes for colors to highlight syntax |
  164. | `escapeRegex` | `boolean` | escape special characters in regular expressions |
  165. | `escapeString` | `boolean` | escape special characters in strings |
  166. | `indent` | `string` | spaces in each level of indentation |
  167. | `maxDepth` | `number` | levels to print in arrays, objects, elements, and so on |
  168. | `min` | `boolean` | minimize added space: no indentation nor line breaks |
  169. | `plugins` | `array` | plugins to serialize application-specific data types |
  170. | `printFunctionName` | `boolean` | include or omit the name of a function |
  171. | `spacingInner` | `strong` | spacing to separate items in a list |
  172. | `spacingOuter` | `strong` | spacing to enclose a list of items |
  173. Each property of `colors` in `config` corresponds to a property of `theme` in `options`:
  174. - the key is the same (for example, `tag`)
  175. - 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'`)
  176. Some properties in `config` are derived from `min` in `options`:
  177. - `spacingInner` and `spacingOuter` are **newline** if `min` is `false`
  178. - `spacingInner` is **space** and `spacingOuter` is **empty string** if `min` is `true`
  179. ### Example of serialize and test
  180. This plugin is a pattern you can apply to serialize composite data types. Side note: `pretty-format` does not need a plugin to serialize arrays.
  181. ```js
  182. // We reused more code when we factored out a function for child items
  183. // that is independent of depth, name, and enclosing punctuation (see below).
  184. const SEPARATOR = ',';
  185. function serializeItems(items, config, indentation, depth, refs, printer) {
  186. if (items.length === 0) {
  187. return '';
  188. }
  189. const indentationItems = indentation + config.indent;
  190. return (
  191. config.spacingOuter +
  192. items
  193. .map(
  194. item =>
  195. indentationItems +
  196. printer(item, config, indentationItems, depth, refs), // callback
  197. )
  198. .join(SEPARATOR + config.spacingInner) +
  199. (config.min ? '' : SEPARATOR) + // following the last item
  200. config.spacingOuter +
  201. indentation
  202. );
  203. }
  204. const plugin = {
  205. test(val) {
  206. return Array.isArray(val);
  207. },
  208. serialize(array, config, indentation, depth, refs, printer) {
  209. const name = array.constructor.name;
  210. return ++depth > config.maxDepth
  211. ? '[' + name + ']'
  212. : (config.min ? '' : name + ' ') +
  213. '[' +
  214. serializeItems(array, config, indentation, depth, refs, printer) +
  215. ']';
  216. },
  217. };
  218. ```
  219. ```js
  220. const val = {
  221. filter: 'completed',
  222. items: [
  223. {
  224. text: 'Write test',
  225. completed: true,
  226. },
  227. {
  228. text: 'Write serialize',
  229. completed: true,
  230. },
  231. ],
  232. };
  233. ```
  234. ```js
  235. console.log(
  236. prettyFormat(val, {
  237. plugins: [plugin],
  238. }),
  239. );
  240. /*
  241. Object {
  242. "filter": "completed",
  243. "items": Array [
  244. Object {
  245. "completed": true,
  246. "text": "Write test",
  247. },
  248. Object {
  249. "completed": true,
  250. "text": "Write serialize",
  251. },
  252. ],
  253. }
  254. */
  255. ```
  256. ```js
  257. console.log(
  258. prettyFormat(val, {
  259. indent: 4,
  260. plugins: [plugin],
  261. }),
  262. );
  263. /*
  264. Object {
  265. "filter": "completed",
  266. "items": Array [
  267. Object {
  268. "completed": true,
  269. "text": "Write test",
  270. },
  271. Object {
  272. "completed": true,
  273. "text": "Write serialize",
  274. },
  275. ],
  276. }
  277. */
  278. ```
  279. ```js
  280. console.log(
  281. prettyFormat(val, {
  282. maxDepth: 1,
  283. plugins: [plugin],
  284. }),
  285. );
  286. /*
  287. Object {
  288. "filter": "completed",
  289. "items": [Array],
  290. }
  291. */
  292. ```
  293. ```js
  294. console.log(
  295. prettyFormat(val, {
  296. min: true,
  297. plugins: [plugin],
  298. }),
  299. );
  300. /*
  301. {"filter": "completed", "items": [{"completed": true, "text": "Write test"}, {"completed": true, "text": "Write serialize"}]}
  302. */
  303. ```
  304. ### print
  305. The **original** interface is adequate for plugins:
  306. - that **do not** depend on options other than `highlight` or `min`
  307. - that **do not** depend on `depth` or `refs` in recursive traversal, and
  308. - if values either
  309. - do **not** require indentation, or
  310. - do **not** occur as children of JavaScript data structures (for example, array)
  311. Write `print` to return a string, given the arguments:
  312. - `val` which “passed the test”
  313. - current `printer(valChild)` callback function: serialize children
  314. - current `indenter(lines)` callback function: indent lines at the next level
  315. - unchanging `config` object: derived from `options`
  316. - unchanging `colors` object: derived from `options`
  317. The 3 properties of `config` are `min` in `options` and:
  318. - `spacing` and `edgeSpacing` are **newline** if `min` is `false`
  319. - `spacing` is **space** and `edgeSpacing` is **empty string** if `min` is `true`
  320. Each property of `colors` corresponds to a property of `theme` in `options`:
  321. - the key is the same (for example, `tag`)
  322. - 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'`)
  323. ### Example of print and test
  324. This plugin prints functions with the **number of named arguments** excluding rest argument.
  325. ```js
  326. const plugin = {
  327. print(val) {
  328. return `[Function ${val.name || 'anonymous'} ${val.length}]`;
  329. },
  330. test(val) {
  331. return typeof val === 'function';
  332. },
  333. };
  334. ```
  335. ```js
  336. const val = {
  337. onClick(event) {},
  338. render() {},
  339. };
  340. prettyFormat(val, {
  341. plugins: [plugin],
  342. });
  343. /*
  344. Object {
  345. "onClick": [Function onClick 1],
  346. "render": [Function render 0],
  347. }
  348. */
  349. prettyFormat(val);
  350. /*
  351. Object {
  352. "onClick": [Function onClick],
  353. "render": [Function render],
  354. }
  355. */
  356. ```
  357. This plugin **ignores** the `printFunctionName` option. That limitation of the original `print` interface is a reason to use the improved `serialize` interface, described above.
  358. ```js
  359. prettyFormat(val, {
  360. plugins: [pluginOld],
  361. printFunctionName: false,
  362. });
  363. /*
  364. Object {
  365. "onClick": [Function onClick 1],
  366. "render": [Function render 0],
  367. }
  368. */
  369. prettyFormat(val, {
  370. printFunctionName: false,
  371. });
  372. /*
  373. Object {
  374. "onClick": [Function],
  375. "render": [Function],
  376. }
  377. */
  378. ```