-
Notifications
You must be signed in to change notification settings - Fork 51
feat: add pluck
function
#376
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
Conversation
Hey again, @nusohiro. 👋 Would you say this has considerable advantages over using users.map(user => _.pick(user, ['name', 'age'])) edit: Oh, just realized it's not quite equivalent behavior. Is there a reason you would want an array of tuples instead of objects? users.map(user => _.pick(user, ['name', 'age']))
// => { name, age }[]
_.pluck(users, ['name', 'age'])
// => [name, age][] One of Radashi's core values is to avoid syntax sugar, as we prefer codebases use idiomatic JavaScript as long as it's not excessively verbose or hard to read. Basically, an over-abundance of abstractions makes a codebase harder to follow, so we don't want to encourage that. |
Sorry for the delay, and thanks for the thoughtful feedback! The motivation behind this
|
Thanks for the clarification! I think we can accept this function into Radashi. Two more questions:
|
Thanks, that's great to hear!
No strong objection at all — I'm happy to switch to an array for the
Good question! The main reason for returning a flat array when a single key is passed is simply because it's often easier to work with. When you're extracting something like an That said, I can see the argument for always returning a tuple-like structure for consistency. If you prefer that (i.e. always returning |
634a16b
to
4574fe9
Compare
Benchmark Results
Performance regressions of 30% or more should be investigated, unless they were anticipated. Smaller regressions may be due to normal variability, as we don't use dedicated CI infrastructure. |
Thanks @nusohiro! Hope to see more from you in the future. As you probably saw, I made the necessary changes. :) |
Excuse the notification! This was released in a previous beta, so it shouldn't renotify... 😅 |
Summary
This PR adds the
pluck
function.The
pluck
function extracts values from an array of objects based on specified keys and returns a new array containing those values. It does not mutate the original array.This implementation is inspired by Ruby's
pluck
, which allows extracting column values from ActiveRecord models and Enumerable models efficiently.Related issue, if any:
N/A
For any code change,
Does this PR introduce a breaking change?
No
Bundle impact
src/array/pluck.ts
Footnotes
Function size includes the
import
dependencies of the function. ↩