diff --git a/javascript/diff_match_patch.js b/javascript/diff_match_patch.js index 56b12cd8..4df161ee 100644 --- a/javascript/diff_match_patch.js +++ b/javascript/diff_match_patch.js @@ -1,24 +1,25 @@ -var diff_match_patch=function(){this.Diff_Timeout=1;this.Diff_EditCost=4;this.Match_Threshold=.5;this.Match_Distance=1E3;this.Patch_DeleteThreshold=.5;this.Patch_Margin=4;this.Match_MaxBits=32},DIFF_DELETE=-1,DIFF_INSERT=1,DIFF_EQUAL=0;diff_match_patch.Diff=function(a,b){this[0]=a;this[1]=b};diff_match_patch.Diff.prototype.length=2;diff_match_patch.Diff.prototype.toString=function(){return this[0]+","+this[1]}; +'use strict';var diff_match_patch=function(){this.Diff_Timeout=1;this.Diff_EditCost=4;this.Match_Threshold=.5;this.Match_Distance=1E3;this.Patch_DeleteThreshold=.5;this.Patch_Margin=4;this.Match_MaxBits=32},DIFF_DELETE=-1,DIFF_INSERT=1,DIFF_EQUAL=0;diff_match_patch.Diff=function(a,b){this[0]=a;this[1]=b};diff_match_patch.Diff.prototype.length=2;diff_match_patch.Diff.prototype.toString=function(){return this[0]+","+this[1]}; +diff_match_patch.Diff.prototype[Symbol.iterator]=function*(){yield this[0];yield this[1]}; diff_match_patch.prototype.diff_main=function(a,b,d,c){"undefined"==typeof c&&(c=0>=this.Diff_Timeout?Number.MAX_VALUE:(new Date).getTime()+1E3*this.Diff_Timeout);if(null==a||null==b)throw Error("Null input. (diff_main)");if(a==b)return a?[new diff_match_patch.Diff(DIFF_EQUAL,a)]:[];"undefined"==typeof d&&(d=!0);var e=d,f=this.diff_commonPrefix(a,b);d=a.substring(0,f);a=a.substring(f);b=b.substring(f);f=this.diff_commonSuffix(a,b);var g=a.substring(a.length-f);a=a.substring(0,a.length-f);b=b.substring(0, b.length-f);a=this.diff_compute_(a,b,e,c);d&&a.unshift(new diff_match_patch.Diff(DIFF_EQUAL,d));g&&a.push(new diff_match_patch.Diff(DIFF_EQUAL,g));this.diff_cleanupMerge(a);return a}; diff_match_patch.prototype.diff_compute_=function(a,b,d,c){if(!a)return[new diff_match_patch.Diff(DIFF_INSERT,b)];if(!b)return[new diff_match_patch.Diff(DIFF_DELETE,a)];var e=a.length>b.length?a:b,f=a.length>b.length?b:a,g=e.indexOf(f);return-1!=g?(d=[new diff_match_patch.Diff(DIFF_INSERT,e.substring(0,g)),new diff_match_patch.Diff(DIFF_EQUAL,f),new diff_match_patch.Diff(DIFF_INSERT,e.substring(g+f.length))],a.length>b.length&&(d[0][0]=d[2][0]=DIFF_DELETE),d):1==f.length?[new diff_match_patch.Diff(DIFF_DELETE, a),new diff_match_patch.Diff(DIFF_INSERT,b)]:(e=this.diff_halfMatch_(a,b))?(b=e[1],f=e[3],a=e[4],e=this.diff_main(e[0],e[2],d,c),d=this.diff_main(b,f,d,c),e.concat([new diff_match_patch.Diff(DIFF_EQUAL,a)],d)):d&&100d);u++){for(var v=-u+n;v<=u-w;v+=2){var q=f+v;var t=v==-u||v!=u&&h[q-1]c)w+=2;else if(y>e)n+=2;else if(m&&(q=f+k-v,0<=q&&q= -x)return this.diff_bisectSplit_(a,b,t,y,d)}}for(v=-u+p;v<=u-r;v+=2){q=f+v;x=v==-u||v!=u&&l[q-1]c)r+=2;else if(t>e)p+=2;else if(!m&&(q=f+k-v,0<=q&&q=x)))return this.diff_bisectSplit_(a,b,t,y,d)}}return[new diff_match_patch.Diff(DIFF_DELETE,a),new diff_match_patch.Diff(DIFF_INSERT,b)]}; +diff_match_patch.prototype.diff_bisect_=function(a,b,d){for(var c=a.length,e=b.length,f=Math.ceil((c+e)/2),g=2*f,h=Array(g),l=Array(g),k=0;kd);u++){for(var t=-u+n;t<=u-w;t+=2){var p=f+t;var r=t==-u||t!=u&&h[p-1]c)w+=2;else if(y>e)n+=2;else if(m&&(p=f+k-t,0<=p&&p= +x)return this.diff_bisectSplit_(a,b,r,y,d)}}for(t=-u+v;t<=u-q;t+=2){p=f+t;x=t==-u||t!=u&&l[p-1]c)q+=2;else if(r>e)v+=2;else if(!m&&(p=f+k-t,0<=p&&p=x)))return this.diff_bisectSplit_(a,b,r,y,d)}}return[new diff_match_patch.Diff(DIFF_DELETE,a),new diff_match_patch.Diff(DIFF_INSERT,b)]}; diff_match_patch.prototype.diff_bisectSplit_=function(a,b,d,c,e){var f=a.substring(0,d),g=b.substring(0,c);a=a.substring(d);b=b.substring(c);f=this.diff_main(f,g,!1,e);e=this.diff_main(a,b,!1,e);return f.concat(e)}; -diff_match_patch.prototype.diff_linesToChars_=function(a,b){function d(l){for(var k="",m=0,n=-1,w=c.length;nc?a=a.substring(d-c):d=k.length?[u,v,q,t,r]:null}if(0>=this.Diff_Timeout)return null; -var c=a.length>b.length?a:b,e=a.length>b.length?b:a;if(4>c.length||2*e.lengthc[4].length?g:c:c:g;else return null;if(a.length>b.length){c=g[0];e=g[1];var h=g[2];var l=g[3]}else h=g[0],l=g[1],c=g[2],e=g[3];return[c,e,h,l,g[4]]}; +diff_match_patch.prototype.diff_halfMatch_=function(a,b){function d(h,l,k){for(var m=h.substring(k,k+Math.floor(h.length/4)),n=-1,w="",v,q,u,t;-1!=(n=l.indexOf(m,n+1));){var p=f.diff_commonPrefix(h.substring(k),l.substring(n)),r=f.diff_commonSuffix(h.substring(0,k),l.substring(0,n));w.length=h.length?[v,q,u,t,w]:null}if(0>=this.Diff_Timeout)return null; +var c=a.length>b.length?a:b,e=a.length>b.length?b:a;if(4>c.length||2*e.lengthc[4].length?g:c:c:g;else return null;a.length>b.length?(a=g[0],b=g[1],c=g[2],e=g[3]):(c=g[0],e=g[1],a=g[2],b=g[3]);return[a,b,c,e,g[4]]}; diff_match_patch.prototype.diff_cleanupSemantic=function(a){for(var b=!1,d=[],c=0,e=null,f=0,g=0,h=0,l=0,k=0;f=e){if(c>=b.length/2||c>=d.length/2)a.splice(f,0,new diff_match_patch.Diff(DIFF_EQUAL,d.substring(0,c))),a[f-1][1]=b.substring(0,b.length-c),a[f+1][1]=d.substring(c),f++}else if(e>=b.length/2||e>=d.length/2)a.splice(f,0,new diff_match_patch.Diff(DIFF_EQUAL,b.substring(0,e))),a[f-1][0]=DIFF_INSERT,a[f-1][1]=d.substring(0,d.length-e),a[f+1][0]=DIFF_DELETE, a[f+1][1]=b.substring(e),f++;f++}f++}}; -diff_match_patch.prototype.diff_cleanupSemanticLossless=function(a){function b(n,w){if(!n||!w)return 6;var p=n.charAt(n.length-1),r=w.charAt(0),u=p.match(diff_match_patch.nonAlphaNumericRegex_),v=r.match(diff_match_patch.nonAlphaNumericRegex_),q=u&&p.match(diff_match_patch.whitespaceRegex_),t=v&&r.match(diff_match_patch.whitespaceRegex_);p=q&&p.match(diff_match_patch.linebreakRegex_);r=t&&r.match(diff_match_patch.linebreakRegex_);var y=p&&n.match(diff_match_patch.blanklineEndRegex_),x=r&&w.match(diff_match_patch.blanklineStartRegex_); -return y||x?5:p||r?4:u&&!q&&t?3:q||t?2:u||v?1:0}for(var d=1;d=k&&(k=m,g=c,h=e,l=f)}a[d-1][1]!=g&&(g?a[d-1][1]=g:(a.splice(d- +diff_match_patch.prototype.diff_cleanupSemanticLossless=function(a){function b(n,w){if(!n||!w)return 6;var v=n.charAt(n.length-1),q=w.charAt(0),u=v.match(diff_match_patch.nonAlphaNumericRegex_),t=q.match(diff_match_patch.nonAlphaNumericRegex_),p=u&&v.match(diff_match_patch.whitespaceRegex_),r=t&&q.match(diff_match_patch.whitespaceRegex_);v=p&&v.match(diff_match_patch.linebreakRegex_);q=r&&q.match(diff_match_patch.linebreakRegex_);n=v&&n.match(diff_match_patch.blanklineEndRegex_);w=q&&w.match(diff_match_patch.blanklineStartRegex_); +return n||w?5:v||q?4:u&&!p&&r?3:p||r?2:u||t?1:0}for(var d=1;d=k&&(k=m,g=c,h=e,l=f)}a[d-1][1]!=g&&(g?a[d-1][1]=g:(a.splice(d- 1,1),d--),a[d][1]=h,l?a[d+1][1]=l:(a.splice(d+1,1),d--))}d++}};diff_match_patch.nonAlphaNumericRegex_=/[^a-zA-Z0-9]/;diff_match_patch.whitespaceRegex_=/\s/;diff_match_patch.linebreakRegex_=/[\r\n]/;diff_match_patch.blanklineEndRegex_=/\n\r?\n$/;diff_match_patch.blanklineStartRegex_=/^\r?\n\r?\n/; diff_match_patch.prototype.diff_cleanupEfficiency=function(a){for(var b=!1,d=[],c=0,e=null,f=0,g=!1,h=!1,l=!1,k=!1;fthis.Match_MaxBits)throw Error("Pattern too long for this browser.");var e=this.match_alphabet_(b),f=this,g=this.Match_Threshold,h=a.indexOf(b,d);-1!=h&&(g=Math.min(c(0,h),g),h=a.lastIndexOf(b,d+b.length),-1!=h&&(g=Math.min(c(0,h),g)));var l=1<=k;r--){var u=e[a.charAt(r-1)];m[r]=0===p?(m[r+1]<<1|1)&u:(m[r+1]<<1|1)&u|(w[r+1]|w[r])<<1|1|w[r+1];if(m[r]&l&&(u=c(p,r-1),u<=g))if(g=u,h=r-1,h>d)k=Math.max(1,2*d-h);else break}if(c(p+1,d)>g)break;w=m}return h}; +diff_match_patch.prototype.diff_fromDelta=function(a,b){var d=[],c=0,e=0;b=b.split(/\t/g);for(var f=0;fthis.Match_MaxBits)throw Error("Pattern too long for this browser.");var e=this.match_alphabet_(b),f=this,g=this.Match_Threshold,h=a.indexOf(b,d);-1!=h&&(g=Math.min(c(0,h),g),h=a.lastIndexOf(b,d+b.length),-1!=h&&(g=Math.min(c(0,h),g)));var l=1<=k;q--){var u=e[a.charAt(q-1)];m[q]=0===v?(m[q+1]<<1|1)&u:(m[q+1]<<1|1)&u|(w[q+1]|w[q])<<1|1|w[q+1];if(m[q]&l&&(u=c(v,q-1),u<=g))if(g=u,h=q-1,h>d)k=Math.max(1,2*d-h);else break}if(c(v+1,d)>g)break;w=m}return h}; diff_match_patch.prototype.match_alphabet_=function(a){for(var b={},d=0;d=2*this.Patch_Margin&&e&&(this.patch_addContext_(a,h),d.push(a),a=new diff_match_patch.patch_obj,e=0,h=c,f=g)}k!==DIFF_INSERT&&(f+=m.length);k!==DIFF_DELETE&&(g+=m.length)}e&&(this.patch_addContext_(a,h),d.push(a));return d}; diff --git a/javascript/diff_match_patch_uncompressed.js b/javascript/diff_match_patch_uncompressed.js index a9b41d66..a5110979 100644 --- a/javascript/diff_match_patch_uncompressed.js +++ b/javascript/diff_match_patch_uncompressed.js @@ -88,6 +88,21 @@ diff_match_patch.Diff.prototype.toString = function() { return this[0] + ',' + this[1]; }; +/** + * Provide an iterable instance on a Diff object. + * + * While it's not intended that the Diff be iterated, + * this instance is useful for destructuring assignment. + * + * Example: + * + * const diff = new diff_match_patch.Diff(DIFF_EQUAL, 'hello'); + * const [op, text] = diff; + */ +diff_match_patch.Diff.prototype[Symbol.iterator] = function*() { + yield this[0]; + yield this[1]; +} /** * Find the differences between two texts. Simplifies the problem by stripping diff --git a/javascript/tests/diff_match_patch_test.html b/javascript/tests/diff_match_patch_test.html index 46617309..14233e09 100644 --- a/javascript/tests/diff_match_patch_test.html +++ b/javascript/tests/diff_match_patch_test.html @@ -88,6 +88,7 @@ } var tests = [ + 'testDiffIsDestructurable', 'testDiffCommonPrefix', 'testDiffCommonSuffix', 'testDiffCommonOverlap', diff --git a/javascript/tests/diff_match_patch_test.js b/javascript/tests/diff_match_patch_test.js index 99aaca13..4f8cb93a 100644 --- a/javascript/tests/diff_match_patch_test.js +++ b/javascript/tests/diff_match_patch_test.js @@ -76,6 +76,17 @@ function diff_rebuildtexts(diffs) { var dmp = new diff_match_patch(); +// Diff object TEST FUNCTIONS + +function testDiffIsDestructurable() { + const diff = new diff_match_patch.Diff(dmp.DIFF_EQUAL, 'six one way half a dozen another'); + const [op, text] = diff; + + assertEquals(diff[0], op); + assertEquals(diff[1], text); +} + + // DIFF TEST FUNCTIONS