Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature request: return indexes #16

Open
reslear opened this issue Mar 10, 2022 · 8 comments
Open

feature request: return indexes #16

reslear opened this issue Mar 10, 2022 · 8 comments
Labels
enhancement New feature or request

Comments

@reslear
Copy link

reslear commented Mar 10, 2022

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
Copy link
Author

reslear commented Mar 10, 2022

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

@tjmoses
Copy link
Owner

tjmoses commented Mar 10, 2022

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 tjmoses added the enhancement New feature or request label Mar 10, 2022
@reslear
Copy link
Author

reslear commented Mar 11, 2022

@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

@tjmoses
Copy link
Owner

tjmoses commented Mar 11, 2022

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

@reslear
Copy link
Author

reslear commented Mar 11, 2022

@tjmoses tuple is great idea :)

@reslear
Copy link
Author

reslear commented Mar 12, 2022

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)
}

@tjmoses
Copy link
Owner

tjmoses commented Mar 12, 2022

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.

@reslear
Copy link
Author

reslear commented Mar 12, 2022

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants