2018-05-09 12:01:36 +05:30
|
|
|
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,
|
2018-12-13 13:39:08 +05:30
|
|
|
endLineNumber: lineNumber + change.count - 1,
|
2018-05-09 12:01:36 +05:30
|
|
|
});
|
|
|
|
} else if ('added' in change || 'removed' in change) {
|
2020-05-24 23:13:21 +05:30
|
|
|
acc.push({
|
|
|
|
...change,
|
|
|
|
lineNumber,
|
|
|
|
modified: undefined,
|
|
|
|
endLineNumber: lineNumber + change.count - 1,
|
|
|
|
});
|
2018-05-09 12:01:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (!change.removed) {
|
|
|
|
lineNumber += change.count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, []);
|
|
|
|
};
|