partition
EditSplits an array into matching and non-matching groups using a predicate.
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];}Download
Section titled “Download”wget -O src/lib/partition.ts https://raw.githubusercontent.com/jrTilak/lazykit/HEAD/registry/functions/partition.tstemp_dir="$(mktemp -d)" && bunx degit jrTilak/lazykit/registry/functions "$temp_dir" && mv "$temp_dir/partition.ts" src/lib/partition.ts && rm -r "$temp_dir"Examples
Section titled “Examples”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[]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 }]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) — Returnstruefor the first group,falsefor the second, and is invoked without a receiver.valueis always present; the original array retains skipped holes.- A type-predicate callback narrows both groups:
[Match[], Exclude<T, Match>[]].
Returns
Section titled “Returns”[T[], T[]] — Matching values followed by non-matching values, with order preserved in both groups.