google / diff-match-patch

Diff Match Patch is a high-performance library in multiple languages that manipulates plain text.
Apache License 2.0
7.25k stars 1.09k forks source link

JavaScript: Diff is not iterable #39

Open JackuB opened 5 years ago

JackuB commented 5 years ago

There was a change in JS version https://github.com/google/diff-match-patch/commit/cd60d246aecf871367313c7cf6dc8814af32c5e3#diff-5270d640a6c9c1b0590326b029d71ec8R76 from plain Array to a diff_match_patch.Diff Object that's trying to emulate Array.

The new object is not iterable, which messes up for example with Array destructing:

const a = dmp.diff_main('abc', 'ab123c', false);
const [eq, str] = a[0]; // => Uncaught TypeError: a[0] is not iterable
  1. was this change necessary? Tested that plain array works just fine with current version
  2. To really emulate array here, adding [Symbol.iterator] would do the trick, but its browser support is questionable
bebbi commented 5 years ago

@NeilFraser A quick comment on whether the new JS API behaviour which breaks Array destructuring is intentional or whether it's a bug and will be fixed would be very helpful - thanks!

GrosSacASac commented 5 years ago

I think the API behaviour change is not intentional Here is a fix that could help: After

/**
 * Emulate the output of a two-element array.
 * @return {string} Diff operation as a string.
 */
diff_match_patch.Diff.prototype.toString = function() {
  return this[0] + ',' + this[1];
};

add the following


if (typeof Symbol === 'function') {
    diff_match_patch.Diff.prototype[Symbol.iterator] = function* () {
      yield this[0];
      yield this[1];
    };
}

Another possible is to cast to a real Array:

const [eq, str] = Array.from(a[0]);

dmsnell commented 2 months ago

I've incorporated a Symbol.iterator method on Diff in my fork: https://github.com/dmsnell/diff-match-patch