From f55b858811f10b329b3ed0532f100813c11c8281 Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Sat, 12 Nov 2022 10:19:10 +0000 Subject: [PATCH] Remove unnecessary reassignment of `lhs`/`rhs` --- src/added.js | 10 ++++------ src/deleted.js | 9 +++------ src/diff.js | 23 ++++++++++------------- src/updated.js | 17 +++++++---------- 4 files changed, 24 insertions(+), 35 deletions(-) diff --git a/src/added.js b/src/added.js index 00e17fd..2064360 100644 --- a/src/added.js +++ b/src/added.js @@ -4,12 +4,10 @@ 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; @@ -17,7 +15,7 @@ const addedDiff = (lhs, rhs) => { return acc; } - acc[key] = r[key]; + acc[key] = rhs[key]; return acc; }, makeObjectWithoutPrototype()); }; diff --git a/src/deleted.js b/src/deleted.js index f720123..a27211e 100644 --- a/src/deleted.js +++ b/src/deleted.js @@ -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; diff --git a/src/diff.js b/src/diff.js index bcb6f3d..e0c8569 100644 --- a/src/diff.js +++ b/src/diff.js @@ -5,11 +5,8 @@ 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; } @@ -17,21 +14,21 @@ const diff = (lhs, rhs) => { 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 diff --git a/src/updated.js b/src/updated.js index 579d9e4..f6942da 100644 --- a/src/updated.js +++ b/src/updated.js @@ -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;