基于 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:07:14  来源:igfitidea点击:

JavaScript based diff utility

javascriptdiff

提问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

jsdifflibinline mode comparison, try tweaking the context size to display just the desired window of change you want. There's a demo here

jsdifflib内联模式比较,尝试调整上下文大小以仅显示所需的更改窗口。有一个演示在这里

回答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

回答by imslavko

Checkout my minimal implementation: https://github.com/Slava/diff.js

查看我的最小实现:https: //github.com/Slava/diff.js

回答by realshadow

I dont know much about the diff utility in linux or linux in general, but this might be what you are looking for jsdifflib. You can find a live example there and see if it works for you.

我一般不太了解 linux 或 linux 中的 diff 实用程序,但这可能是您正在寻找的jsdifflib。你可以在那里找到一个活生生的例子,看看它是否适合你。

回答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));

回答by Justin Ethier

jsdiffliblooks promising - try the demo linked from that page.

jsdifflib看起来很有希望 - 尝试从该页面链接的演示。