diffLines.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.diffLinesRaw = exports.diffLinesUnified2 = exports.diffLinesUnified = void 0;
  6. var _diffSequences = _interopRequireDefault(require('diff-sequences'));
  7. var _cleanupSemantic = require('./cleanupSemantic');
  8. var _normalizeDiffOptions = require('./normalizeDiffOptions');
  9. var _printDiffs = require('./printDiffs');
  10. function _interopRequireDefault(obj) {
  11. return obj && obj.__esModule ? obj : {default: obj};
  12. }
  13. /**
  14. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  15. *
  16. * This source code is licensed under the MIT license found in the
  17. * LICENSE file in the root directory of this source tree.
  18. */
  19. const isEmptyString = lines => lines.length === 1 && lines[0].length === 0; // Compare two arrays of strings line-by-line. Format as comparison lines.
  20. const diffLinesUnified = (aLines, bLines, options) =>
  21. (0, _printDiffs.printDiffLines)(
  22. diffLinesRaw(
  23. isEmptyString(aLines) ? [] : aLines,
  24. isEmptyString(bLines) ? [] : bLines
  25. ),
  26. (0, _normalizeDiffOptions.normalizeDiffOptions)(options)
  27. ); // Given two pairs of arrays of strings:
  28. // Compare the pair of comparison arrays line-by-line.
  29. // Format the corresponding lines in the pair of displayable arrays.
  30. exports.diffLinesUnified = diffLinesUnified;
  31. const diffLinesUnified2 = (
  32. aLinesDisplay,
  33. bLinesDisplay,
  34. aLinesCompare,
  35. bLinesCompare,
  36. options
  37. ) => {
  38. if (isEmptyString(aLinesDisplay) && isEmptyString(aLinesCompare)) {
  39. aLinesDisplay = [];
  40. aLinesCompare = [];
  41. }
  42. if (isEmptyString(bLinesDisplay) && isEmptyString(bLinesCompare)) {
  43. bLinesDisplay = [];
  44. bLinesCompare = [];
  45. }
  46. if (
  47. aLinesDisplay.length !== aLinesCompare.length ||
  48. bLinesDisplay.length !== bLinesCompare.length
  49. ) {
  50. // Fall back to diff of display lines.
  51. return diffLinesUnified(aLinesDisplay, bLinesDisplay, options);
  52. }
  53. const diffs = diffLinesRaw(aLinesCompare, bLinesCompare); // Replace comparison lines with displayable lines.
  54. let aIndex = 0;
  55. let bIndex = 0;
  56. diffs.forEach(diff => {
  57. switch (diff[0]) {
  58. case _cleanupSemantic.DIFF_DELETE:
  59. diff[1] = aLinesDisplay[aIndex];
  60. aIndex += 1;
  61. break;
  62. case _cleanupSemantic.DIFF_INSERT:
  63. diff[1] = bLinesDisplay[bIndex];
  64. bIndex += 1;
  65. break;
  66. default:
  67. diff[1] = bLinesDisplay[bIndex];
  68. aIndex += 1;
  69. bIndex += 1;
  70. }
  71. });
  72. return (0, _printDiffs.printDiffLines)(
  73. diffs,
  74. (0, _normalizeDiffOptions.normalizeDiffOptions)(options)
  75. );
  76. }; // Compare two arrays of strings line-by-line.
  77. exports.diffLinesUnified2 = diffLinesUnified2;
  78. const diffLinesRaw = (aLines, bLines) => {
  79. const aLength = aLines.length;
  80. const bLength = bLines.length;
  81. const isCommon = (aIndex, bIndex) => aLines[aIndex] === bLines[bIndex];
  82. const diffs = [];
  83. let aIndex = 0;
  84. let bIndex = 0;
  85. const foundSubsequence = (nCommon, aCommon, bCommon) => {
  86. for (; aIndex !== aCommon; aIndex += 1) {
  87. diffs.push(
  88. new _cleanupSemantic.Diff(_cleanupSemantic.DIFF_DELETE, aLines[aIndex])
  89. );
  90. }
  91. for (; bIndex !== bCommon; bIndex += 1) {
  92. diffs.push(
  93. new _cleanupSemantic.Diff(_cleanupSemantic.DIFF_INSERT, bLines[bIndex])
  94. );
  95. }
  96. for (; nCommon !== 0; nCommon -= 1, aIndex += 1, bIndex += 1) {
  97. diffs.push(
  98. new _cleanupSemantic.Diff(_cleanupSemantic.DIFF_EQUAL, bLines[bIndex])
  99. );
  100. }
  101. };
  102. (0, _diffSequences.default)(aLength, bLength, isCommon, foundSubsequence); // After the last common subsequence, push remaining change items.
  103. for (; aIndex !== aLength; aIndex += 1) {
  104. diffs.push(
  105. new _cleanupSemantic.Diff(_cleanupSemantic.DIFF_DELETE, aLines[aIndex])
  106. );
  107. }
  108. for (; bIndex !== bLength; bIndex += 1) {
  109. diffs.push(
  110. new _cleanupSemantic.Diff(_cleanupSemantic.DIFF_INSERT, bLines[bIndex])
  111. );
  112. }
  113. return diffs;
  114. };
  115. exports.diffLinesRaw = diffLinesRaw;