Skip to content

Commit cfafa10

Browse files
Rewrite json.js -> json.ts and get tests passing (I'm honestly shocked they passed)
1 parent 5aaa633 commit cfafa10

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

src/diff/base.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export interface DiffOptions<ValueT> {
1919

2020
// diffChars or diffWords only:
2121
ignoreCase?: boolean,
22+
23+
// diffJson only:
24+
undefinedReplacement?: any,
25+
stringifyReplacer?: (k: string, v: any) => any,
2226
}
2327

2428
/**
@@ -66,7 +70,7 @@ export default class Diff<
6670
}
6771

6872
private diffWithOptionsObj(oldTokens: TokenT[], newTokens: TokenT[], options: DiffOptions<TokenT>, callback: DiffCallback<TokenT>) {
69-
let self = this;
73+
const self = this;
7074
function done(value) {
7175
value = self.postProcess(value, options);
7276
if (callback) {
@@ -156,7 +160,7 @@ export default class Diff<
156160

157161
if (basePath.oldPos + 1 >= oldLen && newPos + 1 >= newLen) {
158162
// If we have hit the end of both strings, then we are done
159-
return done(this.buildValues(basePath.lastComponent, newTokens, oldTokens));
163+
return done(self.buildValues(basePath.lastComponent, newTokens, oldTokens));
160164
} else {
161165
bestPath[diagonalPath] = basePath;
162166
if (basePath.oldPos + 1 >= oldLen) {

src/diff/json.js renamed to src/diff/json.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
1-
import Diff from './base';
1+
import Diff, {DiffOptions} from './base';
22
import {lineDiff} from './line';
33

4-
export const jsonDiff = new Diff();
5-
// Discriminate between two lines of pretty-printed, serialized JSON where one of them has a
6-
// dangling comma and the other doesn't. Turns out including the dangling comma yields the nicest output:
7-
// TODO: Override getter
8-
jsonDiff.useLongestToken = true;
4+
class JsonDiff extends Diff<string, string> {
5+
protected get useLongestToken() {
6+
// Discriminate between two lines of pretty-printed, serialized JSON where one of them has a
7+
// dangling comma and the other doesn't. Turns out including the dangling comma yields the nicest output:
8+
return true;
9+
}
10+
11+
protected tokenize = lineDiff.tokenize
12+
13+
protected castInput(value: string, options: DiffOptions<string>) {
14+
const {undefinedReplacement, stringifyReplacer = (k, v) => typeof v === 'undefined' ? undefinedReplacement : v} = options;
15+
16+
return typeof value === 'string' ? value : JSON.stringify(canonicalize(value, null, null, stringifyReplacer), stringifyReplacer, ' ');
17+
};
18+
19+
protected equals(left: string, right: string, options: DiffOptions<string>) {
20+
return super.equals.call(jsonDiff, left.replace(/,([\r\n])/g, '$1'), right.replace(/,([\r\n])/g, '$1'), options);
21+
};
22+
}
923

10-
jsonDiff.tokenize = lineDiff.tokenize;
11-
jsonDiff.castInput = function(value, options) {
12-
const {undefinedReplacement, stringifyReplacer = (k, v) => typeof v === 'undefined' ? undefinedReplacement : v} = options;
24+
const jsonDiff = new JsonDiff();
1325

14-
return typeof value === 'string' ? value : JSON.stringify(canonicalize(value, null, null, stringifyReplacer), stringifyReplacer, ' ');
15-
};
16-
jsonDiff.equals = function(left, right, options) {
17-
return Diff.prototype.equals.call(jsonDiff, left.replace(/,([\r\n])/g, '$1'), right.replace(/,([\r\n])/g, '$1'), options);
18-
};
1926

20-
export function diffJson(oldObj, newObj, options) { return jsonDiff.diff(oldObj, newObj, options); }
27+
export function diffJson(oldObj: any, newObj: any, options: DiffOptions<string>) { return jsonDiff.diff(oldObj, newObj, options); }
2128

2229
// This function handles the presence of circular references by bailing out when encountering an
2330
// object that is already on the "stack" of items being processed. Accepts an optional replacer
24-
export function canonicalize(obj, stack, replacementStack, replacer, key) {
31+
export function canonicalize(obj: any, stack: Array<any> | null, replacementStack: Array<any> | null, replacer: (string, any) => any, key?: string) {
2532
stack = stack || [];
2633
replacementStack = replacementStack || [];
2734

0 commit comments

Comments
 (0)