reverse.js 1.1 KB

1234567891011121314151617181920212223
  1. export function reversePatch(structuredPatch) {
  2. if (Array.isArray(structuredPatch)) {
  3. // (See comment in unixToWin for why we need the pointless-looking anonymous function here)
  4. return structuredPatch.map(patch => reversePatch(patch)).reverse();
  5. }
  6. return Object.assign(Object.assign({}, structuredPatch), { oldFileName: structuredPatch.newFileName, oldHeader: structuredPatch.newHeader, newFileName: structuredPatch.oldFileName, newHeader: structuredPatch.oldHeader, hunks: structuredPatch.hunks.map(hunk => {
  7. return {
  8. oldLines: hunk.newLines,
  9. oldStart: hunk.newStart,
  10. newLines: hunk.oldLines,
  11. newStart: hunk.oldStart,
  12. lines: hunk.lines.map(l => {
  13. if (l.startsWith('-')) {
  14. return `+${l.slice(1)}`;
  15. }
  16. if (l.startsWith('+')) {
  17. return `-${l.slice(1)}`;
  18. }
  19. return l;
  20. })
  21. };
  22. }) });
  23. }