diff --git a/src/added/index.js b/src/added/index.js index 602d671..76d40b0 100644 --- a/src/added/index.js +++ b/src/added/index.js @@ -1,14 +1,14 @@ -import { isEmpty, isObject, properObject } from '../utils'; +import { isEmpty, isObject, hasOwnProperty } from '../utils'; const addedDiff = (lhs, rhs) => { if (lhs === rhs || !isObject(lhs) || !isObject(rhs)) return {}; - const l = properObject(lhs); - const r = properObject(rhs); + const l = lhs; + const r = rhs; return Object.keys(r).reduce((acc, key) => { - if (l.hasOwnProperty(key)) { + if (hasOwnProperty(l, key)) { const difference = addedDiff(l[key], r[key]); if (isObject(difference) && isEmpty(difference)) return acc; diff --git a/src/arrayDiff/index.js b/src/arrayDiff/index.js index 46d22d4..bbbebe4 100644 --- a/src/arrayDiff/index.js +++ b/src/arrayDiff/index.js @@ -1,15 +1,15 @@ -import { isDate, isEmpty, isObject, properObject } from '../utils'; +import { isDate, isEmpty, isObject, hasOwnProperty } from '../utils'; const diff = (lhs, rhs) => { if (lhs === rhs) return {}; // equal return no diff if (!isObject(lhs) || !isObject(rhs)) return rhs; // return updated rhs - const l = properObject(lhs); - const r = properObject(rhs); + const l = lhs; + const r = rhs; const deletedValues = Object.keys(l).reduce((acc, key) => { - return r.hasOwnProperty(key) ? acc : { ...acc, [key]: undefined }; + return hasOwnProperty(r, key) ? acc : { ...acc, [key]: undefined }; }, {}); if (isDate(l) || isDate(r)) { @@ -19,11 +19,11 @@ const diff = (lhs, rhs) => { if (Array.isArray(r) && Array.isArray(l)) { const deletedValues = l.reduce((acc, item, index) => { - return r.hasOwnProperty(index) ? acc.concat(item) : acc.concat(undefined); + return hasOwnProperty(r, index) ? acc.concat(item) : acc.concat(undefined); }, []); return r.reduce((acc, rightItem, index) => { - if (!deletedValues.hasOwnProperty(index)) { + if (!hasOwnProperty(deletedValues, index)) { return acc.concat(rightItem); } @@ -40,7 +40,7 @@ const diff = (lhs, rhs) => { } return Object.keys(r).reduce((acc, key) => { - if (!l.hasOwnProperty(key)) return { ...acc, [key]: r[key] }; // return added r key + if (!hasOwnProperty(l, key)) return { ...acc, [key]: r[key] }; // return added r key const difference = diff(l[key], r[key]); diff --git a/src/deleted/index.js b/src/deleted/index.js index 58e910a..1a238f1 100644 --- a/src/deleted/index.js +++ b/src/deleted/index.js @@ -1,13 +1,13 @@ -import { isEmpty, isObject, properObject } from '../utils'; +import { isEmpty, isObject, hasOwnProperty } from '../utils'; const deletedDiff = (lhs, rhs) => { if (lhs === rhs || !isObject(lhs) || !isObject(rhs)) return {}; - const l = properObject(lhs); - const r = properObject(rhs); + const l = lhs; + const r = rhs; return Object.keys(l).reduce((acc, key) => { - if (r.hasOwnProperty(key)) { + if (hasOwnProperty(r, key)) { const difference = deletedDiff(l[key], r[key]); if (isObject(difference) && isEmpty(difference)) return acc; diff --git a/src/diff/index.js b/src/diff/index.js index 74b961b..cbecd19 100644 --- a/src/diff/index.js +++ b/src/diff/index.js @@ -1,15 +1,15 @@ -import { isDate, isEmpty, isObject, properObject } from '../utils'; +import { isDate, isEmpty, isObject, hasOwnProperty } from '../utils'; const diff = (lhs, rhs) => { if (lhs === rhs) return {}; // equal return no diff if (!isObject(lhs) || !isObject(rhs)) return rhs; // return updated rhs - const l = properObject(lhs); - const r = properObject(rhs); + const l = lhs; + const r = rhs; const deletedValues = Object.keys(l).reduce((acc, key) => { - return r.hasOwnProperty(key) ? acc : { ...acc, [key]: undefined }; + return hasOwnProperty(r, key) ? acc : { ...acc, [key]: undefined }; }, {}); if (isDate(l) || isDate(r)) { @@ -18,7 +18,7 @@ const diff = (lhs, rhs) => { } return Object.keys(r).reduce((acc, key) => { - if (!l.hasOwnProperty(key)) return { ...acc, [key]: r[key] }; // return added r key + if (!hasOwnProperty(l, key)) return { ...acc, [key]: r[key] }; // return added r key const difference = diff(l[key], r[key]); diff --git a/src/preserveArray.js b/src/preserveArray.js index f78e783..8c817e4 100644 --- a/src/preserveArray.js +++ b/src/preserveArray.js @@ -1,4 +1,4 @@ -import { isObject } from './utils'; +import { isObject, hasOwnProperty } from './utils'; const getLargerArray = (l, r) => l.length > r.length ? l : r; @@ -16,7 +16,7 @@ const preserve = (diff, left, right) => { return { ...acc, [key]: array.reduce((acc2, item, index) => { - if (diff[key].hasOwnProperty(index)) { + if (hasOwnProperty(diff[key], index)) { acc2[index] = preserve(diff[key][index], leftArray[index], rightArray[index]); // diff recurse and check for nested arrays return acc2; } diff --git a/src/updated/index.js b/src/updated/index.js index 4b01775..c75734b 100644 --- a/src/updated/index.js +++ b/src/updated/index.js @@ -1,4 +1,4 @@ -import { isDate, isEmpty, isObject, properObject } from '../utils'; +import { isDate, isEmpty, isObject, hasOwnProperty } from '../utils'; const updatedDiff = (lhs, rhs) => { @@ -6,8 +6,8 @@ const updatedDiff = (lhs, rhs) => { if (!isObject(lhs) || !isObject(rhs)) return rhs; - const l = properObject(lhs); - const r = properObject(rhs); + const l = lhs; + const r = rhs; if (isDate(l) || isDate(r)) { if (l.valueOf() == r.valueOf()) return {}; @@ -16,7 +16,7 @@ const updatedDiff = (lhs, rhs) => { return Object.keys(r).reduce((acc, key) => { - if (l.hasOwnProperty(key)) { + if (hasOwnProperty(l, key)) { const difference = updatedDiff(l[key], r[key]); if (isObject(difference) && isEmpty(difference) && !isDate(difference)) return acc; diff --git a/src/utils/index.js b/src/utils/index.js index d8b0b8e..e7a961d 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -1,4 +1,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 properObject = o => isObject(o) && !o.hasOwnProperty ? { ...o } : o; +export const hasOwnProperty = (o, ...args) => Object.prototype.hasOwnProperty.call(o, ...args) diff --git a/src/utils/index.test.js b/src/utils/index.test.js index 800f06c..acd7d36 100644 --- a/src/utils/index.test.js +++ b/src/utils/index.test.js @@ -1,4 +1,4 @@ -import { isDate, isEmpty, isObject, properObject } from './'; +import { isDate, isEmpty, isObject } from './'; describe('utils', () => { @@ -70,27 +70,4 @@ describe('utils', () => { expect(isObject(value)).toBe(false); }); }); - - describe('.properObject', () => { - it('returns given object when object has keys and hasOwnProperty function', () => { - const o = { a: 1 }; - const a = [1]; - expect(properObject(o)).toBe(o); - expect(properObject(a)).toBe(a); - }); - - it('returns given value when value is not an object', () => { - const o = 'hello'; - expect(properObject(o)).toBe(o); - }); - - it('returns object that has given keys and hasOwnProperty function when given object is created from a null', () => { - const o = Object.create(null); - o.a = 1; - const actual = properObject(o); - expect(actual).toEqual({ a: 1 }); - expect(typeof actual.hasOwnProperty === 'function').toBe(true); - expect(actual).not.toBe(o); - }); - }); });