UNPKG

@augment-vir/common

Version:

A collection of augments, helpers types, functions, and classes for any JavaScript environment.

55 lines (54 loc) 2.67 kB
import { type AnyObject, type Values } from '@augment-vir/core'; import { type ExcludeEmpty } from '../object/empty.js'; import { type KeyCount } from '../object/key-count.js'; import { type TsRecurse, type TsRecursionStart, type TsRecursionTracker, type TsTooMuchRecursion } from '../type/type-recursion.js'; import { type GenericSelectionSet, type SelectFrom, type SelectionSet } from './selection-set.js'; /** * The same as {@link selectFrom} except that the final output is collapsed until the first nested * value that has more than 1 key or that is not an object. * * @category Selection * @category Package : @augment-vir/common * @example * * ```ts * import {selectCollapsedFrom} from '@augment-vir/common'; * * selectCollapsedFrom( * [ * { * child: { * grandChild: 'hi', * grandChild2: 3, * grandChild3: /something/, * }, * }, * { * child: { * grandChild: 'hi', * grandChild2: 4, * grandChild3: /something/, * }, * }, * ], * { * child: { * grandChild2: true, * }, * }, * ); * // output is `[3, 4]` * ``` * * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common) */ export declare function selectCollapsedFrom<Full extends AnyObject, const Selection extends SelectionSet<NoInfer<Full>>>(originalObject: Readonly<Full>, selectionSet: Readonly<Selection>): PickCollapsedSelection<Full, Selection>; /** * Collapses a selected value to the first part of the selection that contains more than 1 key or * that is not an object. This produces the output type for {@link selectCollapsedFrom}. * * @category Selection * @category Package : @augment-vir/common * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common) */ export type PickCollapsedSelection<Full extends Readonly<AnyObject>, Selection extends SelectionSet<Full>, Depth extends TsRecursionTracker = TsRecursionStart> = Depth extends TsTooMuchRecursion ? 'Error: recursive object depth is too deep.' : KeyCount<ExcludeEmpty<NonNullable<SelectFrom<Full, Selection, Depth>>>> extends 1 ? Selection[keyof SelectFrom<Full, Selection, Depth>] extends GenericSelectionSet ? PickCollapsedSelection<NonNullable<Full[keyof SelectFrom<Full, Selection, Depth>]>, Selection[keyof SelectFrom<Full, Selection, Depth>], TsRecurse<Depth>> | Extract<Full[keyof SelectFrom<Full, Selection, Depth>], undefined | null> : Values<SelectFrom<Full, Selection, Depth>> : SelectFrom<Full, Selection, Depth>;