separateMany

Partition an object into several named groups of keys plus an others catch-all, returning a new object for each group in one pass.

Usage

The separateMany function partitions an object into multiple named groups by key sets.

import { separateMany } from '@movk/core'

const options = { id: 1, name: 'John', email: 'a@b.com', role: 'admin' }
const { a, b, others } = separateMany(options, { a: ['id'], b: ['name'] as const })
// a: { id: 1 }
// b: { name: 'John' }
// others: { email: 'a@b.com', role: 'admin' }

API

separateMany(obj, groups)

Partitions an object into multiple named groups by key sets.

Parameters

obj
T required
The source object.
groups
M required
Group mapping, e.g. { a: ['x', 'y'], b: ['z'] }.

Returns

returns
{ [P in keyof M]: PickByKey<T, M[P][number]> } & { others: OmitByKey<T, M[keyof M][number]> }
An object containing a sub-object for each group plus others (all keys not captured by any group).

Changelog

No recent changes
Copyright © 2024 - 2026 YiXuan - MIT License