Skip to content

filter()

Forwards upstream values only when predicate returns true; otherwise holds the last passing value. Returns a StoreOperator for use with pipe().

Signature

ts
function filter<A>(
	predicate: (value: A) => boolean,
	opts?: StoreOptions,
): StoreOperator<A, A | undefined>

Parameters

ParameterTypeDescription
predicate(value: A) =&gt; booleanIf false, downstream gets RESOLVED (no new DATA) when the held value is unchanged.
optsStoreOptionsOptional name and equals for memoization.

StoreOptions

PropertyTypeDefaultDescription
namestringundefinedDebug name for Inspector.
equals(a: A, b: A) =&gt; booleanundefinedPush-phase dedup when the filtered value repeats.

Returns

StoreOperator&lt;A, A | undefined&gt;get() re-evaluates the predicate against the current input when disconnected.

Basic Usage

ts
import { state, pipe } from 'callbag-recharge';
import { filter } from 'callbag-recharge/extra';

const n = state(0);
const evens = pipe(n, filter((x) => x % 2 === 0));
n.set(2);
evens.get(); // 2

Options / Behavior Details

  • Tier 1: Participates in diamond resolution; forwards STATE from upstream.

See Also

Released under the MIT License.