crud-compare icon indicating copy to clipboard operation
crud-compare copied to clipboard

feature request: return indexes

Open reslear opened this issue 3 years ago • 8 comments
trafficstars

add functionality for return indexed

e.g. add option to return keys { extend: true }, and we can get the result


// from 
[{ one: 22, five: 5 }]

// to
[
  {
    value: { one: 22, five: 5 },
    index: 3
  }
]

this is necessary so that later it was not necessary to restart the search function by index if you need to work with reactive data

reslear avatar Mar 10 '22 20:03 reslear

oh i find solution with https://www.npmjs.com/package/just-diff-apply

reslear avatar Mar 10 '22 22:03 reslear

Good thoughts. I've been meaning to add more options in that help real world scenarios. I'll add this feature in the next release. Let me know if you have any other ideas.

Thanks!

tjmoses avatar Mar 10 '22 23:03 tjmoses

@tjmoses thanks for the answer) It would be good to call all about their names, and rename all ...Vals to ...Values

createdVals - createdValues 
updatedVals - updatedValues
deletedVals - deletedValues

reslear avatar Mar 11 '22 00:03 reslear

Hmm, I'll probably just return a tuple and let anyone name them how they want.

tjmoses avatar Mar 11 '22 14:03 tjmoses

@tjmoses tuple is great idea :)

reslear avatar Mar 11 '22 23:03 reslear

so demonstrate example:

before

if (deletedVals) {
  deletedVals.forEach((value) => {
    const index = state.findIndex((_) => _.id === value.id)
    state.splice(index, 1)
  });
}

if (updatedVals) {
  updatedVals.forEach((value) => {
    const index = state.findIndex((_) => _.id === value.id)
    state[index] = value
  })
}

after

if (deletedVals) {
  deletedVals.forEach(({ index }) => state.splice(index, 1))
}

if (updatedVals) {
  updatedVals.forEach(({ value, index }) => state[index] = value)
}

reslear avatar Mar 12 '22 00:03 reslear

Gotcha, def makes sense. Another idea I had was to return functions vs. values so your second after would be like:

deleted(({ index }) => state.splice(index, 1));
updated(({ value, index }) => state[index] = value);

Although, I would use some immutable state library like Immer to mutate the state.

tjmoses avatar Mar 12 '22 04:03 tjmoses

@tjmoses yes, really you went even further, it looks interesting syntax

P.S. thanks for hint Immer, but sometimes it is enough to have simple functions

reslear avatar Mar 12 '22 11:03 reslear