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

feat: Add collect and collectFirst functions #327

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

adamhamlin
Copy link

Description

Adds two new functions: collect and collectFirst. See more info/discussion in #269.

  • collect - Map+filter operation where undefined transformations are filtered out
  • collectFirst - Map+find operation where the first defined transformation is returned

Checklist

  • Changes are covered by tests if behavior has been changed or added
  • Tests have 100% coverage
  • If code changes were made, the version in package.json has been bumped (matching semver)
  • If code changes were made, the yarn build command has been run and to update the cdn directory
  • If code changes were made, the documentation (in the /docs directory) has been updated

Resolves

Resolves #269

@vercel
Copy link

vercel bot commented Jul 8, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
radash-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 8, 2023 3:16pm

@adamhamlin
Copy link
Author

@rayepps bump

@sodiray
Copy link
Owner

sodiray commented Feb 21, 2024

Hey @adamhamlin thanks for the PR 🙏 and apologies for the delay 🙃 I think the collect function as you've implemented it is super similar to the existing select function. Although, there is no selectFirst.

@adamhamlin
Copy link
Author

Hey @adamhamlin thanks for the PR 🙏 and apologies for the delay 🙃 I think the collect function as you've implemented it is super similar to the existing select function. Although, there is no selectFirst.

Yes, I definitely agree that collect and select are duplicative. Reworking this PR to add selectFirst is certainly doable, but as I kind of alluded to in the associated issue, I think the semantics of collect and collectFirst are preferable--namely, using a single function arg instead of 2.

In practice when using collect, I find myself doing something like the following that would require some code duplication using select, for example:

const selectRes = select(
    myArr,
    (someObj) => someObj.nestedProp.valueIWant,
    (someObj) => someObj.nestedProp.valueIWant !== undefined
);
// versus...
const collectRes = collect(
    myArr,
    (someObj) => someObj.nestedProp.valueIWant
);

Obviously, it's up to you. Ultimately, if we go with collect/collectFirst, it would probably warrant deprecating select at some point.

This PR has conflicts now, so I'll wait to hear back from you before fixing those up.

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.

Any interest in collect/collectFirst?
2 participants