Skip to content

Commit

Permalink
Merge branch 'master' into no-prototype-builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
mattphillips authored Jan 25, 2022
2 parents e310b60 + ff1235e commit cc08b6e
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules/
dist
coverage
npm-debug.log
yarn-error.log
6 changes: 4 additions & 2 deletions src/diff/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isDate, isEmpty, isObject, hasOwnProperty } from '../utils';
import { isDate, isEmptyObject, isObject, hasOwnProperty } from '../utils';

const diff = (lhs, rhs) => {
if (lhs === rhs) return {}; // equal return no diff
Expand All @@ -22,7 +22,9 @@ const diff = (lhs, rhs) => {

const difference = diff(l[key], r[key]);

if (isObject(difference) && isEmpty(difference) && !isDate(difference)) return acc; // return no diff
// If the difference is empty, and the lhs is an empty object or the rhs is not an empty object
if (isEmptyObject(difference) && !isDate(difference) && (isEmptyObject(l[key]) || !isEmptyObject(r[key])))
return acc; // return no diff

return { ...acc, [key]: difference }; // return updated key
}, deletedValues);
Expand Down
7 changes: 7 additions & 0 deletions src/diff/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ describe('.diff', () => {

describe('recursive case', () => {
describe('object', () => {
test("return right hand side empty object value when left hand side has been updated", () => {
expect(diff({ a: 1 }, { a: {} })).toEqual({ a: {} });
});

test('returns right hand side value when given objects are different', () => {
expect(diff({ a: 1 }, { a: 2 })).toEqual({ a: 2 });
});
Expand Down Expand Up @@ -77,6 +81,9 @@ describe('.diff', () => {
});

describe('arrays', () => {
test("return right hand side empty object value when left hand side has been updated", () => {
expect(diff([{ a: 1 }], [{ a: {} }])).toEqual({ 0: { a: {} } });
});
test('returns right hand side value as object of indices to value when arrays are different', () => {
expect(diff([1], [2])).toEqual({ 0: 2 });
});
Expand Down
8 changes: 4 additions & 4 deletions src/updated/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { isDate, isEmpty, isObject, hasOwnProperty } from '../utils';
import { isDate, isEmptyObject, isObject, hasOwnProperty } from '../utils';

const updatedDiff = (lhs, rhs) => {

if (lhs === rhs) return {};

if (!isObject(lhs) || !isObject(rhs)) return rhs;
Expand All @@ -15,11 +14,12 @@ const updatedDiff = (lhs, rhs) => {
}

return Object.keys(r).reduce((acc, key) => {

if (hasOwnProperty(l, key)) {
const difference = updatedDiff(l[key], r[key]);

if (isObject(difference) && isEmpty(difference) && !isDate(difference)) return acc;
// If the difference is empty, and the lhs is an empty object or the rhs is not an empty object
if (isEmptyObject(difference) && !isDate(difference) && (isEmptyObject(l[key]) || !isEmptyObject(r[key])))
return acc; // return no diff

return { ...acc, [key]: difference };
}
Expand Down
8 changes: 8 additions & 0 deletions src/updated/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ describe('.updatedDiff', () => {

describe('recursive case', () => {
describe('object', () => {
test("return right hand side empty object value when left hand side has been updated", () => {
expect(updatedDiff({ a: 1 }, { a: {} })).toEqual({ a: {} });
});

test('returns right hand side value when given objects are different at root', () => {
expect(updatedDiff({ a: 1 }, { a: 2 })).toEqual({ a: 2 });
});
Expand Down Expand Up @@ -77,6 +81,10 @@ describe('.updatedDiff', () => {
});

describe('arrays', () => {
test("return right hand side empty object value when left hand side has been updated", () => {
expect(updatedDiff([{ a: 1 }], [{ a: {} }])).toEqual({ 0: { a: {} } });
});

test('returns right hand side value as object of indices to value when arrays are different', () => {
expect(updatedDiff([1], [2])).toEqual({ 0: 2 });
});
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export const isDate = d => d instanceof Date;
export const isEmpty = o => Object.keys(o).length === 0;
export const isObject = o => o != null && typeof o === 'object';
export const hasOwnProperty = (o, ...args) => Object.prototype.hasOwnProperty.call(o, ...args)
export const isEmptyObject = (o) => isObject(o) && isEmpty(o);

0 comments on commit cc08b6e

Please sign in to comment.