R
fn

partition

v0.0.10tested

Splits an array into two: elements that satisfy the predicate and those that do not

Examples

ts
partition([1, 2, 3, 4], n => n % 2 === 0) // => [[2, 4], [1, 3]]
ts
const [strings, others] = partition(mixed, (v): v is string => typeof v === 'string');

Signatures

ts
export function partition<Value, Matched extends Value>(
  array: Value[],
  predicate: (item: Value, index: number) => item is Matched,
): [Matched[], Array<Exclude<Value, Matched>>];
ts
export function partition<Value>(
  array: Value[],
  predicate: (item: Value, index: number) => boolean,
): [Value[], Value[]];

Type Parameters

Value

Parameters

ParameterTypeDescription
arrayValue[]The array to split
predicate(item: Value, index: number) => booleanDecides which partition an element belongs to

Returns

[Value[], Value[]]A tuple of `[matching, rest]`