版博士V2.0程序
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

README.md 9.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. # jsdiff
  2. [![Build Status](https://secure.travis-ci.org/kpdecker/jsdiff.svg)](http://travis-ci.org/kpdecker/jsdiff)
  3. [![Sauce Test Status](https://saucelabs.com/buildstatus/jsdiff)](https://saucelabs.com/u/jsdiff)
  4. A javascript text differencing implementation.
  5. Based on the algorithm proposed in
  6. ["An O(ND) Difference Algorithm and its Variations" (Myers, 1986)](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.4.6927).
  7. ## Installation
  8. ```bash
  9. npm install diff --save
  10. ```
  11. ## API
  12. * `Diff.diffChars(oldStr, newStr[, options])` - diffs two blocks of text, comparing character by character.
  13. Returns a list of change objects (See below).
  14. Options
  15. * `ignoreCase`: `true` to ignore casing difference. Defaults to `false`.
  16. * `Diff.diffWords(oldStr, newStr[, options])` - diffs two blocks of text, comparing word by word, ignoring whitespace.
  17. Returns a list of change objects (See below).
  18. Options
  19. * `ignoreCase`: Same as in `diffChars`.
  20. * `Diff.diffWordsWithSpace(oldStr, newStr[, options])` - diffs two blocks of text, comparing word by word, treating whitespace as significant.
  21. Returns a list of change objects (See below).
  22. * `Diff.diffLines(oldStr, newStr[, options])` - diffs two blocks of text, comparing line by line.
  23. Options
  24. * `ignoreWhitespace`: `true` to ignore leading and trailing whitespace. This is the same as `diffTrimmedLines`
  25. * `newlineIsToken`: `true` to treat newline characters as separate tokens. This allows for changes to the newline structure to occur independently of the line content and to be treated as such. In general this is the more human friendly form of `diffLines` and `diffLines` is better suited for patches and other computer friendly output.
  26. Returns a list of change objects (See below).
  27. * `Diff.diffTrimmedLines(oldStr, newStr[, options])` - diffs two blocks of text, comparing line by line, ignoring leading and trailing whitespace.
  28. Returns a list of change objects (See below).
  29. * `Diff.diffSentences(oldStr, newStr[, options])` - diffs two blocks of text, comparing sentence by sentence.
  30. Returns a list of change objects (See below).
  31. * `Diff.diffCss(oldStr, newStr[, options])` - diffs two blocks of text, comparing CSS tokens.
  32. Returns a list of change objects (See below).
  33. * `Diff.diffJson(oldObj, newObj[, options])` - diffs two JSON objects, comparing the fields defined on each. The order of fields, etc does not matter in this comparison.
  34. Returns a list of change objects (See below).
  35. * `Diff.diffArrays(oldArr, newArr[, options])` - diffs two arrays, comparing each item for strict equality (===).
  36. Options
  37. * `comparator`: `function(left, right)` for custom equality checks
  38. Returns a list of change objects (See below).
  39. * `Diff.createTwoFilesPatch(oldFileName, newFileName, oldStr, newStr, oldHeader, newHeader)` - creates a unified diff patch.
  40. Parameters:
  41. * `oldFileName` : String to be output in the filename section of the patch for the removals
  42. * `newFileName` : String to be output in the filename section of the patch for the additions
  43. * `oldStr` : Original string value
  44. * `newStr` : New string value
  45. * `oldHeader` : Additional information to include in the old file header
  46. * `newHeader` : Additional information to include in the new file header
  47. * `options` : An object with options.
  48. - `context` describes how many lines of context should be included.
  49. - `ignoreWhitespace`: `true` to ignore leading and trailing whitespace.
  50. - `newlineIsToken`: `true` to treat newline characters as separate tokens. This allows for changes to the newline structure to occur independently of the line content and to be treated as such. In general this is the more human friendly form of `diffLines` and `diffLines` is better suited for patches and other computer friendly output.
  51. * `Diff.createPatch(fileName, oldStr, newStr, oldHeader, newHeader)` - creates a unified diff patch.
  52. Just like Diff.createTwoFilesPatch, but with oldFileName being equal to newFileName.
  53. * `Diff.structuredPatch(oldFileName, newFileName, oldStr, newStr, oldHeader, newHeader, options)` - returns an object with an array of hunk objects.
  54. This method is similar to createTwoFilesPatch, but returns a data structure
  55. suitable for further processing. Parameters are the same as createTwoFilesPatch. The data structure returned may look like this:
  56. ```js
  57. {
  58. oldFileName: 'oldfile', newFileName: 'newfile',
  59. oldHeader: 'header1', newHeader: 'header2',
  60. hunks: [{
  61. oldStart: 1, oldLines: 3, newStart: 1, newLines: 3,
  62. lines: [' line2', ' line3', '-line4', '+line5', '\\ No newline at end of file'],
  63. }]
  64. }
  65. ```
  66. * `Diff.applyPatch(source, patch[, options])` - applies a unified diff patch.
  67. Return a string containing new version of provided data. `patch` may be a string diff or the output from the `parsePatch` or `structuredPatch` methods.
  68. The optional `options` object may have the following keys:
  69. - `fuzzFactor`: Number of lines that are allowed to differ before rejecting a patch. Defaults to 0.
  70. - `compareLine(lineNumber, line, operation, patchContent)`: Callback used to compare to given lines to determine if they should be considered equal when patching. Defaults to strict equality but may be overridden to provide fuzzier comparison. Should return false if the lines should be rejected.
  71. * `Diff.applyPatches(patch, options)` - applies one or more patches.
  72. This method will iterate over the contents of the patch and apply to data provided through callbacks. The general flow for each patch index is:
  73. - `options.loadFile(index, callback)` is called. The caller should then load the contents of the file and then pass that to the `callback(err, data)` callback. Passing an `err` will terminate further patch execution.
  74. - `options.patched(index, content, callback)` is called once the patch has been applied. `content` will be the return value from `applyPatch`. When it's ready, the caller should call `callback(err)` callback. Passing an `err` will terminate further patch execution.
  75. Once all patches have been applied or an error occurs, the `options.complete(err)` callback is made.
  76. * `Diff.parsePatch(diffStr)` - Parses a patch into structured data
  77. Return a JSON object representation of the a patch, suitable for use with the `applyPatch` method. This parses to the same structure returned by `Diff.structuredPatch`.
  78. * `convertChangesToXML(changes)` - converts a list of changes to a serialized XML format
  79. All methods above which accept the optional `callback` method will run in sync mode when that parameter is omitted and in async mode when supplied. This allows for larger diffs without blocking the event loop. This may be passed either directly as the final parameter or as the `callback` field in the `options` object.
  80. ### Change Objects
  81. Many of the methods above return change objects. These objects consist of the following fields:
  82. * `value`: Text content
  83. * `added`: True if the value was inserted into the new string
  84. * `removed`: True if the value was removed from the old string
  85. Note that some cases may omit a particular flag field. Comparison on the flag fields should always be done in a truthy or falsy manner.
  86. ## Examples
  87. Basic example in Node
  88. ```js
  89. require('colors');
  90. const Diff = require('diff');
  91. const one = 'beep boop';
  92. const other = 'beep boob blah';
  93. const diff = Diff.diffChars(one, other);
  94. diff.forEach((part) => {
  95. // green for additions, red for deletions
  96. // grey for common parts
  97. const color = part.added ? 'green' :
  98. part.removed ? 'red' : 'grey';
  99. process.stderr.write(part.value[color]);
  100. });
  101. console.log();
  102. ```
  103. Running the above program should yield
  104. <img src="images/node_example.png" alt="Node Example">
  105. Basic example in a web page
  106. ```html
  107. <pre id="display"></pre>
  108. <script src="diff.js"></script>
  109. <script>
  110. const one = 'beep boop',
  111. other = 'beep boob blah',
  112. color = '';
  113. let span = null;
  114. const diff = Diff.diffChars(one, other),
  115. display = document.getElementById('display'),
  116. fragment = document.createDocumentFragment();
  117. diff.forEach((part) => {
  118. // green for additions, red for deletions
  119. // grey for common parts
  120. const color = part.added ? 'green' :
  121. part.removed ? 'red' : 'grey';
  122. span = document.createElement('span');
  123. span.style.color = color;
  124. span.appendChild(document
  125. .createTextNode(part.value));
  126. fragment.appendChild(span);
  127. });
  128. display.appendChild(fragment);
  129. </script>
  130. ```
  131. Open the above .html file in a browser and you should see
  132. <img src="images/web_example.png" alt="Node Example">
  133. **[Full online demo](https://kpdecker.github.io/jsdiff)**
  134. ## Compatibility
  135. [![Sauce Test Status](https://saucelabs.com/browser-matrix/jsdiff.svg)](https://saucelabs.com/u/jsdiff)
  136. jsdiff supports all ES3 environments with some known issues on IE8 and below. Under these browsers some diff algorithms such as word diff and others may fail due to lack of support for capturing groups in the `split` operation.
  137. ## License
  138. See [LICENSE](https://github.com/kpdecker/jsdiff/blob/master/LICENSE).