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

visitorCallbackFn is not called for diffs in the first level #16

Open
gilisho opened this issue Nov 29, 2020 · 0 comments
Open

visitorCallbackFn is not called for diffs in the first level #16

gilisho opened this issue Nov 29, 2020 · 0 comments

Comments

@gilisho
Copy link

gilisho commented Nov 29, 2020

I'm having an issue regarding visitorCallbackFn. Seems like it's not called if the diff is on the first level of the jsons...
I suspect it's because of this line. Any reason for this check? I'd change this code to be something like this:

if (i !== length - 1) {
      val = val[key];
      if (visitorCallback) {
        visitorCallback(val);
      }
    } else {
      val[key] = value;
      if (i === length -1 && length === 1 && visitorCallback) {
        visitorCallback(val);
      }
    }

In my use case I would like to call the visitor in the first level, which happens to be also the last...
I'll demonstrate the issue by a failing test:
In jsonDiff.js:

import { applyDiff, getDiff } from 'recursive-diff';

export const getNewObjWithDiff = (originalObj, newObj) => {
  const callback = (ob) => {
    if (ob instanceof Object) {
      ob.isVisited = true;
    }
  };
  const delta = getDiff(originalObj, newObj);
  return applyDiff({ ...originalObj, ...newObj }, delta, callback);
};

In jsonDiff.spec.js:

import { getNewObjWithDiff } from './jsonDiff';

const json = { '/hello': {} };
const newJson = {
  '/hello': { leaf: true },
  '/drafts': {
    leaf: true,
  },
};

describe('jsonDiff', () => {
  it('should return new json with diff inside', () => {
    expect(getNewObjWithDiff(json, newJson)).toEqual({
      '/hello': {
        leaf: true,
        isVisited: true,
      },
      '/drafts': {
        leaf: true,
        isVisited: true,
      },
    });
  });
});

This currently fails with the following returned output (missing isVisited in "/drafts"):

{
  "/drafts": {
    "leaf": true
  },
  "/hello": {
    "isVisited": true,
    "leaf": true
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant