Skip to content

Fix async mode return value quirk #602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions release-notes.md
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
- [#601](https://github.com/kpdecker/jsdiff/pull/601) - **`diffJson`'s `stringifyReplacer` option behaves more like `JSON.stringify`'s `replacer` argument now.** In particular:
* Each key/value pair now gets passed through the replacer once instead of twice
* 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`.
- [#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.

## 7.0.0

4 changes: 2 additions & 2 deletions src/diff/base.js
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ Diff.prototype = {
value = self.postProcess(value, options);
if (callback) {
setTimeout(function() { callback(value); }, 0);
return true;
return undefined;
} else {
return value;
}
@@ -106,7 +106,7 @@ Diff.prototype = {

if (basePath.oldPos + 1 >= oldLen && newPos + 1 >= newLen) {
// If we have hit the end of both strings, then we are done
return done(buildValues(self, basePath.lastComponent, newString, oldString, self.useLongestToken));
return done(buildValues(self, basePath.lastComponent, newString, oldString, self.useLongestToken)) || true;
} else {
bestPath[diagonalPath] = basePath;
if (basePath.oldPos + 1 >= oldLen) {
5 changes: 5 additions & 0 deletions test/diff/character.js
Original file line number Diff line number Diff line change
@@ -59,5 +59,10 @@ describe('diff/character', function() {
diffChars('whatever', 'whatever', {ignoreCase: true});
diffChars('whatever', 'whatever', {ignoreCase: true, callback: () => {}});
});

it('should return undefined when called in async mode', function() {
expect(diffChars('whatever', 'whatever', {callback: () => {}})).to.be.undefined;
expect(diffChars('whatever', 'whatever else', {callback: () => {}})).to.be.undefined;
});
});
});