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

Package Idea: array upsert #481

Open
TClark1011 opened this issue Aug 2, 2022 · 2 comments · May be fixed by #499
Open

Package Idea: array upsert #481

TClark1011 opened this issue Aug 2, 2022 · 2 comments · May be fixed by #499
Labels

Comments

@TClark1011
Copy link
Contributor

An idea for a new package: An "upsert" function for arrays. This would be based on the "upsert" operation commonly used in databases, which is a combination of "update" and "insert", however, we would adapt it to fit better for use with arrays.

You would call the function similar to this:

upsert([1,2,3,4], 2, -1) // [1,2,-1,4]
upsert(["a","b","c"], 6, "d") // ["a","b","c","d"]

The idea is that you pass an array, and index that you want to "upsert" into, and the element which you want to be "upserted". If the provided index is within the bounds of the array, then the provided item will replace the item at that index. If the provided index is outside the bounds of the provided index, then it gets pushed onto the end of the array.

I can't really say for sure how much a function like this would be used/needed, I just thought I would suggest it since I had to write my own version of it for a recent project.

@angus-c
Copy link
Owner

angus-c commented Aug 6, 2022

Hi Thomas, thanks for the suggestion.
My first thought was you could achieve the same with Array#splice.
But that said, splice mutates and does not return the updated value. So yes I think this has value!

@Lucky-victory
Copy link

Permission to work on this?

@TClark1011 TClark1011 linked a pull request Sep 16, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants