Browse API packages

API · v0.11.0

fn

Standard library functions

Member signatures use schematic object.member(...) API notation. Exported type declarations are valid TypeScript.

Utilities for predicates, identity transforms, and asynchronous callbacks.

These dependency-free utilities work in current browsers, Node, Deno, and Bun.

ts
import fn from "@rcompat/fn";

fn.defined#

Checks that a value is not undefined and narrows its type.

Type parameters

  • T — Input value type.

Parameters

  • value — Value to inspect.
ts
fn.defined<T>(value: T): value is Exclude<T, undefined>;

fn.identity#

Returns a value unchanged while preserving its type and identity.

Type parameters

  • T — Input value type.

Parameters

  • value — Value to return.
ts
fn.identity<T>(value: T): T;

fn.async#

Concurrent asynchronous collection operations.

ts
fn.async: AsyncUtilities;

Async methods#

fn.async.map()#

Maps all array elements concurrently and preserves their result order.

The mapper is invoked through Array.map, then all returned promises are awaited together with Promise.all.

Type parameters

  • T — Input element type.
  • U — Resolved output element type.

Parameters

  • array — Array to map.
  • mapper — Asynchronous mapping callback.

Returns: A promise for mapped results in input order.

ts
fn.async.map<T, U>(array: T[], mapper: AsyncMapper<T, U>): Promise<U[]>;

Example

ts
await fn.async.map([1, 2], async value => value * 2); // [2, 4]

Supporting contracts#

AsyncUtilities#

Concurrent asynchronous collection operations.

ts
interface AsyncUtilities {
  // Maps all array elements concurrently and preserves their result order.
  map<T, U>(array: T[], mapper: AsyncMapper<T, U>): Promise<U[]>;
}

AsyncMapper#

Callback used by fn.async.map.

Type parameters

  • T — Input element type.
  • U — Resolved output element type.

Parameters

  • value — Current array element.
  • index — Current zero-based index.
  • array — Array being mapped.

Returns: A promise for the mapped value.

ts
interface AsyncMapper<T, U> {
  (value: T, index: number, array: T[]): Promise<U>;
}