-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
10 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |