Package: effect
Module: Option
Filters an Option using a predicate. Returns None if the predicate is
not satisfied or the input is None.
When to use
Use when you need to discard an Option’s present value when it does not
meet a condition, while narrowing the type via a refinement predicate.
Details
None → NoneSome where predicate(value) is true → Some(value)Some where predicate(value) is false → NoneExample (Filtering with a predicate)
import { Option } from "effect"
const removeEmpty = (input: Option.Option<string>) =>
Option.filter(input, (value) => value !== "")
console.log(removeEmpty(Option.some("hello")))
// Output: { _id: 'Option', _tag: 'Some', value: 'hello' }
console.log(removeEmpty(Option.some("")))
// Output: { _id: 'Option', _tag: 'None' }
console.log(removeEmpty(Option.none()))
// Output: { _id: 'Option', _tag: 'None' }
See
filterMap to transform and filter simultaneouslyexists to test without filteringSignature
declare const filter: { <A, B extends A>(refinement: Refinement<A, B>): (self: Option<A>) => Option<B>; <A>(predicate: Predicate<A>): <B extends A>(self: Option<B>) => Option<B>; <A, B extends A>(self: Option<A>, refinement: Refinement<A, B>): Option<B>; <A>(self: Option<A>, predicate: Predicate<A>): Option<A>; }
Since v2.0.0