Skip to content

Commit

Permalink
Merge pull request #156 from ewnd9/patch-1
Browse files Browse the repository at this point in the history
Add `undefinedReplacement` option to `diffJson`
  • Loading branch information
kpdecker authored Dec 22, 2016
2 parents 0974127 + d234b04 commit 2f92dae
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/diff/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@ jsonDiff.useLongestToken = true;

jsonDiff.tokenize = lineDiff.tokenize;
jsonDiff.castInput = function(value) {
return typeof value === 'string' ? value : JSON.stringify(canonicalize(value), undefined, ' ');
const {undefinedReplacement} = this.options;

return typeof value === 'string' ? value : JSON.stringify(canonicalize(value), function(k, v) {
if (typeof v === 'undefined') {
return undefinedReplacement;
}

return v;
}, ' ');
};
jsonDiff.equals = function(left, right) {
return Diff.prototype.equals(left.replace(/,([\r\n])/g, '$1'), right.replace(/,([\r\n])/g, '$1'));
};

export function diffJson(oldObj, newObj, callback) { return jsonDiff.diff(oldObj, newObj, callback); }

export function diffJson(oldObj, newObj, options) { return jsonDiff.diff(oldObj, newObj, options); }

// This function handles the presence of circular references by bailing out when encountering an
// object that is already on the "stack" of items being processed.
Expand Down
9 changes: 9 additions & 0 deletions test/diff/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ describe('diff/json', function() {
)).to.eql([
{ count: 4, value: '{\n "a": 123,\n "b": 456\n}' }
]);
expect(diffJson(
{a: 123, b: 456, c: undefined},
{a: 123, b: 456},
{undefinedReplacement: null}
)).to.eql([
{ count: 3, value: '{\n "a": 123,\n "b": 456,\n' },
{ count: 1, value: ' "c": null\n', added: undefined, removed: true },
{ count: 1, value: '}' }
]);
});

it('should accept already stringified JSON', function() {
Expand Down

0 comments on commit 2f92dae

Please sign in to comment.