Skip to content
Merged
Show file tree
Hide file tree
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
7 changes: 2 additions & 5 deletions src/diff/json.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import Diff from './base';
import {lineDiff} from './line';

const objectPrototypeToString = Object.prototype.toString;


export const jsonDiff = new Diff();
// Discriminate between two lines of pretty-printed, serialized JSON where one of them has a
// dangling comma and the other doesn't. Turns out including the dangling comma yields the nicest output:
Expand Down Expand Up @@ -41,7 +38,7 @@ export function canonicalize(obj, stack, replacementStack, replacer, key) {

let canonicalizedObj;

if ('[object Array]' === objectPrototypeToString.call(obj)) {
if ('[object Array]' === Object.prototype.toString.call(obj)) {
stack.push(obj);
canonicalizedObj = new Array(obj.length);
replacementStack.push(canonicalizedObj);
Expand All @@ -65,7 +62,7 @@ export function canonicalize(obj, stack, replacementStack, replacer, key) {
key;
for (key in obj) {
/* istanbul ignore else */
if (obj.hasOwnProperty(key)) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
sortedKeys.push(key);
}
}
Expand Down
16 changes: 16 additions & 0 deletions test/diff/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,22 @@ describe('diff/json', function() {
{ count: 1, value: '}', removed: false, added: false }
]);
});

it("doesn't throw on Object.create(null)", function() {
let diff;
expect(function() {
diff = diffJson(
Object.assign(Object.create(null), {a: 123}),
{b: 456}
);
}).not.to['throw']();
expect(diff).to.eql([
{ count: 1, value: '{\n', removed: false, added: false },
{ count: 1, value: ' \"a\": 123\n', removed: true, added: false },
{ count: 1, value: ' \"b\": 456\n', removed: false, added: true },
{ count: 1, value: '}', removed: false, added: false }
]);
});
});
});

Expand Down