sodiray / radash

Functional utility library - modern, simple, typed, powerful
https://radash-docs.vercel.app
MIT License
4.3k stars 173 forks source link

feat: Add `collect` and `collectFirst` functions #327

Open adamhamlin opened 1 year ago

adamhamlin commented 1 year ago

Description

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

Checklist

Resolves

Resolves #269

vercel[bot] commented 1 year ago

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 commented 1 year ago

@rayepps bump

sodiray commented 8 months ago

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 commented 8 months ago

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.