diff-match-patch icon indicating copy to clipboard operation
diff-match-patch copied to clipboard

API messed up 1.0.0 -> 1.0.3

Open bebbi opened this issue 5 years ago • 3 comments

1.0.0 worked. In 1.0.3, the API is somehow messed up. The test harness does not detect it.

// that's what it should be but is not:
var a = [[DIFF_EQUAL, 'ab'], [DIFF_INSERT, '123'], [DIFF_EQUAL, 'c']]
var b = dmp.diff_main('abc', 'ab123c', false)
// Instead it's a strange object
console.log(b)
[ { '0': 0, '1': 'ab' },
  { '0': 1, '1': '123' },
  { '0': 0, '1': 'c' } ]
console.log(Array.isArray(a[0])) // true
console.log(Array.isArray(b[0])) // false
console.log(b)
assertEquivalent(a, b); // yes, equivalent

And it breaks ES6 clients:

const [eq, str] = a[0]  // works
const [eq, str] = b[0]  // breaks

bebbi avatar Sep 03 '18 17:09 bebbi

Yea, the change happened here https://github.com/JackuB/diff-match-patch/pull/13/files#diff-168726dbe96b3ce427e7fedce31bb0bcR76

It's emulating array for backward compatibility. But the destructing use case is not taken care of.

  1. We could add Symbol.iterator to the Diff object (this would require some kind of transpilation)
  2. Publish this as breaking and go for v2 (and release 1.0.4 without this change)
  3. Patch it to use array (current tests are passing)
diff_match_patch.Diff = function(op, text) {
  return [op, text];
};

Wondering if there is any reason they did that - most likely to keep the API 1:1 with rest of the implementations. Will try to look into it a bit more.

JackuB avatar Sep 03 '18 17:09 JackuB

I was going to submit an issue in google but saw yours , thanks. If google is happy with status quo, I'd propose option 2 as the one that can break in least possible ways, although it's a bit a strange api. Lets see what they say..

bebbi avatar Sep 04 '18 09:09 bebbi

Released 1.0.4 with a rollback https://github.com/JackuB/diff-match-patch/releases/tag/1.0.4 Will hold on with releasing v2 as there is a chance they will rollback the API change

JackuB avatar Sep 04 '18 10:09 JackuB