From e78ac40dd2834ef9559122b061539218512072b0 Mon Sep 17 00:00:00 2001 From: Mark Hetherington Date: Tue, 12 Jul 2022 13:27:41 -0400 Subject: [PATCH 1/5] JSON support --- package.json | 4 ++-- src/compute-lines.ts | 36 ++++++++++++++++++++++++------------ 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 47c5a873..8ce7d8e9 100644 --- a/package.json +++ b/package.json @@ -31,14 +31,12 @@ "dependencies": { "classnames": "^2.2.6", "create-emotion": "^10.0.14", - "diff": "^4.0.1", "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/enzyme": "^3.1.14", "@types/enzyme-adapter-react-16": "^1.0.3", "@types/expect": "^1.20.3", @@ -85,4 +83,6 @@ "engines": { "node": ">= 8" } + "diff": "^5.1.0", + "@types/diff": "^5.0.2", } diff --git a/src/compute-lines.ts b/src/compute-lines.ts index 53eb93ca..62cc863e 100644 --- a/src/compute-lines.ts +++ b/src/compute-lines.ts @@ -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', @@ -17,6 +17,7 @@ export enum DiffMethod { TRIMMED_LINES = 'diffTrimmedLines', SENTENCES = 'diffSentences', CSS = 'diffCss', + JSON = 'diffJson', } export interface DiffInformation { @@ -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 @@ -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[] = []; From 5e6c3420687d69a16a5c17f8deeba2ffcf60ec42 Mon Sep 17 00:00:00 2001 From: Mark Hetherington Date: Tue, 12 Jul 2022 13:32:53 -0400 Subject: [PATCH 2/5] Update Diff Library --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 8ce7d8e9..256a1a25 100644 --- a/package.json +++ b/package.json @@ -31,12 +31,14 @@ "dependencies": { "classnames": "^2.2.6", "create-emotion": "^10.0.14", + "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": "^5.0.2", "@types/enzyme": "^3.1.14", "@types/enzyme-adapter-react-16": "^1.0.3", "@types/expect": "^1.20.3", @@ -83,6 +85,4 @@ "engines": { "node": ">= 8" } - "diff": "^5.1.0", - "@types/diff": "^5.0.2", } From a81976b24c8136a123364be4ca0cdffd4e29642c Mon Sep 17 00:00:00 2001 From: Mark Hetherington Date: Tue, 12 Jul 2022 13:46:32 -0400 Subject: [PATCH 3/5] Update readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6be4b73e..daf74b89 100644 --- a/README.md +++ b/README.md @@ -161,6 +161,7 @@ enum DiffMethod { TRIMMED_LINES = 'diffTrimmedLines', SENTENCES = 'diffSentences', CSS = 'diffCss', + JSON = 'diffJson', } ``` From d99f8d8f1800efc52e5d2044fdece93205c77a17 Mon Sep 17 00:00:00 2001 From: Mark Hetherington Date: Tue, 12 Jul 2022 13:51:24 -0400 Subject: [PATCH 4/5] Update yarn.lock --- yarn.lock | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/yarn.lock b/yarn.lock index 38e6fb9d..ec5acced 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" From 6155ccf1e56cb36af9969b253e534a3b2ea8ff84 Mon Sep 17 00:00:00 2001 From: Mark Hetherington Date: Tue, 12 Jul 2022 14:44:53 -0400 Subject: [PATCH 5/5] up version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 256a1a25..eee7c9fc 100644 --- a/package.json +++ b/package.json @@ -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": [