Skip to content

Commit

Permalink
Remove unnecessary reassignment of lhs/rhs
Browse files Browse the repository at this point in the history
  • Loading branch information
mattphillips committed Nov 12, 2022
1 parent 04c06f8 commit f55b858
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 35 deletions.
10 changes: 4 additions & 6 deletions src/added.js
Expand Up @@ -4,20 +4,18 @@ const addedDiff = (lhs, rhs) => {

if (lhs === rhs || !isObject(lhs) || !isObject(rhs)) return {};

const l = lhs;
const r = rhs;

return Object.keys(r).reduce((acc, key) => {
if (hasOwnProperty(l, key)) {
const difference = addedDiff(l[key], r[key]);
return Object.keys(rhs).reduce((acc, key) => {
if (hasOwnProperty(lhs, key)) {
const difference = addedDiff(lhs[key], rhs[key]);

if (isObject(difference) && isEmpty(difference)) return acc;

acc[key] = difference;
return acc;
}

acc[key] = r[key];
acc[key] = rhs[key];
return acc;
}, makeObjectWithoutPrototype());
};
Expand Down
9 changes: 3 additions & 6 deletions src/deleted.js
Expand Up @@ -3,12 +3,9 @@ import { isEmpty, isObject, hasOwnProperty, makeObjectWithoutPrototype } from '.
const deletedDiff = (lhs, rhs) => {
if (lhs === rhs || !isObject(lhs) || !isObject(rhs)) return {};

const l = lhs;
const r = rhs;

return Object.keys(l).reduce((acc, key) => {
if (hasOwnProperty(r, key)) {
const difference = deletedDiff(l[key], r[key]);
return Object.keys(lhs).reduce((acc, key) => {
if (hasOwnProperty(rhs, key)) {
const difference = deletedDiff(lhs[key], rhs[key]);

if (isObject(difference) && isEmpty(difference)) return acc;

Expand Down
23 changes: 10 additions & 13 deletions src/diff.js
Expand Up @@ -5,33 +5,30 @@ const diff = (lhs, rhs) => {

if (!isObject(lhs) || !isObject(rhs)) return rhs; // return updated rhs

const l = lhs;
const r = rhs;

const deletedValues = Object.keys(l).reduce((acc, key) => {
if (!hasOwnProperty(r, key)) {
const deletedValues = Object.keys(lhs).reduce((acc, key) => {
if (!hasOwnProperty(rhs, key)) {
acc[key] = undefined;

}

return acc;
}, makeObjectWithoutPrototype());

if (isDate(l) || isDate(r)) {
if (l.valueOf() == r.valueOf()) return {};
return r;
if (isDate(lhs) || isDate(rhs)) {
if (lhs.valueOf() == rhs.valueOf()) return {};
return rhs;
}

return Object.keys(r).reduce((acc, key) => {
if (!hasOwnProperty(l, key)){
acc[key] = r[key]; // return added r key
return Object.keys(rhs).reduce((acc, key) => {
if (!hasOwnProperty(lhs, key)){
acc[key] = rhs[key]; // return added r key
return acc;
}

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

// 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])))
if (isEmptyObject(difference) && !isDate(difference) && (isEmptyObject(lhs[key]) || !isEmptyObject(rhs[key])))
return acc; // return no diff

acc[key] = difference // return updated key
Expand Down
17 changes: 7 additions & 10 deletions src/updated.js
Expand Up @@ -5,20 +5,17 @@ const updatedDiff = (lhs, rhs) => {

if (!isObject(lhs) || !isObject(rhs)) return rhs;

const l = lhs;
const r = rhs;

if (isDate(l) || isDate(r)) {
if (l.valueOf() == r.valueOf()) return {};
return r;
if (isDate(lhs) || isDate(rhs)) {
if (lhs.valueOf() == rhs.valueOf()) return {};
return rhs;
}

return Object.keys(r).reduce((acc, key) => {
if (hasOwnProperty(l, key)) {
const difference = updatedDiff(l[key], r[key]);
return Object.keys(rhs).reduce((acc, key) => {
if (hasOwnProperty(lhs, key)) {
const difference = updatedDiff(lhs[key], rhs[key]);

// 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])))
if (isEmptyObject(difference) && !isDate(difference) && (isEmptyObject(lhs[key]) || !isEmptyObject(rhs[key])))
return acc; // return no diff

acc[key] = difference;
Expand Down

0 comments on commit f55b858

Please sign in to comment.