Skip to content

Commit 71f13f6

Browse files
committed
Add addedDiff function to find addition only difference in lhs/ rhs
1 parent 4c5a3e0 commit 71f13f6

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

src/added/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { isEmpty, isObject } from '../utils';
2+
3+
const addedDiff = (lhs, rhs) => {
4+
5+
if (lhs === rhs || !isObject(lhs) || !isObject(rhs)) return {};
6+
7+
return Object.keys(rhs).reduce((acc, key) => {
8+
if (lhs.hasOwnProperty(key)) {
9+
const difference = addedDiff(lhs[key], rhs[key]);
10+
11+
if (isObject(difference) && isEmpty(difference)) return acc;
12+
13+
return { ...acc, [key]: difference };
14+
}
15+
16+
return { ...acc, [key]: rhs[key] };
17+
}, {});
18+
};
19+
20+
export default addedDiff;

src/added/index.spec.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { expect } from 'chai';
2+
import forEach from 'mocha-each';
3+
4+
import addedDiff from './';
5+
6+
describe('.addedDiff', () => {
7+
8+
describe('base case', () => {
9+
describe('equal', () => {
10+
forEach([
11+
['int', 1],
12+
['string', 'a'],
13+
['boolean', true],
14+
['null', null],
15+
['undefined', undefined],
16+
['object', { a: 1 }],
17+
['array', [1]],
18+
['function', () => ({})],
19+
]).it('returns empty object when given values of type %s are equal', (type, value) => {
20+
expect(addedDiff(value, value)).to.deep.equal({});
21+
});
22+
});
23+
24+
describe('not equal and not object', () => {
25+
forEach([
26+
[1, 2],
27+
['a', 'b'],
28+
[true, false],
29+
['hello', null],
30+
['hello', undefined],
31+
[null, undefined],
32+
[undefined, null],
33+
[null, { a: 1 }],
34+
['872983', { areaCode: '+44', number: '872983' }],
35+
[100, () => ({})],
36+
[() => ({}), 100],
37+
]).it('returns empty object when values are not equal (%s, %s)', (lhs, rhs) => {
38+
expect(addedDiff(lhs, rhs)).to.deep.equal({});
39+
});
40+
});
41+
});
42+
43+
describe('recursive case', () => {
44+
describe('object', () => {
45+
it('returns empty object when given objects are updated', () => {
46+
expect(addedDiff({ a: 1 }, { a: 2 })).to.deep.equal({});
47+
});
48+
49+
it('returns empty object when right hand side has deletion', () => {
50+
expect(addedDiff({ a: 1, b: 2 }, { a: 1 })).to.deep.equal({});
51+
});
52+
53+
it('returns subset of right hand side value when a key value has been added to the root', () => {
54+
expect(addedDiff({ a: 1 }, { a: 1, b: 2 })).to.deep.equal({ b: 2 });
55+
});
56+
57+
it('returns subset of right hand side value when a key value has been added deeply', () => {
58+
expect(addedDiff({ a: { b: 1} }, { a: { b: 1, c: 2 } })).to.deep.equal({ a: { c: 2 } });
59+
});
60+
});
61+
62+
describe('arrays', () => {
63+
it('returns empty object when array is updated', () => {
64+
expect(addedDiff([1], [2])).to.deep.equal({});
65+
});
66+
67+
it('returns empty object when right hand side array has deletions', () => {
68+
expect(addedDiff([1, 2, 3], [1, 3])).to.deep.equal({});
69+
});
70+
71+
it('returns subset of right hand side array as object of indices to value when right hand side array has additions', () => {
72+
expect(addedDiff([1, 2, 3], [1, 2, 3, 9])).to.deep.equal({ 3: 9 });
73+
});
74+
});
75+
});
76+
});

0 commit comments

Comments
 (0)