Skip to content

Commit 7ef796d

Browse files
committed
Add deletedDiff function to find deletion only difference in lhs/ rhs
1 parent 71f13f6 commit 7ef796d

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

src/deleted/index.js

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

src/deleted/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 deletedDiff from './';
5+
6+
describe('.deletedDiff', () => {
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(deletedDiff(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 given values are unequal', (lhs, rhs) => {
38+
expect(deletedDiff(lhs, rhs)).to.deep.equal({});
39+
});
40+
});
41+
});
42+
43+
describe('recursive case', () => {
44+
describe('object', () => {
45+
it('returns empty object when rhs has been updated', () => {
46+
expect(deletedDiff({ a: 1 }, { a: 2 })).to.deep.equal({});
47+
});
48+
49+
it('returns empty object when right hand side has been added to', () => {
50+
expect(deletedDiff({ a: 1 }, { a: 1, b: 2 })).to.deep.equal({});
51+
});
52+
53+
it('returns keys as undefined when deleted from right hand side root', () => {
54+
expect(deletedDiff({ a: 1, b: { c: 2 }}, { a: 1 })).to.deep.equal({ b: undefined });
55+
});
56+
57+
it('returns keys as undefined when deeply deleted from right hand side', () => {
58+
expect(deletedDiff({ a: { b: 1 }, c: 2, d: { e: 100 } }, { a: { b: 1 }, c: 2, d: {} })).to.deep.equal({ d: { e: undefined } });
59+
});
60+
});
61+
62+
describe('arrays', () => {
63+
it('returns empty object when rhs array has been updated', () => {
64+
expect(deletedDiff([1], [2])).to.deep.equal({});
65+
});
66+
67+
it('returns empty object when right hand side array has additions', () => {
68+
expect(deletedDiff([1, 2, 3], [1, 2, 3, 9])).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 deletions', () => {
72+
expect(deletedDiff([1, 2, 3], [1, 3])).to.deep.equal({ 2: undefined });
73+
});
74+
});
75+
});
76+
});

0 commit comments

Comments
 (0)