Skip to content

Commit 0307063

Browse files
Fix types for diffJson (#610)
* Fix types for diffJson * Add release notes --------- Co-authored-by: Mark Amery <[email protected]>
1 parent f87a74c commit 0307063

File tree

3 files changed

+35
-30
lines changed

3 files changed

+35
-30
lines changed

release-notes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Release Notes
22

3+
## 8.0.1
4+
5+
- [#610](https://github.com/kpdecker/jsdiff/pull/610) **Fixes types for `diffJson` which were broken by 8.0.0**. The new bundled types in 8.0.0 only allowed `diffJson` to be passed string arguments, but it should've been possible to pass either strings or objects (and now is). Thanks to Josh Kelley for the fix.
6+
37
## 8.0.0
48

59
- [#580](https://github.com/kpdecker/jsdiff/pull/580) **Multiple tweaks to `diffSentences`**:

src/diff/base.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,37 @@ interface Path {
2323

2424
export default class Diff<
2525
TokenT,
26-
ValueT extends Iterable<TokenT> = Iterable<TokenT>
26+
ValueT extends Iterable<TokenT> = Iterable<TokenT>,
27+
InputValueT = ValueT
2728
> {
2829
diff(
29-
oldStr: ValueT,
30-
newStr: ValueT,
30+
oldStr: InputValueT,
31+
newStr: InputValueT,
3132
options: DiffCallbackNonabortable<ValueT>
3233
): undefined;
3334
diff(
34-
oldStr: ValueT,
35-
newStr: ValueT,
35+
oldStr: InputValueT,
36+
newStr: InputValueT,
3637
options: AllDiffOptions & AbortableDiffOptions & CallbackOptionAbortable<ValueT>
3738
): undefined
3839
diff(
39-
oldStr: ValueT,
40-
newStr: ValueT,
40+
oldStr: InputValueT,
41+
newStr: InputValueT,
4142
options: AllDiffOptions & CallbackOptionNonabortable<ValueT>
4243
): undefined
4344
diff(
44-
oldStr: ValueT,
45-
newStr: ValueT,
45+
oldStr: InputValueT,
46+
newStr: InputValueT,
4647
options: AllDiffOptions & AbortableDiffOptions
4748
): ChangeObject<ValueT>[] | undefined
4849
diff(
49-
oldStr: ValueT,
50-
newStr: ValueT,
50+
oldStr: InputValueT,
51+
newStr: InputValueT,
5152
options?: AllDiffOptions
5253
): ChangeObject<ValueT>[]
5354
diff(
54-
oldString: ValueT,
55-
newString: ValueT,
55+
oldStr: InputValueT,
56+
newStr: InputValueT,
5657
// Type below is not accurate/complete - see above for full possibilities - but it compiles
5758
options: DiffCallbackNonabortable<ValueT> | AllDiffOptions & Partial<CallbackOptionNonabortable<ValueT>> = {}
5859
): ChangeObject<ValueT>[] | undefined {
@@ -64,8 +65,8 @@ export default class Diff<
6465
callback = options.callback;
6566
}
6667
// Allow subclasses to massage the input prior to running
67-
oldString = this.castInput(oldString, options);
68-
newString = this.castInput(newString, options);
68+
const oldString = this.castInput(oldStr, options);
69+
const newString = this.castInput(newStr, options);
6970

7071
const oldTokens = this.removeEmpty(this.tokenize(oldString, options));
7172
const newTokens = this.removeEmpty(this.tokenize(newString, options));
@@ -282,8 +283,8 @@ export default class Diff<
282283
}
283284

284285
// eslint-disable-next-line @typescript-eslint/no-unused-vars
285-
castInput(value: ValueT, options: AllDiffOptions): ValueT {
286-
return value;
286+
castInput(value: InputValueT, options: AllDiffOptions): ValueT {
287+
return value as unknown as ValueT;
287288
}
288289

289290
// eslint-disable-next-line @typescript-eslint/no-unused-vars

src/diff/json.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Diff from './base.js';
22
import type { ChangeObject, CallbackOptionAbortable, CallbackOptionNonabortable, DiffCallbackNonabortable, DiffJsonOptionsAbortable, DiffJsonOptionsNonabortable} from '../types.js';
33
import { tokenize } from './line.js';
44

5-
class JsonDiff extends Diff<string, string> {
5+
class JsonDiff extends Diff<string, string, string | object> {
66
get useLongestToken() {
77
// Discriminate between two lines of pretty-printed, serialized JSON where one of them has a
88
// dangling comma and the other doesn't. Turns out including the dangling comma yields the nicest output:
@@ -11,7 +11,7 @@ class JsonDiff extends Diff<string, string> {
1111

1212
tokenize = tokenize;
1313

14-
castInput(value: string, options: DiffJsonOptionsNonabortable | DiffJsonOptionsAbortable) {
14+
castInput(value: string | object, options: DiffJsonOptionsNonabortable | DiffJsonOptionsAbortable) {
1515
const {undefinedReplacement, stringifyReplacer = (k, v) => typeof v === 'undefined' ? undefinedReplacement : v} = options;
1616

1717
return typeof value === 'string' ? value : JSON.stringify(canonicalize(value, null, null, stringifyReplacer), null, ' ');
@@ -31,31 +31,31 @@ export const jsonDiff = new JsonDiff();
3131
* @returns a list of change objects.
3232
*/
3333
export function diffJson(
34-
oldStr: string,
35-
newStr: string,
34+
oldStr: string | object,
35+
newStr: string | object,
3636
options: DiffCallbackNonabortable<string>
3737
): undefined;
3838
export function diffJson(
39-
oldStr: string,
40-
newStr: string,
39+
oldStr: string | object,
40+
newStr: string | object,
4141
options: DiffJsonOptionsAbortable & CallbackOptionAbortable<string>
4242
): undefined
4343
export function diffJson(
44-
oldStr: string,
45-
newStr: string,
44+
oldStr: string | object,
45+
newStr: string | object,
4646
options: DiffJsonOptionsNonabortable & CallbackOptionNonabortable<string>
4747
): undefined
4848
export function diffJson(
49-
oldStr: string,
50-
newStr: string,
49+
oldStr: string | object,
50+
newStr: string | object,
5151
options: DiffJsonOptionsAbortable
5252
): ChangeObject<string>[] | undefined
5353
export function diffJson(
54-
oldStr: string,
55-
newStr: string,
54+
oldStr: string | object,
55+
newStr: string | object,
5656
options?: DiffJsonOptionsNonabortable
5757
): ChangeObject<string>[]
58-
export function diffJson(oldStr: string, newStr: string, options?: any): undefined | ChangeObject<string>[] {
58+
export function diffJson(oldStr: string | object, newStr: string | object, options?: any): undefined | ChangeObject<string>[] {
5959
return jsonDiff.diff(oldStr, newStr, options);
6060
}
6161

0 commit comments

Comments
 (0)