Skip to content

partition

Edit

Splits an array into matching and non-matching groups using a predicate.

partition.ts
export type PartitionPredicate<Value> = (
this: void,
value: Value,
index: number,
array: readonly (Value | undefined)[]
) => boolean;
export type PartitionTypeGuard<Value, Match extends Value> = (
this: void,
value: Value,
index: number,
array: readonly (Value | undefined)[]
) => value is Match;
export type PartitionResult<Value> = [matches: Value[], nonMatches: Value[]];
export type GuardedPartitionResult<Value, Match extends Value> = [
matches: Match[],
nonMatches: Exclude<Value, Match>[],
];
/** Splits values into matching and non-matching groups. */
export function partition<Value, Match extends Value>(
array: readonly Value[],
predicate: PartitionTypeGuard<Value, Match>
): GuardedPartitionResult<Value, Match>;
export function partition<Value>(
array: readonly Value[],
predicate: PartitionPredicate<Value>
): PartitionResult<Value>;
export function partition<Value>(
array: readonly Value[],
predicate: PartitionPredicate<Value>
): PartitionResult<Value> {
const matches: Value[] = [];
const nonMatches: Value[] = [];
for (let index = 0; index < array.length; index += 1) {
if (!Object.hasOwn(array, index)) continue;
const value = array[index] as Value;
(predicate(value, index, array) ? matches : nonMatches).push(value);
}
return [matches, nonMatches];
}
Terminal
wget -O src/lib/partition.ts https://raw.githubusercontent.com/jrTilak/lazykit/HEAD/registry/functions/partition.ts
partition.example.ts
import { partition } from "./partition";
const values: Array<string | number> = ["one", 2, "three", 4];
const [strings, numbers] = partition(
values,
(value): value is string => typeof value === "string"
);
console.log(strings); // ["one", "three"] and typed as string[]
console.log(numbers); // [2, 4] and typed as number[]
partition.2.example.ts
import { partition } from "./partition";
const people = [
{ name: 'Alice', active: true },
{ name: 'Bob', active: false },
{ name: 'Charlie', active: true },
];
const [active, inactive] = partition(people, (person) => person.active);
console.log(active); // Output: [{ name: 'Alice', active: true }, { name: 'Charlie', active: true }]
console.log(inactive); // Output: [{ name: 'Bob', active: false }]
partition.3.example.ts
import { partition } from "./partition";
const words = ["short", "exceedingly long", "medium", "tiny"];
const [longWords, shortWords] = partition(words, (word) => word.length > 5);
console.log(longWords); // Output: ["exceedingly long", "medium"]
console.log(shortWords); // Output: ["short", "tiny"]
  • array (readonly T[]) — Values to divide. Empty slots are skipped.
  • predicate ((value: T, index, array: readonly (T | undefined)[]) => boolean) — Returns true for the first group, false for the second, and is invoked without a receiver. value is always present; the original array retains skipped holes.
  • A type-predicate callback narrows both groups: [Match[], Exclude<T, Match>[]].

[T[], T[]] — Matching values followed by non-matching values, with order preserved in both groups.