Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. # jest-diff
  2. Display differences clearly so people can review changes confidently.
  3. The `diff` named export serializes JavaScript **values**, compares them line-by-line, and returns a string which includes comparison lines.
  4. Two named exports compare **strings** character-by-character:
  5. - `diffStringsUnified` returns a string.
  6. - `diffStringsRaw` returns an array of `Diff` objects.
  7. Three named exports compare **arrays of strings** line-by-line:
  8. - `diffLinesUnified` and `diffLinesUnified2` return a string.
  9. - `diffLinesRaw` returns an array of `Diff` objects.
  10. ## Installation
  11. To add this package as a dependency of a project, run either of the following commands:
  12. - `npm install jest-diff`
  13. - `yarn add jest-diff`
  14. ## Usage of `diff()`
  15. Given JavaScript **values**, `diff(a, b, options?)` does the following:
  16. 1. **serialize** the values as strings using the `pretty-format` package
  17. 2. **compare** the strings line-by-line using the `diff-sequences` package
  18. 3. **format** the changed or common lines using the `chalk` package
  19. To use this function, write either of the following:
  20. - `const {diff} = require('jest-diff');` in CommonJS modules
  21. - `import {diff} from 'jest-diff';` in ECMAScript modules
  22. ### Example of `diff()`
  23. ```js
  24. const a = ['delete', 'common', 'changed from'];
  25. const b = ['common', 'changed to', 'insert'];
  26. const difference = diff(a, b);
  27. ```
  28. The returned **string** consists of:
  29. - annotation lines: describe the two change indicators with labels, and a blank line
  30. - comparison lines: similar to “unified” view on GitHub, but `Expected` lines are green, `Received` lines are red, and common lines are dim (by default, see Options)
  31. ```diff
  32. - Expected
  33. + Received
  34. Array [
  35. - "delete",
  36. "common",
  37. - "changed from",
  38. + "changed to",
  39. + "insert",
  40. ]
  41. ```
  42. ### Edge cases of `diff()`
  43. Here are edge cases for the return value:
  44. - `' Comparing two different types of values. …'` if the arguments have **different types** according to the `jest-get-type` package (instances of different classes have the same `'object'` type)
  45. - `'Compared values have no visual difference.'` if the arguments have either **referential identity** according to `Object.is` method or **same serialization** according to the `pretty-format` package
  46. - `null` if either argument is a so-called **asymmetric matcher** in Jasmine or Jest
  47. ## Usage of diffStringsUnified
  48. Given **strings**, `diffStringsUnified(a, b, options?)` does the following:
  49. 1. **compare** the strings character-by-character using the `diff-sequences` package
  50. 2. **clean up** small (often coincidental) common substrings, also known as chaff
  51. 3. **format** the changed or common lines using the `chalk` package
  52. Although the function is mainly for **multiline** strings, it compares any strings.
  53. Write either of the following:
  54. - `const {diffStringsUnified} = require('jest-diff');` in CommonJS modules
  55. - `import {diffStringsUnified} from 'jest-diff';` in ECMAScript modules
  56. ### Example of diffStringsUnified
  57. ```js
  58. const a = 'common\nchanged from';
  59. const b = 'common\nchanged to';
  60. const difference = diffStringsUnified(a, b);
  61. ```
  62. The returned **string** consists of:
  63. - annotation lines: describe the two change indicators with labels, and a blank line
  64. - comparison lines: similar to “unified” view on GitHub, and **changed substrings** have **inverse** foreground and background colors (that is, `from` has white-on-green and `to` has white-on-red, which the following example does not show)
  65. ```diff
  66. - Expected
  67. + Received
  68. common
  69. - changed from
  70. + changed to
  71. ```
  72. ### Performance of diffStringsUnified
  73. To get the benefit of **changed substrings** within the comparison lines, a character-by-character comparison has a higher computational cost (in time and space) than a line-by-line comparison.
  74. If the input strings can have **arbitrary length**, we recommend that the calling code set a limit, beyond which splits the strings, and then calls `diffLinesUnified` instead. For example, Jest falls back to line-by-line comparison if either string has length greater than 20K characters.
  75. ## Usage of diffLinesUnified
  76. Given **arrays of strings**, `diffLinesUnified(aLines, bLines, options?)` does the following:
  77. 1. **compare** the arrays line-by-line using the `diff-sequences` package
  78. 2. **format** the changed or common lines using the `chalk` package
  79. You might call this function when strings have been split into lines and you do not need to see changed substrings within lines.
  80. ### Example of diffLinesUnified
  81. ```js
  82. const aLines = ['delete', 'common', 'changed from'];
  83. const bLines = ['common', 'changed to', 'insert'];
  84. const difference = diffLinesUnified(aLines, bLines);
  85. ```
  86. ```diff
  87. - Expected
  88. + Received
  89. - delete
  90. common
  91. - changed from
  92. + changed to
  93. + insert
  94. ```
  95. ### Edge cases of diffLinesUnified or diffStringsUnified
  96. Here are edge cases for arguments and return values:
  97. - both `a` and `b` are empty strings: no comparison lines
  98. - only `a` is empty string: all comparison lines have `bColor` and `bIndicator` (see Options)
  99. - only `b` is empty string: all comparison lines have `aColor` and `aIndicator` (see Options)
  100. - `a` and `b` are equal non-empty strings: all comparison lines have `commonColor` and `commonIndicator` (see Options)
  101. ## Usage of diffLinesUnified2
  102. Given two **pairs** of arrays of strings, `diffLinesUnified2(aLinesDisplay, bLinesDisplay, aLinesCompare, bLinesCompare, options?)` does the following:
  103. 1. **compare** the pair of `Compare` arrays line-by-line using the `diff-sequences` package
  104. 2. **format** the corresponding lines in the pair of `Display` arrays using the `chalk` package
  105. Jest calls this function to consider lines as common instead of changed if the only difference is indentation.
  106. You might call this function for case insensitive or Unicode equivalence comparison of lines.
  107. ### Example of diffLinesUnified2
  108. ```js
  109. import {format} from 'pretty-format';
  110. const a = {
  111. text: 'Ignore indentation in serialized object',
  112. time: '2019-09-19T12:34:56.000Z',
  113. type: 'CREATE_ITEM',
  114. };
  115. const b = {
  116. payload: {
  117. text: 'Ignore indentation in serialized object',
  118. time: '2019-09-19T12:34:56.000Z',
  119. },
  120. type: 'CREATE_ITEM',
  121. };
  122. const difference = diffLinesUnified2(
  123. // serialize with indentation to display lines
  124. format(a).split('\n'),
  125. format(b).split('\n'),
  126. // serialize without indentation to compare lines
  127. format(a, {indent: 0}).split('\n'),
  128. format(b, {indent: 0}).split('\n'),
  129. );
  130. ```
  131. The `text` and `time` properties are common, because their only difference is indentation:
  132. ```diff
  133. - Expected
  134. + Received
  135. Object {
  136. + payload: Object {
  137. text: 'Ignore indentation in serialized object',
  138. time: '2019-09-19T12:34:56.000Z',
  139. + },
  140. type: 'CREATE_ITEM',
  141. }
  142. ```
  143. The preceding example illustrates why (at least for indentation) it seems more intuitive that the function returns the common line from the `bLinesDisplay` array instead of from the `aLinesDisplay` array.
  144. ## Usage of diffStringsRaw
  145. Given **strings** and a boolean option, `diffStringsRaw(a, b, cleanup)` does the following:
  146. 1. **compare** the strings character-by-character using the `diff-sequences` package
  147. 2. optionally **clean up** small (often coincidental) common substrings, also known as chaff
  148. Because `diffStringsRaw` returns the difference as **data** instead of a string, you can format it as your application requires (for example, enclosed in HTML markup for browser instead of escape sequences for console).
  149. The returned **array** describes substrings as instances of the `Diff` class, which calling code can access like array tuples:
  150. The value at index `0` is one of the following:
  151. | value | named export | description |
  152. | ----: | :------------ | :-------------------- |
  153. | `0` | `DIFF_EQUAL` | in `a` and in `b` |
  154. | `-1` | `DIFF_DELETE` | in `a` but not in `b` |
  155. | `1` | `DIFF_INSERT` | in `b` but not in `a` |
  156. The value at index `1` is a substring of `a` or `b` or both.
  157. ### Example of diffStringsRaw with cleanup
  158. ```js
  159. const diffs = diffStringsRaw('changed from', 'changed to', true);
  160. ```
  161. | `i` | `diffs[i][0]` | `diffs[i][1]` |
  162. | --: | ------------: | :------------ |
  163. | `0` | `0` | `'changed '` |
  164. | `1` | `-1` | `'from'` |
  165. | `2` | `1` | `'to'` |
  166. ### Example of diffStringsRaw without cleanup
  167. ```js
  168. const diffs = diffStringsRaw('changed from', 'changed to', false);
  169. ```
  170. | `i` | `diffs[i][0]` | `diffs[i][1]` |
  171. | --: | ------------: | :------------ |
  172. | `0` | `0` | `'changed '` |
  173. | `1` | `-1` | `'fr'` |
  174. | `2` | `1` | `'t'` |
  175. | `3` | `0` | `'o'` |
  176. | `4` | `-1` | `'m'` |
  177. ### Advanced import for diffStringsRaw
  178. Here are all the named imports that you might need for the `diffStringsRaw` function:
  179. - `const {DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, Diff, diffStringsRaw} = require('jest-diff');` in CommonJS modules
  180. - `import {DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, Diff, diffStringsRaw} from 'jest-diff';` in ECMAScript modules
  181. To write a **formatting** function, you might need the named constants (and `Diff` in TypeScript annotations).
  182. If you write an application-specific **cleanup** algorithm, then you might need to call the `Diff` constructor:
  183. ```js
  184. const diffCommon = new Diff(DIFF_EQUAL, 'changed ');
  185. const diffDelete = new Diff(DIFF_DELETE, 'from');
  186. const diffInsert = new Diff(DIFF_INSERT, 'to');
  187. ```
  188. ## Usage of diffLinesRaw
  189. Given **arrays of strings**, `diffLinesRaw(aLines, bLines)` does the following:
  190. - **compare** the arrays line-by-line using the `diff-sequences` package
  191. Because `diffLinesRaw` returns the difference as **data** instead of a string, you can format it as your application requires.
  192. ### Example of diffLinesRaw
  193. ```js
  194. const aLines = ['delete', 'common', 'changed from'];
  195. const bLines = ['common', 'changed to', 'insert'];
  196. const diffs = diffLinesRaw(aLines, bLines);
  197. ```
  198. | `i` | `diffs[i][0]` | `diffs[i][1]` |
  199. | --: | ------------: | :--------------- |
  200. | `0` | `-1` | `'delete'` |
  201. | `1` | `0` | `'common'` |
  202. | `2` | `-1` | `'changed from'` |
  203. | `3` | `1` | `'changed to'` |
  204. | `4` | `1` | `'insert'` |
  205. ### Edge case of diffLinesRaw
  206. If you call `string.split('\n')` for an empty string:
  207. - the result is `['']` an array which contains an empty string
  208. - instead of `[]` an empty array
  209. Depending of your application, you might call `diffLinesRaw` with either array.
  210. ### Example of split method
  211. ```js
  212. import {diffLinesRaw} from 'jest-diff';
  213. const a = 'non-empty string';
  214. const b = '';
  215. const diffs = diffLinesRaw(a.split('\n'), b.split('\n'));
  216. ```
  217. | `i` | `diffs[i][0]` | `diffs[i][1]` |
  218. | --: | ------------: | :------------------- |
  219. | `0` | `-1` | `'non-empty string'` |
  220. | `1` | `1` | `''` |
  221. Which you might format as follows:
  222. ```diff
  223. - Expected - 1
  224. + Received + 1
  225. - non-empty string
  226. +
  227. ```
  228. ### Example of splitLines0 function
  229. For edge case behavior like the `diffLinesUnified` function, you might define a `splitLines0` function, which given an empty string, returns `[]` an empty array:
  230. ```js
  231. export const splitLines0 = string =>
  232. string.length === 0 ? [] : string.split('\n');
  233. ```
  234. ```js
  235. import {diffLinesRaw} from 'jest-diff';
  236. const a = '';
  237. const b = 'line 1\nline 2\nline 3';
  238. const diffs = diffLinesRaw(a.split('\n'), b.split('\n'));
  239. ```
  240. | `i` | `diffs[i][0]` | `diffs[i][1]` |
  241. | --: | ------------: | :------------ |
  242. | `0` | `1` | `'line 1'` |
  243. | `1` | `1` | `'line 2'` |
  244. | `2` | `1` | `'line 3'` |
  245. Which you might format as follows:
  246. ```diff
  247. - Expected - 0
  248. + Received + 3
  249. + line 1
  250. + line 2
  251. + line 3
  252. ```
  253. In contrast to the `diffLinesRaw` function, the `diffLinesUnified` and `diffLinesUnified2` functions **automatically** convert array arguments computed by string `split` method, so callers do **not** need a `splitLine0` function.
  254. ## Options
  255. The default options are for the report when an assertion fails from the `expect` package used by Jest.
  256. For other applications, you can provide an options object as a third argument:
  257. - `diff(a, b, options)`
  258. - `diffStringsUnified(a, b, options)`
  259. - `diffLinesUnified(aLines, bLines, options)`
  260. - `diffLinesUnified2(aLinesDisplay, bLinesDisplay, aLinesCompare, bLinesCompare, options)`
  261. ### Properties of options object
  262. | name | default |
  263. | :-------------------------------- | :----------------- |
  264. | `aAnnotation` | `'Expected'` |
  265. | `aColor` | `chalk.green` |
  266. | `aIndicator` | `'-'` |
  267. | `bAnnotation` | `'Received'` |
  268. | `bColor` | `chalk.red` |
  269. | `bIndicator` | `'+'` |
  270. | `changeColor` | `chalk.inverse` |
  271. | `changeLineTrailingSpaceColor` | `string => string` |
  272. | `commonColor` | `chalk.dim` |
  273. | `commonIndicator` | `' '` |
  274. | `commonLineTrailingSpaceColor` | `string => string` |
  275. | `contextLines` | `5` |
  276. | `emptyFirstOrLastLinePlaceholder` | `''` |
  277. | `expand` | `true` |
  278. | `includeChangeCounts` | `false` |
  279. | `omitAnnotationLines` | `false` |
  280. | `patchColor` | `chalk.yellow` |
  281. For more information about the options, see the following examples.
  282. ### Example of options for labels
  283. If the application is code modification, you might replace the labels:
  284. ```js
  285. const options = {
  286. aAnnotation: 'Original',
  287. bAnnotation: 'Modified',
  288. };
  289. ```
  290. ```diff
  291. - Original
  292. + Modified
  293. common
  294. - changed from
  295. + changed to
  296. ```
  297. The `jest-diff` package does not assume that the 2 labels have equal length.
  298. ### Example of options for colors of changed lines
  299. For consistency with most diff tools, you might exchange the colors:
  300. ```ts
  301. import chalk = require('chalk');
  302. const options = {
  303. aColor: chalk.red,
  304. bColor: chalk.green,
  305. };
  306. ```
  307. ### Example of option for color of changed substrings
  308. Although the default inverse of foreground and background colors is hard to beat for changed substrings **within lines**, especially because it highlights spaces, if you want bold font weight on yellow background color:
  309. ```ts
  310. import chalk = require('chalk');
  311. const options = {
  312. changeColor: chalk.bold.bgYellowBright,
  313. };
  314. ```
  315. ### Example of option to format trailing spaces
  316. Because `diff()` does not display substring differences within lines, formatting can help you see when lines differ by the presence or absence of trailing spaces found by `/\s+$/` regular expression.
  317. - If change lines have a background color, then you can see trailing spaces.
  318. - If common lines have default dim color, then you cannot see trailing spaces. You might want yellowish background color to see them.
  319. ```js
  320. const options = {
  321. aColor: chalk.rgb(128, 0, 128).bgRgb(255, 215, 255), // magenta
  322. bColor: chalk.rgb(0, 95, 0).bgRgb(215, 255, 215), // green
  323. commonLineTrailingSpaceColor: chalk.bgYellow,
  324. };
  325. ```
  326. The value of a Color option is a function, which given a string, returns a string.
  327. If you want to replace trailing spaces with middle dot characters:
  328. ```js
  329. const replaceSpacesWithMiddleDot = string => '·'.repeat(string.length);
  330. const options = {
  331. changeLineTrailingSpaceColor: replaceSpacesWithMiddleDot,
  332. commonLineTrailingSpaceColor: replaceSpacesWithMiddleDot,
  333. };
  334. ```
  335. If you need the TypeScript type of a Color option:
  336. ```ts
  337. import {DiffOptionsColor} from 'jest-diff';
  338. ```
  339. ### Example of options for no colors
  340. To store the difference in a file without escape codes for colors, provide an identity function:
  341. ```js
  342. const noColor = string => string;
  343. const options = {
  344. aColor: noColor,
  345. bColor: noColor,
  346. changeColor: noColor,
  347. commonColor: noColor,
  348. patchColor: noColor,
  349. };
  350. ```
  351. ### Example of options for indicators
  352. For consistency with the `diff` command, you might replace the indicators:
  353. ```js
  354. const options = {
  355. aIndicator: '<',
  356. bIndicator: '>',
  357. };
  358. ```
  359. The `jest-diff` package assumes (but does not enforce) that the 3 indicators have equal length.
  360. ### Example of options to limit common lines
  361. By default, the output includes all common lines.
  362. To emphasize the changes, you might limit the number of common “context” lines:
  363. ```js
  364. const options = {
  365. contextLines: 1,
  366. expand: false,
  367. };
  368. ```
  369. A patch mark like `@@ -12,7 +12,9 @@` accounts for omitted common lines.
  370. ### Example of option for color of patch marks
  371. If you want patch marks to have the same dim color as common lines:
  372. ```ts
  373. import chalk = require('chalk');
  374. const options = {
  375. expand: false,
  376. patchColor: chalk.dim,
  377. };
  378. ```
  379. ### Example of option to include change counts
  380. To display the number of changed lines at the right of annotation lines:
  381. ```js
  382. const a = ['common', 'changed from'];
  383. const b = ['common', 'changed to', 'insert'];
  384. const options = {
  385. includeChangeCounts: true,
  386. };
  387. const difference = diff(a, b, options);
  388. ```
  389. ```diff
  390. - Expected - 1
  391. + Received + 2
  392. Array [
  393. "common",
  394. - "changed from",
  395. + "changed to",
  396. + "insert",
  397. ]
  398. ```
  399. ### Example of option to omit annotation lines
  400. To display only the comparison lines:
  401. ```js
  402. const a = 'common\nchanged from';
  403. const b = 'common\nchanged to';
  404. const options = {
  405. omitAnnotationLines: true,
  406. };
  407. const difference = diffStringsUnified(a, b, options);
  408. ```
  409. ```diff
  410. common
  411. - changed from
  412. + changed to
  413. ```
  414. ### Example of option for empty first or last lines
  415. If the **first** or **last** comparison line is **empty**, because the content is empty and the indicator is a space, you might not notice it.
  416. The replacement option is a string whose default value is `''` empty string.
  417. Because Jest trims the report when a matcher fails, it deletes an empty last line.
  418. Therefore, Jest uses as placeholder the downwards arrow with corner leftwards:
  419. ```js
  420. const options = {
  421. emptyFirstOrLastLinePlaceholder: '↵', // U+21B5
  422. };
  423. ```
  424. If a content line is empty, then the corresponding comparison line is automatically trimmed to remove the margin space (represented as a middle dot below) for the default indicators:
  425. | Indicator | untrimmed | trimmed |
  426. | ----------------: | :-------- | :------ |
  427. | `aIndicator` | `'-·'` | `'-'` |
  428. | `bIndicator` | `'+·'` | `'+'` |
  429. | `commonIndicator` | `' ·'` | `''` |