API reference › @evolu/common › Array › partitionArray
Call Signature
function partitionArray<T, S>(
array: readonly T[],
refinement: RefinementWithIndex<T, S>,
): readonly [readonly S[], readonly Exclude<T, S>[]];
Defined in: packages/common/src/Array.ts:572
Partitions an array into two readonly arrays based on a predicate or refinement function.
Returns a tuple where the first array contains elements that satisfy the predicate, and the second array contains elements that do not.
When used with a refinement function (with value is Type syntax),
TypeScript will narrow the first array to the narrowed type, making it useful
for filtering with Evolu Types like PositiveInt.is.
Example
With predicate
const [evens, odds] = partitionArray([1, 2, 3, 4, 5], (x) => x % 2 === 0);
evens; // [2, 4]
odds; // [1, 3, 5]
With refinement
const mixed: ReadonlyArray<NonEmptyString | PositiveInt> = [
NonEmptyString.orThrow("hello"),
PositiveInt.orThrow(42),
];
const [positiveInts, strings] = partitionArray(mixed, PositiveInt.is);
// positiveInts: ReadonlyArray<PositiveInt> (narrowed type)
// strings: ReadonlyArray<NonEmptyString> (Exclude<T, PositiveInt>)
Call Signature
function partitionArray<T>(
array: readonly T[],
predicate: PredicateWithIndex<T>,
): readonly [readonly T[], readonly T[]];
Defined in: packages/common/src/Array.ts:577
With predicate.