Skip to content

Add second selector for arrays #51

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

Closed
badBlackShark opened this issue Jan 16, 2023 · 5 comments
Closed

Add second selector for arrays #51

badBlackShark opened this issue Jan 16, 2023 · 5 comments

Comments

@badBlackShark
Copy link

badBlackShark commented Jan 16, 2023

There's already first, last, and all. For me, it would often be beneficial to also have a second selector. If you have two elements that need to be mapped differently, then using last also works -- unless the second element doesn't always exist.

So:

people = [{ name: 'Amy' }, { name: 'Bob' }]

get(:people, :first, :name) #=> 'Amy'
get(:people, :second, :name) #=> 'Bob'
get(:people, :last, :name) #=> 'Bob'

people = [{ name: 'Amy' }]

get(:people, :first, :name) #=> 'Amy'
get(:people, :second, :name) #=> nil
get(:people, :last, :name) #=> 'Amy'

Would it be possible to add something like this?

@oleander
Copy link
Owner

There's already the at(index). Does that work?

@badBlackShark
Copy link
Author

Oh really? So given my example I could do:

people = [{ name: 'Amy' }, { name: 'Bob' }]

get(:people, :first, :name) #=> 'Amy'
get(:people, at(1), :name) #=> 'Bob'

people = [{ name: 'Amy' }]

get(:people, :first, :name) #=> 'Amy'
get(:people, at(1), :name) #=> nil

That would indeed solve my problem. In that case, having second as syntax sugar would be neat imo, but not actually needed and probably not worth more than like 15 minutes of effort.

@oleander
Copy link
Owner

Yes, that should work.

First and last are just syntax sugar for at(n). You can easily add "second" your self if you look at the code. It would be good practice if you want to extend the DSL in the future. Just make sure to add some tests if you decide to create a pull request ツ

@oleander
Copy link
Owner

@badBlackShark
Copy link
Author

Oh yeah, that's actually very neat and should be easy. Well, thank you so much! I will consider this solved then! If I add it in the future, I'll make sure to make a PR :) Thank you for your help!

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

No branches or pull requests

2 participants