API reference@evolu/commonArray › mapArray

Call Signature

function mapArray<T, U>(
  array: readonly [T, T],
  mapper: (item: T, index: number, array: readonly T[]) => U,
): readonly [U, U];

Defined in: packages/common/src/Array.ts:445

Maps an array using a mapper function, returning a new readonly array.

Preserves non-empty type.

Mapping with indexes while preserving non-emptiness

import {
  assertEqual,
  assertType,
  mapArray,
  type NonEmptyReadonlyArray,
} from "@evolu/common";

const values: ReadonlyArray<number> = [1, 2, 3];
const indexed = mapArray(values, (value, index) => value + index);
assertEqual(indexed, [1, 3, 5]);
assertType<ReadonlyArray<number>, typeof indexed>();

const nonEmpty: NonEmptyReadonlyArray<number> = [1, 2, 3];
const doubled = mapArray(nonEmpty, (x) => x * 2);
assertType<NonEmptyReadonlyArray<number>, typeof doubled>();

The mapper receives (item, index, array), matching native Array.map.

Call Signature

function mapArray<T, U>(
  array: readonly T[],
  mapper: (item: T, index: number, array: readonly T[]) => U,
): readonly U[];

Defined in: packages/common/src/Array.ts:450

Possibly empty array.