Skip to content

Commit 2c36f81

Browse files
Fix async mode return value quirk (#602)
* Add test showing bug * Fix bug demonstrated by the test * Add release notes
1 parent c376585 commit 2c36f81

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

release-notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- [#601](https://github.com/kpdecker/jsdiff/pull/601) - **`diffJson`'s `stringifyReplacer` option behaves more like `JSON.stringify`'s `replacer` argument now.** In particular:
1414
* Each key/value pair now gets passed through the replacer once instead of twice
1515
* The `key` passed to the replacer when the top-level object is passed in as `value` is now `""` (previously, was `undefined`), and the `key` passed with an array element is the array index as a string, like `"0"` or `"1"` (previously was whatever the key for the entire array was). Both the new behaviours match that of `JSON.stringify`.
16+
- [#602](https://github.com/kpdecker/jsdiff/pull/602) - **diffing functions now consistently return `undefined` when called in async mode** (i.e. with a callback). Previously, there was an odd quirk where they would return `true` if the strings being diffed were equal and `undefined` otherwise.
1617

1718
## 7.0.0
1819

src/diff/base.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Diff.prototype = {
1414
value = self.postProcess(value, options);
1515
if (callback) {
1616
setTimeout(function() { callback(value); }, 0);
17-
return true;
17+
return undefined;
1818
} else {
1919
return value;
2020
}
@@ -106,7 +106,7 @@ Diff.prototype = {
106106

107107
if (basePath.oldPos + 1 >= oldLen && newPos + 1 >= newLen) {
108108
// If we have hit the end of both strings, then we are done
109-
return done(buildValues(self, basePath.lastComponent, newString, oldString, self.useLongestToken));
109+
return done(buildValues(self, basePath.lastComponent, newString, oldString, self.useLongestToken)) || true;
110110
} else {
111111
bestPath[diagonalPath] = basePath;
112112
if (basePath.oldPos + 1 >= oldLen) {

test/diff/character.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,10 @@ describe('diff/character', function() {
5959
diffChars('whatever', 'whatever', {ignoreCase: true});
6060
diffChars('whatever', 'whatever', {ignoreCase: true, callback: () => {}});
6161
});
62+
63+
it('should return undefined when called in async mode', function() {
64+
expect(diffChars('whatever', 'whatever', {callback: () => {}})).to.be.undefined;
65+
expect(diffChars('whatever', 'whatever else', {callback: () => {}})).to.be.undefined;
66+
});
6267
});
6368
});

0 commit comments

Comments
 (0)