字符串或数组的两种状态的差异

Diff of two states of a string or array

对于字符串和数组,我有一个 "old" 状态和一个 "new" 状态(经过一些修改)。

我需要经常向服务器发送更改(使用 AJAX/XMLHttpRequest),如果可能的话,以一种有效的方式(如果只有一个元素,请不要重新发送 200 KB 的数据)该数组已 changed/has 被移动 deleted/has)。示例:

var oldstate1 = 'hello how are you? very good and you? thanks for asking! this text will be removed.';
var newstate1 = 'hello how are you? very good and you? new text here. thanks for asking!';

var oldstate2 = [[1732, "item1"], [1732, "will be deleted"], [23, "will be moved"], [23, "hello"]];
var newstate2 = [[23, "will be moved"], [1732, "item1"], [23, "hello"], [126, "new item"]];

当然,我可以用 deleteinsertmoveupdate 等事件手动编写客户端和服务器之间的协议,然后发送这些事件带有 AJAX,服务器将相应地更新其数据库。但是要正确地做到这一点非常乏味。

问题:有没有更聪明的方法来编码只在 oldstatenewstate 之间的变化 Javascript 的大字符串或数组? 以一种可以在后端轻松解码的方式,运行 Python.

类似于字符串或数组的 diff/patch 算法,在 JS(客户端)和 Python(后端)之间理解。

注:

如果没有第三方工具,这可能是可能的(如果是这样,请随时 post 另一个答案)。

经过进一步研究,我发现了这个项目:diff-match-patch and particularly this example

Originally built in 2006 to power Google Docs, this library is now available in C++, C#, Dart, Java, JavaScript, Lua, Objective C, and Python.

var oldstate1 = "hello how are you? very good and you? blablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablabla. thanks for asking! this text will be removed.";
var newstate1 = "hello how are you? very good and you? blablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablabla.new text here. thanks for asking!";
var dmp = new diff_match_patch();
var diff = dmp.diff_main(oldstate1, newstate1);
dmp.diff_cleanupSemantic(diff);
console.log(dmp.diff_toDelta(diff))    
// =138 +new text here. =19 -27
<script src="https://cdnjs.cloudflare.com/ajax/libs/diff_match_patch/20121119/diff_match_patch.js"></script>

这个 delta + oldstate1 然后可以在服务器端使用 Python 来重建 newstate1:

import diff_match_patch as dmp_module
dmp = dmp_module.diff_match_patch()

oldstate1 = "hello how are you? very good and you? blablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablabla. thanks for asking! this text will be removed."
delta = "=138\t+new text here.\t=19\t-27"
patch = dmp.patch_make(oldstate1, dmp.diff_fromDelta(oldstate1, delta))
newstate1 = dmp.patch_apply(patch, oldstate1)[0]
print(newstate1)

另见 this issue