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.

joinAlignedDiffs.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.joinAlignedDiffsExpand = exports.joinAlignedDiffsNoExpand = void 0;
  6. var _cleanupSemantic = require('./cleanupSemantic');
  7. var _printDiffs = require('./printDiffs');
  8. /**
  9. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  10. *
  11. * This source code is licensed under the MIT license found in the
  12. * LICENSE file in the root directory of this source tree.
  13. */
  14. // jest --no-expand
  15. //
  16. // Given array of aligned strings with inverse highlight formatting,
  17. // return joined lines with diff formatting (and patch marks, if needed).
  18. const joinAlignedDiffsNoExpand = (diffs, options) => {
  19. const iLength = diffs.length;
  20. const nContextLines = options.contextLines;
  21. const nContextLines2 = nContextLines + nContextLines; // First pass: count output lines and see if it has patches.
  22. let jLength = iLength;
  23. let hasExcessAtStartOrEnd = false;
  24. let nExcessesBetweenChanges = 0;
  25. let i = 0;
  26. while (i !== iLength) {
  27. const iStart = i;
  28. while (i !== iLength && diffs[i][0] === _cleanupSemantic.DIFF_EQUAL) {
  29. i += 1;
  30. }
  31. if (iStart !== i) {
  32. if (iStart === 0) {
  33. // at start
  34. if (i > nContextLines) {
  35. jLength -= i - nContextLines; // subtract excess common lines
  36. hasExcessAtStartOrEnd = true;
  37. }
  38. } else if (i === iLength) {
  39. // at end
  40. const n = i - iStart;
  41. if (n > nContextLines) {
  42. jLength -= n - nContextLines; // subtract excess common lines
  43. hasExcessAtStartOrEnd = true;
  44. }
  45. } else {
  46. // between changes
  47. const n = i - iStart;
  48. if (n > nContextLines2) {
  49. jLength -= n - nContextLines2; // subtract excess common lines
  50. nExcessesBetweenChanges += 1;
  51. }
  52. }
  53. }
  54. while (i !== iLength && diffs[i][0] !== _cleanupSemantic.DIFF_EQUAL) {
  55. i += 1;
  56. }
  57. }
  58. const hasPatch = nExcessesBetweenChanges !== 0 || hasExcessAtStartOrEnd;
  59. if (nExcessesBetweenChanges !== 0) {
  60. jLength += nExcessesBetweenChanges + 1; // add patch lines
  61. } else if (hasExcessAtStartOrEnd) {
  62. jLength += 1; // add patch line
  63. }
  64. const jLast = jLength - 1;
  65. const lines = [];
  66. let jPatchMark = 0; // index of placeholder line for current patch mark
  67. if (hasPatch) {
  68. lines.push(''); // placeholder line for first patch mark
  69. } // Indexes of expected or received lines in current patch:
  70. let aStart = 0;
  71. let bStart = 0;
  72. let aEnd = 0;
  73. let bEnd = 0;
  74. const pushCommonLine = line => {
  75. const j = lines.length;
  76. lines.push(
  77. (0, _printDiffs.printCommonLine)(line, j === 0 || j === jLast, options)
  78. );
  79. aEnd += 1;
  80. bEnd += 1;
  81. };
  82. const pushDeleteLine = line => {
  83. const j = lines.length;
  84. lines.push(
  85. (0, _printDiffs.printDeleteLine)(line, j === 0 || j === jLast, options)
  86. );
  87. aEnd += 1;
  88. };
  89. const pushInsertLine = line => {
  90. const j = lines.length;
  91. lines.push(
  92. (0, _printDiffs.printInsertLine)(line, j === 0 || j === jLast, options)
  93. );
  94. bEnd += 1;
  95. }; // Second pass: push lines with diff formatting (and patch marks, if needed).
  96. i = 0;
  97. while (i !== iLength) {
  98. let iStart = i;
  99. while (i !== iLength && diffs[i][0] === _cleanupSemantic.DIFF_EQUAL) {
  100. i += 1;
  101. }
  102. if (iStart !== i) {
  103. if (iStart === 0) {
  104. // at beginning
  105. if (i > nContextLines) {
  106. iStart = i - nContextLines;
  107. aStart = iStart;
  108. bStart = iStart;
  109. aEnd = aStart;
  110. bEnd = bStart;
  111. }
  112. for (let iCommon = iStart; iCommon !== i; iCommon += 1) {
  113. pushCommonLine(diffs[iCommon][1]);
  114. }
  115. } else if (i === iLength) {
  116. // at end
  117. const iEnd = i - iStart > nContextLines ? iStart + nContextLines : i;
  118. for (let iCommon = iStart; iCommon !== iEnd; iCommon += 1) {
  119. pushCommonLine(diffs[iCommon][1]);
  120. }
  121. } else {
  122. // between changes
  123. const nCommon = i - iStart;
  124. if (nCommon > nContextLines2) {
  125. const iEnd = iStart + nContextLines;
  126. for (let iCommon = iStart; iCommon !== iEnd; iCommon += 1) {
  127. pushCommonLine(diffs[iCommon][1]);
  128. }
  129. lines[jPatchMark] = (0, _printDiffs.createPatchMark)(
  130. aStart,
  131. aEnd,
  132. bStart,
  133. bEnd,
  134. options
  135. );
  136. jPatchMark = lines.length;
  137. lines.push(''); // placeholder line for next patch mark
  138. const nOmit = nCommon - nContextLines2;
  139. aStart = aEnd + nOmit;
  140. bStart = bEnd + nOmit;
  141. aEnd = aStart;
  142. bEnd = bStart;
  143. for (let iCommon = i - nContextLines; iCommon !== i; iCommon += 1) {
  144. pushCommonLine(diffs[iCommon][1]);
  145. }
  146. } else {
  147. for (let iCommon = iStart; iCommon !== i; iCommon += 1) {
  148. pushCommonLine(diffs[iCommon][1]);
  149. }
  150. }
  151. }
  152. }
  153. while (i !== iLength && diffs[i][0] === _cleanupSemantic.DIFF_DELETE) {
  154. pushDeleteLine(diffs[i][1]);
  155. i += 1;
  156. }
  157. while (i !== iLength && diffs[i][0] === _cleanupSemantic.DIFF_INSERT) {
  158. pushInsertLine(diffs[i][1]);
  159. i += 1;
  160. }
  161. }
  162. if (hasPatch) {
  163. lines[jPatchMark] = (0, _printDiffs.createPatchMark)(
  164. aStart,
  165. aEnd,
  166. bStart,
  167. bEnd,
  168. options
  169. );
  170. }
  171. return lines.join('\n');
  172. }; // jest --expand
  173. //
  174. // Given array of aligned strings with inverse highlight formatting,
  175. // return joined lines with diff formatting.
  176. exports.joinAlignedDiffsNoExpand = joinAlignedDiffsNoExpand;
  177. const joinAlignedDiffsExpand = (diffs, options) =>
  178. diffs
  179. .map((diff, i, diffs) => {
  180. const line = diff[1];
  181. const isFirstOrLast = i === 0 || i === diffs.length - 1;
  182. switch (diff[0]) {
  183. case _cleanupSemantic.DIFF_DELETE:
  184. return (0, _printDiffs.printDeleteLine)(line, isFirstOrLast, options);
  185. case _cleanupSemantic.DIFF_INSERT:
  186. return (0, _printDiffs.printInsertLine)(line, isFirstOrLast, options);
  187. default:
  188. return (0, _printDiffs.printCommonLine)(line, isFirstOrLast, options);
  189. }
  190. })
  191. .join('\n');
  192. exports.joinAlignedDiffsExpand = joinAlignedDiffsExpand;