mattphillips / deep-object-diff

Deep diffs two objects, including nested structures of arrays and objects, and returns the difference. ❄️
https://www.npmjs.com/package/deep-object-diff
MIT License
1.05k stars 89 forks source link

updatedDiff() not suitable for nested array? #84

Closed lucianobosco closed 2 years ago

lucianobosco commented 2 years ago

Having this original object:

originalResource = {
  id: null,
  name: null,
  tags: [],
  coordinates: null,
  transforms: null
}

And the one with the updated property tags


resource = {
  id: null,
  name: null,
  tags: ['lorem', 'ipsum'],
  coordinates: null,
  transforms: null
}

The result of doing

updatedDiff(originalResource.value, resource.value)
// {}

Am I missing something? I just wanted to use only updatedDiff() method to make things easier. Any advice will be appreciated

BTW, it would be interesting to have a method just for listing all properties that have been touched. I'm aware about detailedDiff(), but in a plain way with no details could be useful for knowing what properties are "dirty"

mattphillips commented 2 years ago

Just looking at the example you've given, it looks like you're invoking updatedDiff with .value on both resources but the value key doesn't exist (so would be undefined) which would yield an empty diff ({}). Can you remove the .value and see if that fixes it?

The touched method is interesting, I suppose my only question would be how would you represent deeply nested diffs on properties?

lucianobosco commented 2 years ago

My bad, programmer burnout over here. I'm using Vue 3 composition API and defining consts as reactive, therefore originalResource and resource values should be accessed by using .value

const originalResource = ref({
  id: null,
  name: null,
  tags: [],
  coordinates: null,
  transforms: null
})

const resource = ref({
  id: null,
  name: null,
  tags: ['lorem', 'ipsum'],
  coordinates: null,
  transforms: null
})

console.log( updatedDiff(originalResource.value, resource.value) )

Anyway, I think that using diff() method will be more than enough for what I'm looking for: know the dirty object properties.

Thanks for this library!, I think I will use it in all my projects from now . It's really helpful for knowing what properties are touched and should be sent to the API instead of the whole object in a PATCH request.

mattphillips commented 2 years ago

Ha no worries it happens to the best of us, am glad it was a quick one to fix 😆👍