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

Unable to remove entries in array #1

Open
ilteoood opened this issue Aug 9, 2021 · 2 comments
Open

Unable to remove entries in array #1

ilteoood opened this issue Aug 9, 2021 · 2 comments

Comments

@ilteoood
Copy link

ilteoood commented Aug 9, 2021

Hi @rubeniskov ,
I'm trying to remove entries inside an array, using nested and test options.

You can test the issue using the following snippet:

test('should remove entries in array', (t) => {
  const initialObject = [{
    test: 1
  }, {
    test: 2
  }, {
    test: 3
  }]

  const expected = [{
    test: 2
  }]

  const actual = mutateJson(initialObject, (mutate, value) => {
    if (value.test % 2 !== 0) {
      console.log(value)
      mutate({ op: 'remove', value: {test: value} });
    }
  }, {
    nested: true,
    promises: false,
    test: ([, value]) => value.test,
  });

  t.deepEqual(actual, expected);
});

Executing this, I've the following error:

Error {
message: 'Remove operation must point to an existing value!',
}

It seems that the lastSegment is not updated on remove operation, so it continues to point to an element that doesn't exist.

@rubeniskov
Copy link
Owner

rubeniskov commented Aug 9, 2021

When remove op, the value must be undefined due is not taken by the logic of removing elements. Probably the error that should be raised would be the "incorrect use of the arguments".

mutant-json/test.js

Lines 194 to 208 in 9a7f13c

test('should remove entries with value 1', (t) => {
const expected = {
a: 0,
c: 2,
};
const actual = mutateJson(oneDepthObject, (mutate, value) => {
if (value === 1) {
mutate({ op: 'remove' });
}
});
t.deepEqual(actual, expected);
});

@ilteoood
Copy link
Author

ilteoood commented Aug 9, 2021

Even with undefined value, I have the same problem

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

2 participants