基于 JavaScript 的差异实用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3053587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript based diff utility
提问by poke
I'm looking for a diffequivalent written in JavaScript that only returns/prints relevant lines. I don't want both full text displayed next to each other with the differences highlighted, but just want the actual differences (plus some buffer lines to know where the difference is), similar to the output from the linux diff utility.
我正在寻找用 JavaScript 编写的仅返回/打印相关行的diff等效项。我不希望将两个全文显示在一起并突出显示差异,而只希望实际差异(加上一些缓冲区行可以知道差异在哪里),类似于 linux diff 实用程序的输出。
Does anybody know a javascript function that does this? All differences should be recognized (even changed whitespace). Thanks.
有人知道这样做的javascript函数吗?应识别所有差异(甚至更改空格)。谢谢。
采纳答案by Scott Bale
回答by austincheney
I completely rebuilt the jsdifflib utility for speed. Since my version does not require DOM access it at least 4.5 times faster, and it is also extended to highlight character differences in each line.
我完全重建了 jsdifflib 实用程序以提高速度。由于我的版本不需要 DOM 访问它至少快了 4.5 倍,而且它还被扩展以突出显示每一行中的字符差异。
http://prettydiff.com/diffview.js
http://prettydiff.com/diffview.js
You can test this out with the online tool directly at http://prettydiff.com/
您可以直接在http://prettydiff.com/ 上使用在线工具对此进行测试
回答by Gian Marco Gherardi
There's also google-diff-match-patchfrom Google
还有来自 Google 的google-diff-match-patch
Also available on NPM
也可在NPM 上使用
npm install diff-match-patch
回答by Daniel F
- Mergelyis totally worth checking out. CodeMirror-based, client-side only.
- Then there's the CodeMirror demowhich requires server-side diff computation.
- jsdiffliband prettydiffas mentioned in other answers.
- Mergely完全值得一试。基于 CodeMirror,仅限客户端。
- 然后是需要服务器端差异计算的CodeMirror 演示。
- jsdifflib和prettydiff如其他答案中所述。
回答by imslavko
Checkout my minimal implementation: https://github.com/Slava/diff.js
查看我的最小实现:https: //github.com/Slava/diff.js
回答by realshadow
回答by Tim
Check out CodeMirror. Nuff said.
查看CodeMirror。纳夫说。
回答by Cacycle
Check out the the wikEd diffJavaScript library. There is also an online tool.
查看wikEd diffJavaScript 库。还有一个在线工具。
wikEd diff features inline text comparisons with block move highlighting and character/word-based resolution. It is optimized for Wikipedia source text, but works great for any type of text or code. The library code is fully customizable, has Unicode support, is extensively commented, and is in the public domain.
wikEd diff 具有带块移动突出显示和基于字符/单词的分辨率的内联文本比较。它针对维基百科源文本进行了优化,但适用于任何类型的文本或代码。库代码是完全可定制的,具有 Unicode 支持,被广泛评论,并且在公共领域。
回答by aqm
old question i know, but if your doing node work or want something that is compatible with requirejs/commonjs module
老问题我知道,但如果你做节点工作或想要与 requirejs/commonjs 模块兼容的东西
I've really liked https://www.npmjs.com/package/diff
我真的很喜欢https://www.npmjs.com/package/diff
console.log(diff.createPatch('some file name.txt', expected, actual));

