Skip to content

Add .filterMap(...) on arbitrariesย #6467

@TomerAberbach

Description

@TomerAberbach

๐Ÿš€ Feature Request

It would be nice if there were a .filterMap(...) on arbitraries.

Motivation

Sometimes I find that I need to do some mapping in .map(...), but the mapping is fallible because it's not trivial to represent all the necessary preconditions in the source arbitrary.

In that case, I end up needing to extract the precondition checks to a .filter(...), but that's annoying because it forces me to split the logic, which often results in duplicative work between the .filter(...) and .map(...).

It would be nice if I could use a .filterMap(...) and return the mapped value or null (or maybe a better API would be some Result<T> return type, so that null is an allowed mapped values).

Example

arb.filterMap(value => {
  const transformedValue = transform(value)
  if (someFancyCondition(transformedValue)) {
    return { ok: true, value: transformedValue }
  }
  return { ok: false }
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions