Skip to main content
Module

x/optio/mod.ts>filter

Minimum option type port of Rust
Latest
function filter
import { filter } from "https://deno.land/x/optio@1.0.0/mod.ts";

Returns None if the option is None, otherwise calls predicate with the wrapped value and returns:

  • Some if predicate returns true.
  • None if predicate returns false.

Examples

Example 1

import { type Option } from "https://deno.land/x/optio/spec.ts";
import { filter } from "https://deno.land/x/optio/operators/transform.ts";
import { assertType, IsExact } from "https://deno.land/std/testing/types.ts";

declare const isString: (value: unknown) => value is string;
declare const option: Option<string | number>;

const opt = filter(option, isString);
assertType<IsExact<typeof opt, Option<string>>>(true);

Type Parameters

T
optional
U extends T = T

Parameters

option: Option<T>
guard: (value: T) => value is U

Examples

Example 1

import { None, Some } from "https://deno.land/x/optio/spec.ts";
import { filter } from "https://deno.land/x/optio/operators/transform.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

declare const isEven: (value: number) => boolean;

assertEquals(filter(Some(0), isEven), Some(0));
assertEquals(filter(Some(1), isEven), None);
assertEquals(filter(None, isEven), None);

Parameters

option: Option<T>
predicate: (value: T) => boolean