Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json-support #162

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ enum DiffMethod {
TRIMMED_LINES = 'diffTrimmedLines',
SENTENCES = 'diffSentences',
CSS = 'diffCss',
JSON = 'diffJson',
}
```

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-diff-viewer",
"version": "3.1.1",
"version": "3.2.0",
"private": false,
"description": "A simple and beautiful text diff viewer component made with diff and React",
"keywords": [
Expand Down Expand Up @@ -31,14 +31,14 @@
"dependencies": {
"classnames": "^2.2.6",
"create-emotion": "^10.0.14",
"diff": "^4.0.1",
"diff": "^5.1.0",
"emotion": "^10.0.14",
"memoize-one": "^5.0.4",
"prop-types": "^15.6.2"
},
"devDependencies": {
"@types/classnames": "^2.2.6",
"@types/diff": "^4.0.2",
"@types/diff": "^5.0.2",
"@types/enzyme": "^3.1.14",
"@types/enzyme-adapter-react-16": "^1.0.3",
"@types/expect": "^1.20.3",
Expand Down
36 changes: 24 additions & 12 deletions src/compute-lines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export enum DiffType {
REMOVED = 2,
}

// See https://github.com/kpdecker/jsdiff/tree/v4.0.1#api for more info on the below JsDiff methods
// See https://github.com/kpdecker/jsdiff/tree/v5.0.0#api for more info on the below JsDiff methods
export enum DiffMethod {
CHARS = 'diffChars',
WORDS = 'diffWords',
Expand All @@ -17,6 +17,7 @@ export enum DiffMethod {
TRIMMED_LINES = 'diffTrimmedLines',
SENTENCES = 'diffSentences',
CSS = 'diffCss',
JSON = 'diffJson',
}

export interface DiffInformation {
Expand Down Expand Up @@ -79,6 +80,25 @@ const constructLines = (value: string): string[] => {
return lines;
};

function computeLineDiff(
oldString: string,
newString: string,
compareMethod: string,
): JsDiffChangeObject[] {
try {
if (compareMethod === DiffMethod.JSON) {
return diff.diffJson(JSON.parse(oldString), JSON.parse(newString));
}
} catch (e) {
// noop
}
return diff.diffLines(oldString.trimRight(), newString.trimRight(), {
newlineIsToken: true,
ignoreWhitespace: false,
ignoreCase: false,
});
}

/**
* Computes word diff information in the line.
* [TODO]: Consider adding options argument for JsDiff text block comparison
Expand Down Expand Up @@ -142,19 +162,11 @@ const computeDiff = (
const computeLineInformation = (
oldString: string,
newString: string,
disableWordDiff: boolean = false,
disableWordDiff = false,
compareMethod: string = DiffMethod.CHARS,
linesOffset: number = 0,
linesOffset = 0,
): ComputedLineInformation => {
const diffArray = diff.diffLines(
oldString.trimRight(),
newString.trimRight(),
{
newlineIsToken: true,
ignoreWhitespace: false,
ignoreCase: false,
},
);
const diffArray = computeLineDiff(oldString, newString, compareMethod);
let rightLineNumber = linesOffset;
let leftLineNumber = linesOffset;
let lineInformation: LineInformation[] = [];
Expand Down
13 changes: 9 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@
resolved "https://registry.yarnpkg.com/@types/classnames/-/classnames-2.2.9.tgz#d868b6febb02666330410fe7f58f3c4b8258be7b"
integrity sha512-MNl+rT5UmZeilaPxAVs6YaPC2m6aA8rofviZbhbxpPpl61uKodfdQVsBtgJGTqGizEf02oW3tsVe7FYB8kK14A==

"@types/diff@^4.0.2":
version "4.0.2"
resolved "https://registry.yarnpkg.com/@types/diff/-/diff-4.0.2.tgz#2e9bb89f9acc3ab0108f0f3dc4dbdcf2fff8a99c"
integrity sha512-mIenTfsIe586/yzsyfql69KRnA75S8SVXQbTLpDejRrjH0QSJcpu3AUOi/Vjnt9IOsXKxPhJfGpQUNMueIU1fQ==
"@types/diff@^5.0.2":
version "5.0.2"
resolved "https://registry.yarnpkg.com/@types/diff/-/diff-5.0.2.tgz#dd565e0086ccf8bc6522c6ebafd8a3125c91c12b"
integrity sha512-uw8eYMIReOwstQ0QKF0sICefSy8cNO/v7gOTiIy9SbwuHyEecJUm7qlgueOO5S1udZ5I/irVydHVwMchgzbKTg==

"@types/enzyme-adapter-react-16@^1.0.3":
version "1.0.5"
Expand Down Expand Up @@ -1937,6 +1937,11 @@ diff@^4.0.1:
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.1.tgz#0c667cb467ebbb5cea7f14f135cc2dba7780a8ff"
integrity sha512-s2+XdvhPCOF01LRQBC8hf4vhbVmI2CGS5aZnxLJlT5FtdhPCDFq80q++zK2KlrVorVDdL5BOGZ/VfLrVtYNF+Q==

diff@^5.1.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/diff/-/diff-5.1.0.tgz#bc52d298c5ea8df9194800224445ed43ffc87e40"
integrity sha512-D+mk+qE8VC/PAUrlAU34N+VfXev0ghe5ywmpqrawphmVZc1bEfn56uo9qpyGp1p4xpzOHkSW4ztBd6L7Xx4ACw==

diffie-hellman@^5.0.0:
version "5.0.3"
resolved "https://registry.yarnpkg.com/diffie-hellman/-/diffie-hellman-5.0.3.tgz#40e8ee98f55a2149607146921c63e1ae5f3d2875"
Expand Down