Skip to content

Commit

Permalink
fix(diff): handle undefined|null vs object values;
Browse files Browse the repository at this point in the history
- Closes #94
  • Loading branch information
lukeed committed Jun 4, 2021
1 parent 63be84b commit 0acb895
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/diff.js
Expand Up @@ -195,13 +195,20 @@ export function compare(input, expect) {
if (Array.isArray(expect)) return arrays(input, expect);
if (expect instanceof RegExp) return chars(''+input, ''+expect);

let isA = input && typeof input == 'object';
let isB = expect && typeof expect == 'object';

if (isA && isB) input = sort(input, expect);
if (isB) expect = stringify(expect);
if (isA) input = stringify(input);

if (expect && typeof expect == 'object') {
input = stringify(sort(input, expect));
expect = stringify(expect);
}

let isA = typeof input == 'string';
let isB = typeof expect == 'string';
isA = typeof input == 'string';
isB = typeof expect == 'string';

if (isA && /\r?\n/.test(input)) return lines(input, ''+expect);
if (isB && /\r?\n/.test(expect)) return lines(''+input, expect);
Expand Down
44 changes: 44 additions & 0 deletions test/diff.js
Expand Up @@ -923,6 +923,50 @@ compare('should handle multi-line string against non-type mismatch', () => {
);
});

compare('should handle `null` vs object', () => {
assert.snapshot(
strip($.compare(null, { foo: 123 })),
'Actual:\n' +
'--null\n' +
'Expected:\n' +
'++{\n' +
'++··"foo":·123\n' +
'++}\n'
);

assert.snapshot(
strip($.compare({ foo: 123 }, null)),
'Actual:\n' +
'--{\n' +
'--··"foo":·123\n' +
'--}\n' +
'Expected:\n' +
'++null\n'
);
});

compare('should handle `undefined` vs object', () => {
assert.snapshot(
strip($.compare(undefined, { foo: 123 })),
'Actual:\n' +
'--undefined\n' +
'Expected:\n' +
'++{\n' +
'++··"foo":·123\n' +
'++}\n'
);

assert.snapshot(
strip($.compare({ foo: 123 }, undefined)),
'Actual:\n' +
'--{\n' +
'--··"foo":·123\n' +
'--}\n' +
'Expected:\n' +
'++undefined\n'
);
});

compare.run();

// ---
Expand Down

0 comments on commit 0acb895

Please sign in to comment.