Skip to content

plugin to add properties to delta #190

@venelin-mihaylov

Description

@venelin-mihaylov

Hi all,
I want to achieve the following:
If I compare two objects with "id" property, and they have differences, in addition of the field differences to add a magic __id property to the diff.
Example:
left:

   {
      id: 1,
      name: 'Philip'
    }

right:

    {
      id: 1,
      name: 'Peter'
     }

The diff that I would get is:

{
   name: ['Philip', 'Peter']
}

I want to get:

{
   __id: 1,
   name: ['Philip', 'Peter']
}

Now, the part that I struggle with:
I want to have __id as part of the diff only if there is a difference between the two objects

I tried with the following plugin:

const diffPluginSaveId = <any> function(context) {
  // I want to add __id and __model
  if (context.leftType === 'object' &&
      typeof context.left.id !== 'undefined'
    // ... ????
  ) {
    console.log(context)
    context.setResult({
      __id: context.right.id,
      __model: context.left.constructor.name
    })
  }
}
diffPluginSaveId.filterName = 'saveId'

const differ = jdp.create(diffConfig)
  differ.processor.pipes.diff.before('collectChildren', diffPluginSaveId)

Please give me a hand :)
Thank you :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions