chbrown / rfc6902

Complete implementation of RFC6902 in TypeScript
https://chbrown.github.io/rfc6902/
326 stars 39 forks source link

Array to Object on empty path not applying patches #87

Closed labzy88 closed 2 years ago

labzy88 commented 2 years ago

Hi,

Forgive me if this is expected behaviour! I am very new to this rfc6902 and yet to learn more, however I thought it was worth mentioning...

It appears that converting an entire JSON document from an Array to an Object does not work. For example:

test('issue/79', t => {
  const old = [ { id: 1, name: 'test1' }, { id: 2, name: 'test2' } ]

  const newObj = {
    prop1: 'string',
    prop2: ['array'],
    prop3: 1
  }

  const expectedPatches: [ReplaceOperation] = [
    {
      op: 'replace',
      path: '',
      value: newObj
    }
  ];

  const patches = createPatch(old, newObj);

  t.deepEqual(patches, expectedPatches);
  const nullArray = applyPatch(old, patches);
  t.true(patches.length === nullArray.length);
  t.deepEqual(old, newObj as any);
})

The patch does not appear to be applied in this case.

Also, thanks for such a great library!

Best,

labzy88 commented 2 years ago

I realise now it's obvious changing type won't work! Sorry for the false issue, will close now! Thanks