debian-mirror-gitlab/app/assets/javascripts/ide/lib/diff/diff.js
2020-05-24 23:13:21 +05:30

31 lines
810 B
JavaScript

import { diffLines } from 'diff';
// eslint-disable-next-line import/prefer-default-export
export const computeDiff = (originalContent, newContent) => {
const changes = diffLines(originalContent, newContent);
let lineNumber = 1;
return changes.reduce((acc, change) => {
const findOnLine = acc.find(c => c.lineNumber === lineNumber);
if (findOnLine) {
Object.assign(findOnLine, change, {
modified: true,
endLineNumber: lineNumber + change.count - 1,
});
} else if ('added' in change || 'removed' in change) {
acc.push({
...change,
lineNumber,
modified: undefined,
endLineNumber: lineNumber + change.count - 1,
});
}
if (!change.removed) {
lineNumber += change.count;
}
return acc;
}, []);
};