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:379

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

Preserves non-empty type.

Mapping with indexes while preserving non-emptiness

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

const values: ReadonlyArray<number> = [1, 2, 3];
const indexed = mapArray(values, (value, index) => value + index);
expect(indexed).toEqual([1, 3, 5]);
expectTypeOf(indexed).toEqualTypeOf<ReadonlyArray<number>>();

const nonEmpty: NonEmptyReadonlyArray<number> = [1, 2, 3];
expectTypeOf(mapArray(nonEmpty, (x) => x * 2)).toEqualTypeOf<
  NonEmptyReadonlyArray<number>
>();

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:384

Possibly empty array.