cujojs / jiff

JSON Patch and diff based on rfc6902
Other
627 stars 41 forks source link

Wrong Output when something is added at front and deleted in between #44

Open siddharth232 opened 1 year ago

siddharth232 commented 1 year ago
const oldValue = [
    {
      Details: [{
        Amount: 100,
        CurrencyCode: 'USD',
        Quantity: 2,
        RegisteredPrice: 20,
        id: 10,
      }],
      Type: 'Variable',
      Product: 'NaN',
      id: 10,
    },
    {
      Details: [{
        Amount: 110,
        CurrencyCode: 'USD',
        Quantity: 16,
        RegisteredPrice: 20,
        id: 20,
      }],
      Type: 'Type-11',
      Product: 'Product-11',
      id: 20,
    },
    {
      Details: [{
        Amount: 120,
        CurrencyCode: 'USD',
        Quantity: 17,
        RegisteredPrice: 20,
        id: 30,
      }],
      Type: 'Type-12',
      Product: 'Product-12',
      id: 30,
    },
  ];
  const newValue = [
    {
      Details: [{
        Amount: 100,
        CurrencyCode: 'USD',
        Quantity: 2,
        RegisteredPrice: 20,
        id: 40,
      }],
      Type: 'Variable',
      Product: 'NaN',
      id: 40,
    },
    {
      Details: [{
        Amount: 100,
        CurrencyCode: 'USD',
        Quantity: 2,
        RegisteredPrice: 20,
        id: 10,
      }],
      Type: 'Variable',
      Product: 'NaN',
      id: 10,
    },
    {
      Details: [{
        Amount: 120,
        CurrencyCode: 'USD',
        Quantity: 17,
        RegisteredPrice: 20,
        id: 30,
      }],
      Type: 'Type-12',
      Product: 'Product-12',
      id: 30,
    },
  ];
function hashFn(x) {
    return x.id;
}
console.log(jiff.diff(oldValue,newValue,{hash: hashFn,invertible:false}));

Output: [ { op: 'add', path: '/0', value: { Details: [Array], Type: 'Variable', Product: 'NaN', id: 40 }, context: undefined }, { op: 'remove', path: '/2', context: undefined } ]

Expected: [ { op: 'add', path: '/0', value: { Details: [Array], Type: 'Variable', Product: 'NaN', id: 40 }, context: undefined }, { op: 'remove', path: '/1', context: undefined } ]