Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep arrays as arrays #23 #68

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/diff/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isDate, isEmpty, isObject, properObject } from '../utils';

const diff = (lhs, rhs) => {
const diff = (lhs, rhs, keepArrayValue = false) => {
if (lhs === rhs) return {}; // equal return no diff

if (!isObject(lhs) || !isObject(rhs)) return rhs; // return updated rhs
Expand All @@ -18,14 +18,19 @@ const diff = (lhs, rhs) => {
}

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

const difference = diff(l[key], r[key]);
if (!l.hasOwnProperty(key)) return { ...acc, [key]: r[key] }; // return added r key

const difference = diff(l[key], r[key], keepArrayValue);
if (isObject(difference) && isEmpty(difference) && !isDate(difference)) return acc; // return no diff
if (keepArrayValue && Array.isArray(r[key])) {
return { ...acc, [key]: r[key] };
} else {
return { ...acc, [key]: difference }; // return updated key
}

return { ...acc, [key]: difference }; // return updated key
}, deletedValues);
};

export default diff;

28 changes: 25 additions & 3 deletions src/diff/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('.diff', () => {
});

test('returns subset of right hand side value when nested values differ', () => {
expect(diff({ a: { b: 1, c: 2} }, { a: { b: 1, c: 3 } })).toEqual({ a: { c: 3 } });
expect(diff({ a: { b: 1, c: 2 } }, { a: { b: 1, c: 3 } })).toEqual({ a: { c: 3 } });
});

test('returns subset of right hand side value when nested values differ at multiple paths', () => {
Expand All @@ -72,7 +72,7 @@ describe('.diff', () => {
});

test('returns keys as undefined when deleted from right hand side', () => {
expect(diff({ a: 1, b: { c: 2 }}, { a: 1 })).toEqual({ b: undefined });
expect(diff({ a: 1, b: { c: 2 } }, { a: 1 })).toEqual({ b: undefined });
});
});

Expand Down Expand Up @@ -139,7 +139,7 @@ describe('.diff', () => {

test('returns subset of right hand side value when nested values differ', () => {
const lhs = Object.create(null);
lhs.a = { b: 1, c: 2};
lhs.a = { b: 1, c: 2 };
const rhs = Object.create(null);
rhs.a = { b: 1, c: 3 };
expect(diff(lhs, rhs)).toEqual({ a: { c: 3 } });
Expand Down Expand Up @@ -172,6 +172,28 @@ describe('.diff', () => {
rhs.b = 2;
expect(diff(lhs, rhs)).toEqual({ b: 2 });
});

test('returns value of modified array', () => {
const lhs = Object.create(null);
lhs.a = [1];
const rhs = Object.create(null);
rhs.a = [1];
rhs.a.push(2)
expect(diff(lhs, rhs, true)).toEqual(rhs);
});

test('returns value of modified array in nested object', () => {
const lhs = Object.create(null);
lhs.foo = { bar: [1] };
lhs.a = 1
const rhs = Object.create(null);
rhs.a = 1
rhs.foo = { bar: [1] };
rhs.foo.bar.push(2)
const res = diff(lhs, rhs, true);
console.log(res);
expect(res).toEqual({ foo: { bar: [1, 2] } });
});
});
});
});