Created
August 26, 2018 18:46
-
-
Save alsotang/9683b7673df7da58ce3315d18b4376b1 to your computer and use it in GitHub Desktop.
diff two text with enhanced inline diff
This file contains hidden or 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
require('colors'); | |
const jsdiff = require('diff') | |
const stringSimilarity = require('string-similarity'); | |
const text1 = ` | |
{ | |
"name": "calcit-editor", | |
"version": "0.3.50", | |
"description": "Cirru Calcit Editor", | |
"bin": { | |
"calcit-editor": "dist/server.js", | |
"ce": "dist/server.js" | |
}, | |
"scripts": { | |
"watch": "shadow-cljs watch client server", | |
"serve": "http-server dist -s", | |
"repl": "rlwrap shadow-cljs clj-repl", | |
"build": "shadow-cljs clj-run build.main/build", | |
"build-local": "shadow-cljs clj-run build.main/build-local", | |
"upload": "lumo -c cli/ -m build.upload" | |
}, | |
"repository": { | |
"type": "git", | |
"url": "[email protected]:Cirru/calcit-editor.git" | |
}, | |
"author": "jiyinyiyong", | |
"license": "MIT", | |
"devDependencies": { | |
"http-server": "^0.11.1", | |
"shadow-cljs": "^2.6.2", | |
"source-map-support": "^0.5.9" | |
}, | |
"dependencies": { | |
"chalk": "^2.4.1", | |
"dayjs": "^1.7.5", | |
"express": "^4.16.3", | |
"gaze": "^1.1.3", | |
"md5": "^2.2.1", | |
"serve-index": "^1.9.1", | |
"shortid": "^2.2.13", | |
"ws": "^6.0.0" | |
} | |
} | |
` | |
const text2 = ` | |
{ | |
"name": "calcit-editor", | |
"version": "0.3.51", | |
"description": "Great Cirru Calcit Editor", | |
"bin": { | |
"calcit-editor": "dist/server.js", | |
"ce": "dist/server.js" | |
}, | |
"scripts": { | |
"watch": "shadow-cljs watch client server", | |
"serve": "http-server dist -s", | |
"build": "shadow-cljs clj-run build.main/build", | |
"page": "shadow-cljs clj-run build.main/page", | |
"build-local": "shadow-cljs clj-run build.main/build-local", | |
"upload": "lumo -c cli/ -m build.upload" | |
}, | |
"repository": { | |
"type": "git", | |
"url": "[email protected]:Cirru/calcit-editor.git" | |
}, | |
"author": "jiyinyiyong", | |
"license": "MIT", | |
"devDependencies": { | |
"express": "^0.11.1", | |
"shadow-cljs": "^2.6.2", | |
"source-map-support": "^0.5.9" | |
}, | |
"dependencies": { | |
"chalk": "^2.4.1", | |
"dayjs": "^1.7.5", | |
"express": "^4.16.3", | |
"gaze": "^1.1.3", | |
"md256": "^2.2.1", | |
"serve-index": "^1.9.1", | |
"shortid": "^2.2.13", | |
"ws": "^6.0.0" | |
} | |
} | |
` | |
const diffResult = jsdiff.diffLines(text1, text2) | |
console.log(diffResult) | |
const noNewlineLog = process.stderr.write.bind(process.stderr) | |
let added = '' | |
let removed = '' | |
const originalCompareResult = [] | |
const compareResult = [] | |
diffResult.forEach(function (part) { | |
// 保存原始的行行对比结果 | |
var color = part.added ? 'green' : | |
part.removed ? 'red' : 'grey'; | |
originalCompareResult.push(part.value[color]); | |
// 开始优化版本的逻辑 | |
if (part.added === void 0 && part.removed === void 0) { | |
if (removed && added) { | |
const similarity = stringSimilarity.compareTwoStrings(removed, added); | |
// 判断相似性 | |
if (similarity < 0.8) { | |
// 如果不相似就照常 | |
compareResult.push(removed.red) | |
compareResult.push(added.green) | |
} else { | |
// 相似的话比较行内差异 | |
const charsDiff = jsdiff.diffChars(removed, added); | |
const inlineCompareResult1 = [] | |
const inlineCompareResult2 = [] | |
charsDiff.forEach(function(part){ | |
if (part.added === void 0 && part.removed === void 0) { | |
inlineCompareResult1.push(part.value.red); | |
inlineCompareResult2.push(part.value.green); | |
} else if (part.removed) { | |
inlineCompareResult1.push(part.value.bgRed) | |
} else if (part.added) { | |
inlineCompareResult2.push(part.value.bgGreen) | |
} | |
// var color = part.added ? 'green' : | |
// part.removed ? 'red' : 'grey'; | |
// inlineCompareResult.push(part.value[color]); | |
}); | |
compareResult.push(inlineCompareResult1.join('')) | |
compareResult.push(inlineCompareResult2.join('')) | |
} | |
} else { | |
removed && compareResult.push(removed.red) | |
added && compareResult.push(added.green) | |
} | |
compareResult.push(part.value.grey) | |
removed = '' | |
added = '' | |
} | |
if (part.removed) { | |
removed = part.value | |
} | |
if (part.added) { | |
added = part.value | |
} | |
}) | |
console.log('----------') | |
noNewlineLog(originalCompareResult.join('')) | |
console.log('----------') | |
noNewlineLog(compareResult.join('')) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment