Skip to content

Commit

Permalink
Step #4 ver.2
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexAven committed Jul 30, 2024
1 parent 13cc9ce commit 3502faf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
7 changes: 0 additions & 7 deletions bin/gendiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,7 @@ program
const file1Parsed = fileParse(file1Content);
const file2Parsed = fileParse(file2Content);

// console.log(absolutePath1);
// console.log(absolutePath2);
// console.log(file1Content);
// console.log(file2Content);
// console.log(file1Parsed);
// console.log(file2Parsed);
console.log(genDiff(file1Parsed, file2Parsed));
// return genDiff(file1Parsed, file2Parsed);
});

program.parse(process.argv);
19 changes: 10 additions & 9 deletions src/genDiff.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import _ from 'lodash';

const genDiff = (file1, file2) => {
const fileDifferences = {};
const file1KeysList = Object.keys(file1);
const file2KeysList = Object.keys(file2);
const summaryKeysList = _.sortBy(_.union(file1KeysList, file2KeysList));

summaryKeysList.forEach((key) => {
const fileDifferences = summaryKeysList.reduce((acc, key) => {
if (key in file1 && key in file2) {
if (file1[key] === file2[key]) {
fileDifferences[` ${key}`] = file1[key];
acc.push(` ${key}: ${file1[key]}`);
} else {
fileDifferences[`- ${key}`] = file1[key];
fileDifferences[`+ ${key}`] = file2[key];
acc.push(` - ${key}: ${file1[key]}`);
acc.push(` + ${key}: ${file2[key]}`);
}
} else if (key in file1) {
fileDifferences[`- ${key}`] = file1[key];
acc.push(` - ${key}: ${file1[key]}`);
} else if (key in file2) {
fileDifferences[`+ ${key}`] = file2[key];
acc.push(` + ${key}: ${file2[key]}`);
}
});

return fileDifferences;
return acc;
}, []);

return `{\n${fileDifferences.join('\n')}\n}`;
};

export default genDiff;

0 comments on commit 3502faf

Please sign in to comment.