crud-compare
crud-compare copied to clipboard
feature request: return indexes
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
oh i find solution with https://www.npmjs.com/package/just-diff-apply
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 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
Hmm, I'll probably just return a tuple and let anyone name them how they want.
@tjmoses tuple is great idea :)
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)
}
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 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