From 46c5f1339692087fa28eb2140e4226e4b2a0a3f9 Mon Sep 17 00:00:00 2001 From: DanDaManTran Date: Tue, 25 Jun 2024 15:26:04 -0500 Subject: [PATCH] Displaying removal and adding of lines --- diff.js | 6 ++-- src/diffchecker.js | 59 ++++++++++++++++++++++++++------------- test/dir1/hello-world1.js | 2 +- test/dir2/hello-world2.js | 4 +-- test/test.js | 8 +++--- 5 files changed, 50 insertions(+), 29 deletions(-) diff --git a/diff.js b/diff.js index bf049f5..5f0325b 100644 --- a/diff.js +++ b/diff.js @@ -101,11 +101,13 @@ module.exports = { } var file1 = fs.readFileSync(files[0]).toString(); var file2 = fs.readFileSync(files[1]).toString(); + var {diffLines, diffFile} = diffChecker.diff(file1, file2); + var validCurData = (curData && curData.length !== 2); var fileType = convertType(path.extname(files[1]).substring(1)); var useCurData = useCurData ? validCurData && (typeof curData.description === "string") && !curData.body : false;; - var markdown = '\n```' + fileType + '\n' + file2 + '\n```'; - var diffLines = diffChecker.diff(file1, file2); + var markdown = '\n```' + fileType + '\n' + diffFile.join('\n') + '\n```'; + if(diffLines.length) { var lines = checkSequence(diffLines); markdown += '\n
' + '\n'; diff --git a/src/diffchecker.js b/src/diffchecker.js index 4d7a8a0..1b5dd5c 100644 --- a/src/diffchecker.js +++ b/src/diffchecker.js @@ -1,26 +1,45 @@ +const Diff = require('diff'); + +function normalizeLines(str) { + const strWithNewLine = str.endsWith('\n') ? str : str + '\n'; + + return strWithNewLine.split('\n').map(line => line.replace(/\s+$/, '')).join('\n'); +} + module.exports = { diff: function(ogString, newString) { - var diffLines = []; - var ogLines = ogString.split('\n').map(line => line.trim()); - var ogLinesStripped = [] - for (var index = 0; index < ogLines.length; index++) { - ogLinesStripped.push(ogLines[index].replace(/;/g, "")); - } - var newLines = newString.split('\n').map(line => line.trim()); - - for (var index = 0; index < newLines.length; index++) { - var lineNumber = index + 1; - var newLine = newLines[index].replace(/;/g, ""); - var newLineIndx = ogLinesStripped.indexOf(newLine); - if (newLine != '') { - if (newLineIndx === -1) { - diffLines.push(lineNumber); + const normalizedOgString = normalizeLines(ogString); + const normalizedNewString = normalizeLines(newString); + + const diff = Diff.diffLines(normalizedOgString, normalizedNewString); + const diffLines = []; + const diffFile = []; + let lineNumberNewFile = 1; + + diff.forEach((part) => { + const lines = part.value.split('\n'); + + lines.forEach((line, index) => { + if (index === lines.length - 1 && line === '') { + return; } - else { - ogLinesStripped.splice(newLineIndx, 1); + + if (part.added) { + diffFile.push(`+ ${line}`) + diffLines.push(lineNumberNewFile); + lineNumberNewFile++; + } else if (part.removed) { + diffFile.push(`- ${line}`) + diffLines.push(lineNumberNewFile); + + lineNumberNewFile++; + } else { + diffFile.push(` ${line}`) + lineNumberNewFile++; } - } - } - return diffLines + }); + }); + + return {diffLines, diffFile} } } diff --git a/test/dir1/hello-world1.js b/test/dir1/hello-world1.js index 0f50426..a1600c2 100644 --- a/test/dir1/hello-world1.js +++ b/test/dir1/hello-world1.js @@ -1 +1 @@ -console.log("hello world"); \ No newline at end of file +console.log("Hello World"); \ No newline at end of file diff --git a/test/dir2/hello-world2.js b/test/dir2/hello-world2.js index eadeab0..c1a8daf 100644 --- a/test/dir2/hello-world2.js +++ b/test/dir2/hello-world2.js @@ -1,2 +1,2 @@ -console.log("hello world"); -console.log("hello universe"); \ No newline at end of file +console.log("Hello World"); +console.log("Hello Universe"); \ No newline at end of file diff --git a/test/test.js b/test/test.js index 94fcebc..5ce1feb 100644 --- a/test/test.js +++ b/test/test.js @@ -28,7 +28,7 @@ describe("diff", function(){ } } },function(newDoc, newScope){ - //console.log(newDoc.body); + // console.log(newDoc.body); assert.ok(newDoc.body.indexOf('
') >= 0); }); }); @@ -56,7 +56,7 @@ describe("diff", function(){ } } },function(newDoc, newScope){ - //console.log('test 2', newDoc.body); + // console.log('test 2', newDoc.body); assert.ok(newDoc.body.indexOf('
') >= 0); }); }); @@ -84,8 +84,8 @@ describe("diff", function(){ } } },function(newDoc, newScope){ - //console.log('test 3', newDoc.body); - assert.ok(newDoc.body.indexOf('
') >= 0); + // console.log('test 3', newDoc.body); + assert.ok(newDoc.body.indexOf('
') >= 0); }); });