Skip to content

feature(diff): add depth option #45

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export function diff (originalObj: object, updatedObj: object): object
interface Options {
depth: number
}

export function diff (originalObj: object, updatedObj: object, options: Options): object

export function addedDiff (originalObj: object, updatedObj: object): object

Expand Down
21 changes: 14 additions & 7 deletions src/diff/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
import { isDate, isEmpty, isObject, properObject } from '../utils';
import { isDate, isEmpty, isObject, properObject, isNumber } from '../utils';

const diff = (lhs, rhs) => {
const diff = (lhs, rhs, options = {}) => {
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 deletedValues = Object.keys(l).reduce((acc, key) => {
return r.hasOwnProperty(key) ? acc : { ...acc, [key]: undefined };
}, {});

if (isDate(l) || isDate(r)) {
if (l.valueOf() == r.valueOf()) return {};
return r;
}

if (isNumber(options.depth)) {
if (options.depth === 0) {
return r
}
options.depth -= 1
}

const deletedValues = Object.keys(l).reduce((acc, key) => {
return r.hasOwnProperty(key) ? acc : { ...acc, [key]: undefined };
}, {});

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]);
const difference = diff(l[key], r[key], options);

if (isObject(difference) && isEmpty(difference) && !isDate(difference)) return acc; // return no diff

Expand Down
4 changes: 4 additions & 0 deletions src/diff/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ 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 });
});

test('returns subset of right hand side for first level only', () => {
expect(diff({ a: 1, b: { c: 1 } }, { a: 1, b: { d: 2 } }, { depth: 1 })).toEqual({ b: { d: 2 } });
});
});

describe('arrays', () => {
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,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 isNumber = o => Number(o) !== isNaN(o)