Skip to content

Conversation

nusohiro
Copy link
Contributor

@nusohiro nusohiro commented Mar 15, 2025

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,

  • Related documentation has been updated, if needed
  • Related tests have been added or updated, if needed
  • Related benchmarks have been added or updated, if needed
  • Release notes in next-minor.md or next-major.md have been added, if needed

Does this PR introduce a breaking change?

No

Bundle impact

Status File Size 1
A src/array/pluck.ts 136

Footnotes

  1. Function size includes the import dependencies of the function.

@nusohiro nusohiro requested a review from aleclarson as a code owner March 15, 2025 13:48
@aleclarson
Copy link
Member

aleclarson commented Mar 18, 2025

Hey again, @nusohiro. 👋

Would you say this has considerable advantages over using pick with Array#map?

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.

@aleclarson aleclarson added the awaiting author reply The author hasn't replied to a question, and it's been a few days label Mar 20, 2025
@nusohiro
Copy link
Contributor Author

@aleclarson

Sorry for the delay, and thanks for the thoughtful feedback!

The motivation behind this pluck function comes from a few points:

  • Tuple-style output: Sometimes, especially when working with tabular data (e.g., for exporting CSVs or feeding into charting libraries), the [name, age][] format is more convenient and easier to work with than { name, age }[].
  • Familiarity from Ruby: For teams with Ruby/Rails backgrounds, pluck is a well-known, concise pattern. I referenced ActiveRecord#pluck when designing this, aiming for similar utility.

@aleclarson aleclarson removed the awaiting author reply The author hasn't replied to a question, and it's been a few days label Mar 22, 2025
@aleclarson
Copy link
Member

aleclarson commented Mar 22, 2025

Thanks for the clarification! I think we can accept this function into Radashi.

Two more questions:

  • Any objection to having the keys be an array instead of rest arguments?
  • What's the use case for a single key returning an array of values, rather than an array of tuples? [1, 2, 3] vs [[1], [2], [3]] – Wouldn't it be better to always return an array of tuples?

@nusohiro
Copy link
Contributor Author

@aleclarson

Thanks, that's great to hear!

Any objection to having the keys be an array instead of rest arguments?

No strong objection at all — I'm happy to switch to an array for the keys parameter.
Let me know if you'd like me to update the PR accordingly.

What's the use case for a single key returning an array of values, rather than an array of tuples?

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 id, [1, 2, 3] is more convenient than [[1], [2], [3]] in most cases.
This also follows the behavior of Ruby's pluck, which returns a flat array when only one column is specified.

That said, I can see the argument for always returning a tuple-like structure for consistency. If you prefer that (i.e. always returning [[1], [2], [3]] even for one key), I’m happy to update the implementation — just let me know which direction you’d like to go!

@radashi-bot
Copy link

radashi-bot commented Mar 23, 2025

Benchmark Results

Name Current
pluck: single property from a small array 2,778,799.51 ops/sec ±0.21%
pluck: multiple properties from a small array 2,040,951.59 ops/sec ±0.31%
pluck: all properties from a small array 1,304,199.87 ops/sec ±0.3%
pluck: multiple properties from a large array 10,459.16 ops/sec ±0.7%

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.

@aleclarson aleclarson added this to the v12.5.0 milestone Mar 23, 2025
@aleclarson aleclarson merged commit 8d5c06c into radashi-org:main Mar 23, 2025
9 checks passed
@aleclarson
Copy link
Member

Thanks @nusohiro! Hope to see more from you in the future.

As you probably saw, I made the necessary changes. :)

@aleclarson
Copy link
Member

Excuse the notification! This was released in a previous beta, so it shouldn't renotify... 😅

@aleclarson aleclarson removed this from the v12.6.0 milestone May 15, 2025
@radashi-bot
Copy link

A stable release 12.5.0 has been published to NPM. 🚀

To install:

pnpm add radashi@12.5.0
See the changes

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

Successfully merging this pull request may close these issues.

3 participants