import { type ArrayOrValue } from './../array/array'; import { type Maybe, type MaybeSo } from '../value/maybe.type'; import { type DefinedValues, type NonNullableValues, type PartialWithUndefined } from '../type'; import { type FilterKeyValueTuplesInput, type KeyValueTuple, type KeyValueTupleFilter, KeyValueTypleValueFilter } from './object.filter.tuple'; import { type Prettify } from 'ts-essentials'; /** * Strips the input object. If the object has no keys after the filtering process, returns undefined. */ export type StripObjectFunction = (input: Maybe, copy?: Maybe) => T | undefined; /** * Creates a reusable {@link StripObjectFunction} that filters values from an object and returns * `undefined` when no keys remain after filtering. * * Useful when an upstream consumer treats an "empty" object as a missing value (e.g. for skipping * persistence or omitting a field from a request body). * * @param filter - Filter controlling which key/value pairs are removed from the input object. * @param copy - When true (default), the returned function shallow-copies the input before filtering instead of mutating it. * @returns Returns the stripped object, or `undefined` when filtering removed every key. * * @dbxUtil * @dbxUtilCategory object * @dbxUtilKind factory * @dbxUtilTags object, strip, filter, factory, undefined, empty * @dbxUtilRelated strip-object, filter-from-pojo-function * * @example * ```ts * const stripUndef = stripObjectFunction(KeyValueTypleValueFilter.UNDEFINED); * stripUndef({ a: 1, b: undefined }); // { a: 1 } * stripUndef({ a: undefined }); // undefined * ``` * @__NO_SIDE_EFFECTS__ */ export declare function stripObjectFunction(filter: FilterKeyValueTuplesInput, copy?: boolean): StripObjectFunction; /** * Removes `undefined` values from the input object and returns `undefined` when no keys remain. * * Convenience wrapper around {@link stripObjectFunction} pre-configured to filter out `undefined`. * * @param input - The object to strip; null/undefined inputs short-circuit and return `undefined`. * @param copy - When true (default), shallow-copies the input before filtering instead of mutating it. * @returns The stripped object, or `undefined` when input was missing or filtering removed every key. * * @example * ```ts * stripObject({ a: 1, b: undefined }); // { a: 1 } * stripObject({ a: undefined }); // undefined * stripObject(null); // undefined * ``` */ export declare function stripObject(input: Maybe, copy?: boolean): T | undefined; /** * Assigns all non-filtered values from one or more source objects into the target object. * * Builds a template from the source objects (in order, so later sources win) and applies it to the target. * By default, undefined values in the source objects are excluded from the template, so they will not override existing target values. * * @param target - The object to override values on. * @param config - Configuration for the override operation. * @param config.copy - Whether to return a shallow copy instead of mutating the target. Defaults to `false`. * @param config.from - One or more source objects whose values will be applied to the target. * @param config.filter - Optional filter to control which key/value pairs from sources are included. Defaults to filtering out `undefined` values. * @returns The modified target (or a copy if `copy` is `true`). * * @example * ```typescript * const target = { a: 1, b: 2 }; * overrideInObject(target, { from: [{ a: 10, c: 3 }] }); * // target is now { a: 10, b: 2, c: 3 } * * // undefined values in source are ignored by default: * overrideInObject(target, { from: [{ a: undefined, b: 99 }] }); * // target.a remains 10, target.b becomes 99 * ``` */ export declare function overrideInObject(target: Partial, { copy, from, filter }: { copy?: boolean | undefined; from: ArrayOrValue>; filter?: KeyValueTupleFilter | undefined; }): Partial; /** * Applies a pre-built template of values to a target object, returning the modified target. */ export type OverrideInObjectFunction = (target: Partial) => Partial; /** * Factory that takes an array of source objects and produces an {@link OverrideInObjectFunction} * that applies the merged template to any target. */ export type OverrideInObjectFunctionFactory = (from: PartialWithUndefined[]) => OverrideInObjectFunction; /** * Configuration for {@link overrideInObjectFunctionFactory}. */ export interface OverrideInObjectFunctionFactoryConfig { /** * Filter controlling which key/value pairs from sources are included in the template. * * Accepts a {@link KeyValueTypleValueFilter} enum value or a {@link KeyValueTupleFilter} object. * When not provided, the default filter removes `undefined` values (i.e., `KeyValueTypleValueFilter.UNDEFINED`). */ readonly filter?: FilterKeyValueTuplesInput | undefined; /** * Whether or not to return a copy of the input value, rather than change it directly. * If true, a copy of the input object will be returned. * If false, the input object will be modified. * * False by default. */ readonly copy?: boolean; /** * Whether or not the template being applied to objects should be recalculated each time. * * This is only necessary if you expect the input targets to change and you want those changes reflected in your copy functions. * * False by default. */ readonly dynamic?: boolean; } /** * Creates an {@link OverrideInObjectFunctionFactory} that merges source objects into a template, * then applies that template to target objects. * * The template is built by iterating the source objects in order (later sources override earlier ones), * filtering each through the configured filter. When applied to a target, the template's values overwrite * corresponding keys on the target. * * By default, `undefined` values in sources are excluded from the template. * * @param config - Configuration controlling filtering, copying, and caching behavior. * @param config.filter - Filter applied to source object values when building the template; by default, `undefined` values are excluded. * @param config.copy - When true, the target object is shallow-copied before applying overrides instead of being mutated in place. * @param config.dynamic - When true, the template is recalculated on each call instead of being cached; useful when source objects may change over time. * @returns A factory function that accepts source objects and returns an override function. * * @dbxUtil * @dbxUtilCategory object * @dbxUtilKind factory * @dbxUtilTags object, override, factory, merge, template, cached * @dbxUtilRelated override-in-object, merge-objects-function, filter-from-pojo-function * * @example * ```typescript * const factory = overrideInObjectFunctionFactory({ copy: true }); * const overrideFn = factory([{ color: 'red' }, { size: 10 }]); * const result = overrideFn({ color: 'blue', size: 5 }); * // result is { color: 'red', size: 10 } (a new copy) * ``` * * @__NO_SIDE_EFFECTS__ */ export declare function overrideInObjectFunctionFactory({ filter, copy, dynamic }: OverrideInObjectFunctionFactoryConfig): OverrideInObjectFunctionFactory; /** * Merges all input objects into a single object. * * Objects are applied left-to-right, so the right-most (last) item in the array has the highest priority. * `Maybe` values (null/undefined entries in the array) are silently ignored. * * By default, `undefined` values within each source object are excluded from the merge, * meaning an `undefined` value will not overwrite a previously set value. * * When the first element is a complete, non-null `T` and the filter only drops nullish values (`UNDEFINED` or `NULL`), * the result is typed as a complete `T`: the "merge config over defaults" shape. Under `KeyValueTypleValueFilter.NULL`, * a default whose value is `null` is dropped from the result too. * * @param objects - Array of objects (or null/undefined) to merge together. * @param filter - Optional filter controlling which key/value pairs are included. Defaults to filtering out `undefined` values (`KeyValueTypleValueFilter.UNDEFINED`). * @returns A new object containing the merged result. * * @example * ```typescript * const result = mergeObjects([{ a: 1, b: 2 }, { b: 3, c: 4 }]); * // result is { a: 1, b: 3, c: 4 } * * // undefined values in sources do not override: * const result2 = mergeObjects([{ a: 1 }, { a: undefined, b: 2 }]); * // result2 is { a: 1, b: 2 } * * // a complete default first returns a complete T (UNDEFINED or NULL filter only). Pass explicitly so an * // override whose type is narrower than Partial (e.g. the rest of a destructure) cannot bind the Partial overload: * const limits = mergeObjects([DEFAULT_LIMITS, config.limits]); * // limits is Limits; an explicit `undefined` in config.limits keeps the default * ``` */ export declare function mergeObjects(objects: readonly [T, ...ReadonlyArray>>>], filter?: MergeObjectsCompleteFilter): T; export declare function mergeObjects(objects: ReadonlyArray>>, filter?: FilterKeyValueTuplesInput): Partial; /** * The {@link mergeObjects} filters that only drop nullish values, so merging partial overrides over a complete default * object keeps every default key. */ export type MergeObjectsCompleteFilter = KeyValueTypleValueFilter.UNDEFINED | KeyValueTypleValueFilter.NULL; /** * Function that merges an array of partial objects into a single object. * Objects are applied left-to-right, so the last item in the array has the highest priority. */ export type MergeObjectsFunction = (objects: ReadonlyArray>>) => Partial; /** * Creates a reusable {@link MergeObjectsFunction} with a pre-configured filter. * * Useful when you need to merge objects multiple times with the same filter configuration. * By default, `undefined` values are filtered out of the result (`KeyValueTypleValueFilter.UNDEFINED`). * * @param filter - Optional filter controlling which key/value pairs are included. Defaults to filtering out `undefined` values. * @returns A reusable merge function. * * @dbxUtil * @dbxUtilCategory object * @dbxUtilKind factory * @dbxUtilTags object, merge, factory, filter * @dbxUtilRelated merge-objects, override-in-object-function-factory * * @example * ```typescript * const merge = mergeObjectsFunction(); * const result = merge([{ a: 1 }, { b: 2 }, { a: 3 }]); * // result is { a: 3, b: 2 } * * // With null filter to also exclude null values: * const mergeNoNulls = mergeObjectsFunction(KeyValueTypleValueFilter.NULL); * ``` * * @__NO_SIDE_EFFECTS__ */ export declare function mergeObjectsFunction(filter?: FilterKeyValueTuplesInput): MergeObjectsFunction; /** * Returns a copy of the input object with all `undefined` values removed. * When `filterNull` is `true`, `null` values are also removed. * * This is a convenience wrapper around {@link filterOnlyUndefinedValues} and {@link filterNullAndUndefinedValues}. * * @param obj - The object to filter. * @param filterNull - If `true`, both `null` and `undefined` values are removed. Defaults to `false`. * The result is typed as {@link DefinedValues} (or {@link NonNullableValues} when `filterNull` is `true`). A required key whose * type includes `undefined` stays required in the result type, even when the filter removed it. * * @returns A shallow copy of the object with filtered values removed. * * @example * ```typescript * filterUndefinedValues({ a: 1, b: undefined, c: null }); * // { a: 1, c: null } * * filterUndefinedValues({ a: 1, b: undefined, c: null }, true); * // { a: 1 } * ``` */ export declare function filterUndefinedValues(obj: T): DefinedValues; export declare function filterUndefinedValues(obj: T, filterNull: true): NonNullableValues; export declare function filterUndefinedValues(obj: T, filterNull?: boolean): DefinedValues; /** * Pre-built filter that returns a copy of the input object with all `undefined` values removed. * Keys with `null` or other falsy values are retained. * * @example * ```typescript * filterOnlyUndefinedValues({ a: 1, b: undefined, c: null }); * // { a: 1, c: null } * ``` */ export declare const filterOnlyUndefinedValues: GeneralFilterFromPOJOFunction; /** * Pre-built filter that returns a copy of the input object with all `null` and `undefined` values removed. * Keys with other falsy values (0, false, '') are retained. * * @example * ```typescript * filterNullAndUndefinedValues({ a: 1, b: undefined, c: null, d: 0 }); * // { a: 1, d: 0 } * ``` */ export declare const filterNullAndUndefinedValues: GeneralFilterFromPOJOFunction; /** * The keys of `T` whose value may be `null` or `undefined`, optional keys included. * * These are the keys {@link filterMaybeValues} may drop. */ export type MaybeValueKeys = { [K in keyof T]-?: undefined extends T[K] ? K : null extends T[K] ? K : never; }[keyof T]; /** * The static type of {@link filterMaybeValues}'s result. * * A key whose value may be `null` or `undefined` becomes optional, and its value type loses `null` and `undefined`. * Every other key keeps its declared type, and stays required when it was required. */ export type FilteredMaybeValues = Prettify> & { [K in MaybeValueKeys]?: MaybeSo; }>; /** * Returns a copy of the input object with every `null` and `undefined` value removed, typed so that a nullable key * becomes optional and non-nullable. * * Use it to shape a projection whose source carries `Maybe` values (a wire reply, a parsed row) into an object whose * optional fields must be absent rather than nullish, such as a persisted row or a host-stored record. It replaces a * run of `...(x == null ? {} : { x })` spreads with one plain literal, and the result fits an `x?: X` target without a * cast, which {@link filterNullAndUndefinedValues} and {@link filterUndefinedValues} cannot offer since they return `T`. * * The runtime is {@link filterNullAndUndefinedValues}: the filter is shallow (a nested `null` is kept), it keeps the * input's key order, it keeps other falsy values (`0`, `false`, `''`), and it never mutates the input. * * A value whose type is still a generic parameter at the call site cannot be judged nullable or not, so the result * type cannot settle its key. Filter only the concrete keys and spread the result: * `{ model, answers, ...filterMaybeValues({ usage, provider }) }`. * * @param obj - The object to filter. Every key that must survive should have a non-nullable type, since only a nullable key may be dropped. * @returns A shallow copy of `obj` without its `null` and `undefined` values. * * @dbxUtil * @dbxUtilCategory object * @dbxUtilKind function * @dbxUtilTags object, pojo, filter, maybe, nullish, projection * @dbxUtilRelated filter-undefined-values, merge-objects * * @example * ```typescript * interface Usage { * readonly promptTokens: number; * readonly cachedTokens?: number; * } * * const wire: { prompt_tokens: number; cached_tokens?: Maybe } = { prompt_tokens: 12, cached_tokens: null }; * * const usage: Usage = filterMaybeValues({ promptTokens: wire.prompt_tokens, cachedTokens: wire.cached_tokens }); * // { promptTokens: 12 } * * filterMaybeValues({ a: 0, b: false, c: '', d: null, e: undefined }); * // { a: 0, b: false, c: '' } * ``` */ export declare function filterMaybeValues(obj: T): FilteredMaybeValues; /** * Pre-built filter that returns a copy of the input object with all empty values removed. * Empty values include `null`, `undefined`, empty strings (`''`), empty arrays (`[]`), and empty objects (`{}`). * * @example * ```typescript * filterEmptyPojoValues({ a: 1, b: '', c: [], d: null, e: 'hello' }); * // { a: 1, e: 'hello' } * ``` */ export declare const filterEmptyPojoValues: GeneralFilterFromPOJOFunction; /** * Pre-built filter that returns a copy of the input object with all falsy and empty values removed. * Removes `null`, `undefined`, `0`, `false`, `''`, empty arrays, and empty objects. * * @example * ```typescript * filterFalsyAndEmptyValues({ a: 1, b: false, c: 0, d: '', e: 'hello' }); * // { a: 1, e: 'hello' } * ``` */ export declare const filterFalsyAndEmptyValues: GeneralFilterFromPOJOFunction; /** * Pre-built function that returns all keys from a POJO whose values are not `undefined`. * Keys with `null` or other falsy values are included in the result. * * @example * ```typescript * allNonUndefinedKeys({ a: 'test', b: undefined, c: null, d: 0 }); * // ['a', 'c', 'd'] * ``` */ export declare const allNonUndefinedKeys: GeneralFindPOJOKeysFunction; /** * Pre-built function that returns all keys from a POJO whose values are not falsy or empty. * Excludes keys with `null`, `undefined`, `0`, `false`, `''`, empty arrays, or empty objects. * * @example * ```typescript * allFalsyOrEmptyKeys({ a: 'test', b: false, c: 0, d: null }); * // ['a'] * ``` */ export declare const allFalsyOrEmptyKeys: GeneralFindPOJOKeysFunction; /** * Pre-built function that returns all keys from a POJO whose values are not `null` or `undefined`. * Keys with other falsy values (0, false, '') are included. * * @example * ```typescript * allMaybeSoKeys({ a: 'test', b: undefined, c: null, d: 0 }); * // ['a', 'd'] * ``` */ export declare const allMaybeSoKeys: GeneralFindPOJOKeysFunction; /** * Finds and returns keys from the POJO whose values pass the given filter. * * The filter determines which values are considered "matching" — matched values have their keys included in the result. * For example, using `KeyValueTypleValueFilter.UNDEFINED` returns all keys whose values are NOT `undefined`. * * @param obj - The object to inspect. * @param filter - A {@link FilterKeyValueTuplesInput} controlling which key/value pairs match. Required (no default). * @returns Array of keys whose values pass the filter. * * @example * ```typescript * findPOJOKeys({ a: 1, b: undefined, c: null }, KeyValueTypleValueFilter.NULL); * // ['a'] — only 'a' has a non-null, non-undefined value * ``` */ export declare function findPOJOKeys(obj: T, filter: FilterKeyValueTuplesInput): (keyof T)[]; /** * Function that returns an array of keys from a POJO whose values pass a pre-configured filter. */ export type FindPOJOKeysFunction = (obj: T) => (keyof T)[]; /** * Generic version of {@link FindPOJOKeysFunction} that accepts any object type. */ export type GeneralFindPOJOKeysFunction = (obj: T) => (keyof T)[]; /** * Creates a reusable {@link FindPOJOKeysFunction} with a pre-configured filter. * * The returned function inspects each key/value pair on the input object and collects keys * whose values pass the filter. * * @param filter - A {@link FilterKeyValueTuplesInput} controlling which values match. Required (no default). * @returns A reusable function that returns matching keys from any input object. * * @dbxUtil * @dbxUtilCategory object * @dbxUtilKind factory * @dbxUtilTags object, pojo, keys, find, factory, filter * @dbxUtilRelated find-pojo-keys, count-pojo-keys-function, for-each-key-value-on-pojo-function * * @example * ```typescript * const findDefinedKeys = findPOJOKeysFunction(KeyValueTypleValueFilter.UNDEFINED); * findDefinedKeys({ a: 1, b: undefined, c: 'hello' }); * // ['a', 'c'] * ``` * * @__NO_SIDE_EFFECTS__ */ export declare function findPOJOKeysFunction(filter: FilterKeyValueTuplesInput): FindPOJOKeysFunction; /** * Counts the number of keys on the POJO whose values pass the given filter. * * By default, counts all keys whose values are not `undefined` (`KeyValueTypleValueFilter.UNDEFINED`). * * @param obj - The object to inspect. * @param filter - A {@link FilterKeyValueTuplesInput} controlling which values are counted. Defaults to `KeyValueTypleValueFilter.UNDEFINED`. * @returns The number of keys whose values pass the filter. * * @example * ```typescript * countPOJOKeys({ a: 1, b: undefined, c: null }); * // 2 — 'a' and 'c' are not undefined * * countPOJOKeys({ a: 1, b: undefined, c: null }, KeyValueTypleValueFilter.NULL); * // 1 — only 'a' is not null or undefined * ``` */ export declare function countPOJOKeys(obj: T, filter?: FilterKeyValueTuplesInput): number; /** * Function that counts the number of keys on an object whose values pass a pre-configured filter. */ export type CountPOJOKeysFunction = (obj: T) => number; /** * Creates a reusable {@link CountPOJOKeysFunction} with a pre-configured filter. * * By default, counts all keys whose values are not `undefined` (`KeyValueTypleValueFilter.UNDEFINED`). * * @param filter - A {@link FilterKeyValueTuplesInput} controlling which values are counted. Defaults to `KeyValueTypleValueFilter.UNDEFINED`. * @returns A reusable function that counts matching keys on any input object. * * @dbxUtil * @dbxUtilCategory object * @dbxUtilKind factory * @dbxUtilTags object, pojo, keys, count, factory, filter * @dbxUtilRelated count-pojo-keys, find-pojo-keys-function * * @example * ```typescript * const countDefined = countPOJOKeysFunction(); * countDefined({ a: 1, b: undefined, c: 'test' }); * // 2 * ``` * * @__NO_SIDE_EFFECTS__ */ export declare function countPOJOKeysFunction(filter?: FilterKeyValueTuplesInput): CountPOJOKeysFunction; /** * Configuration for {@link filterFromPOJO} and {@link filterFromPOJOFunction}. */ export interface FilterFromPOJO { /** * Whether to return a shallow copy of the object instead of mutating the original. * Defaults to `false` when used via {@link filterFromPOJOFunction}, but the pre-built * constants (e.g., {@link filterOnlyUndefinedValues}) use `true`. */ copy?: boolean; /** * Filter determining which values to remove. Accepts a {@link KeyValueTypleValueFilter} enum or a {@link KeyValueTupleFilter} object (without `inverse`). * Defaults to `{ valueFilter: KeyValueTypleValueFilter.UNDEFINED }`, which removes `undefined` values. */ filter?: Omit, 'inverse'>; } /** * Removes values from the input object based on the filter configuration. * * By default, removes `undefined` values and does NOT copy the object (mutates in place). * Pass `{ copy: true }` to get a new object without modifying the original. * * @param obj - The POJO to filter values from. * @param config - Configuration for filtering and copying behavior. Defaults to removing `undefined` values without copying. * When called without a config, the result is typed as {@link DefinedValues}, since the default filter removes `undefined` values. * A required key whose type includes `undefined` stays required in the result type, even when the filter removed it. * * @returns The filtered object (either the mutated original or a shallow copy, depending on `config.copy`). * * @example * ```typescript * // Remove undefined values (default): * filterFromPOJO({ a: 1, b: undefined, c: 'hello' }); * // { a: 1, c: 'hello' } * * // Remove null and undefined values: * filterFromPOJO({ a: 1, b: null, c: undefined }, { filter: { valueFilter: KeyValueTypleValueFilter.NULL } }); * // { a: 1 } * ``` */ export declare function filterFromPOJO(obj: T): DefinedValues; export declare function filterFromPOJO(obj: T, config: FilterFromPOJO): T; /** * Function that removes filtered values from an input object. * Accepts an optional `copyOverride` parameter to control copy behavior per-call. */ export type FilterFromPOJOFunction = (input: T, copyOverride?: Maybe) => T; /** * Generic version of {@link FilterFromPOJOFunction} that accepts any object type. */ export type GeneralFilterFromPOJOFunction = (input: T) => T; /** * Creates a reusable {@link FilterFromPOJOFunction} with a pre-configured filter and copy behavior. * * The returned function removes key/value pairs that match the filter from the input object. * Internally, the filter is inverted so that matching values are deleted while non-matching values are retained. * * By default, removes `undefined` values (`KeyValueTypleValueFilter.UNDEFINED`) and does not copy (`copy: false`). * * @param config - Configuration for filtering and copying. Defaults to `{ copy: false, filter: { valueFilter: KeyValueTypleValueFilter.UNDEFINED } }`. * @param config.copy - When true, returns a shallow copy with filtered keys removed instead of mutating the input; defaults to false. * @param config.filter - The filter criteria determining which key/value pairs to remove; defaults to removing `undefined` values. * @returns A reusable filter function. The returned function also accepts a `copyOverride` argument to override the copy behavior per-call. * * @dbxUtil * @dbxUtilCategory object * @dbxUtilKind factory * @dbxUtilTags object, pojo, filter, factory, remove, copy * @dbxUtilRelated filter-from-pojo, strip-object-function, assign-values-to-pojo-function * * @example * ```typescript * // Default: removes undefined values, mutates in place * const filterUndef = filterFromPOJOFunction(); * const obj = { a: 1, b: undefined }; * filterUndef(obj); // obj is now { a: 1 } * * // With copy and null filter: * const filterNulls = filterFromPOJOFunction({ copy: true, filter: { valueFilter: KeyValueTypleValueFilter.NULL } }); * const result = filterNulls({ a: 1, b: null, c: undefined }); * // result is { a: 1 }, original is unchanged * ``` * * @__NO_SIDE_EFFECTS__ */ export declare function filterFromPOJOFunction({ copy, filter: inputFilter }?: FilterFromPOJO): FilterFromPOJOFunction; /** * Pre-built {@link FilterFromPOJOFunction} that removes `undefined` values by mutating the input object (no copy). * * Used internally as the default filter for {@link overrideInObjectFunctionFactory} when no filter is provided. * * @example * ```typescript * const obj = { a: 1, b: undefined }; * defaultFilterFromPOJOFunctionNoCopy(obj); * // obj is now { a: 1 } * ``` */ export declare const defaultFilterFromPOJOFunctionNoCopy: FilterFromPOJOFunction; /** * Assigns filtered values from `obj` onto `target`. * * By default, only non-`undefined` values from `obj` are assigned, and a copy of `target` is returned (the original is not mutated). * * @param target - The object to assign values onto. * @param obj - The source object whose matching values will be assigned. * @param input - Optional filter/copy configuration. Defaults to `KeyValueTypleValueFilter.UNDEFINED` with `copy: true`. * @returns The target with values assigned (either a copy or the original, depending on configuration). * * @example * ```typescript * const target = { a: 1, b: 2 }; * const result = assignValuesToPOJO(target, { a: 10, b: undefined }); * // result is { a: 10, b: 2 } (copy), target unchanged * ``` */ export declare function assignValuesToPOJO(target: T, obj: T, input?: AssignValuesToPOJOFunctionInput): T; /** * Overload that always returns a copy of the target with values assigned. */ export type AssignValuesToPOJOCopyFunction = (target: T, obj: T, returnCopy: true) => T; /** * Overload that mutates and returns the original target with values assigned. */ export type AssignValuesToPOJONoCopyFunction = (target: I, obj: T, returnCopy: false) => I; /** * Function that assigns filtered values from a source object onto a target object. * * Accepts an optional third argument `returnCopy` to override the default copy behavior per-call. * The `_returnCopyByDefault` property indicates the configured default copy behavior. */ export type AssignValuesToPOJOFunction = ((target: T, obj: T, returnCopy?: boolean) => T) & AssignValuesToPOJOCopyFunction & AssignValuesToPOJONoCopyFunction & { readonly _returnCopyByDefault: boolean; }; /** * Configuration for {@link assignValuesToPOJOFunction}. * * Can be a simple {@link KeyValueTypleValueFilter} enum value, or a {@link KeyValueTupleFilter} object * with an additional `copy` property. When a simple enum value is provided, `copy` defaults to `true`. */ export type AssignValuesToPOJOFunctionInput = KeyValueTypleValueFilter | (KeyValueTupleFilter & { /** * Whether or not to copy the target object before assigning values, returning the new object. * * True by default. */ copy?: boolean; }); /** * Creates a reusable {@link AssignValuesToPOJOFunction} with a pre-configured filter and copy behavior. * * The returned function assigns key/value pairs from a source object onto a target, skipping pairs * that are filtered out. By default, `undefined` values are filtered out (`KeyValueTypleValueFilter.UNDEFINED`) * and the target is copied before assignment (`copy: true`). * * @param input - Filter and copy configuration. Defaults to `KeyValueTypleValueFilter.UNDEFINED` (filter undefined, copy target). * @returns A reusable assign function with a `_returnCopyByDefault` property indicating the configured copy behavior. * * @dbxUtil * @dbxUtilCategory object * @dbxUtilKind factory * @dbxUtilTags object, pojo, assign, factory, filter, copy * @dbxUtilRelated assign-values-to-pojo, filter-from-pojo-function * * @example * ```typescript * // Default: filters undefined, returns a copy * const assign = assignValuesToPOJOFunction(); * const target = { a: 1, b: 2 }; * const result = assign(target, { a: 10, b: undefined }); * // result is { a: 10, b: 2 }, target is unchanged * * // With NULL filter and no copy: * const assignNoNulls = assignValuesToPOJOFunction({ valueFilter: KeyValueTypleValueFilter.NULL, copy: false }); * ``` * * @__NO_SIDE_EFFECTS__ */ export declare function assignValuesToPOJOFunction(input?: AssignValuesToPOJOFunctionInput): AssignValuesToPOJOFunction; /** * Extracts values from the POJO whose key/value pairs pass the given filter, returning them as an array. * * By default, only non-`undefined` values are included (`KeyValueTypleValueFilter.UNDEFINED`). * * @param target - The object to extract values from. * @param filter - A {@link FilterKeyValueTuplesInput} controlling which values are included. Defaults to `KeyValueTypleValueFilter.UNDEFINED`. * @returns Array of values whose key/value pairs passed the filter. * * @example * ```typescript * valuesFromPOJO({ a: 1, b: undefined, c: 'hello' }); * // [1, 'hello'] * * valuesFromPOJO({ a: 1, b: null, c: 'hello' }, KeyValueTypleValueFilter.NULL); * // [1, 'hello'] — null excluded * ``` */ export declare function valuesFromPOJO(target: I, filter?: FilterKeyValueTuplesInput): O[]; /** * Function that extracts values from a POJO whose key/value pairs pass a pre-configured filter. */ export type ValuesFromPOJOFunction = (obj: I) => O[]; /** * Creates a reusable {@link ValuesFromPOJOFunction} with a pre-configured filter. * * The returned function iterates each key/value pair on the input object, collects values * from pairs that pass the filter, and returns them as an array. * * By default, only non-`undefined` values are included (`KeyValueTypleValueFilter.UNDEFINED`). * * @param filter - A {@link FilterKeyValueTuplesInput} controlling which values are included. Defaults to `KeyValueTypleValueFilter.UNDEFINED`. * @returns A reusable function that extracts matching values from any input object. * * @dbxUtil * @dbxUtilCategory object * @dbxUtilKind factory * @dbxUtilTags object, pojo, values, factory, filter, extract * @dbxUtilRelated values-from-pojo, find-pojo-keys-function * * @example * ```typescript * const getDefinedValues = valuesFromPOJOFunction(); * getDefinedValues({ a: 1, b: undefined, c: 'test' }); * // [1, 'test'] * * const getNonNullValues = valuesFromPOJOFunction(KeyValueTypleValueFilter.NULL); * getNonNullValues({ a: 1, b: null, c: undefined }); * // [1] * ``` * * @__NO_SIDE_EFFECTS__ */ export declare function valuesFromPOJOFunction(filter?: FilterKeyValueTuplesInput): ValuesFromPOJOFunction; /** * Creates a {@link FilterTuplesOnPOJOFunction} that retains only keys present in `keysToFilter`, * or excludes them if `invertFilter` is `true`. * * Always returns a new object (never mutates the input). * * @param keysToFilter - The set of keys to include (or exclude when inverted). * @param invertFilter - When `true`, keys in `keysToFilter` are excluded instead of included. Defaults to `false`. * @returns Filters keys on any input POJO. * * @dbxUtil * @dbxUtilCategory object * @dbxUtilKind factory * @dbxUtilTags object, pojo, keys, filter, pick, omit, factory * @dbxUtilRelated filter-tuples-on-pojo-function, filter-from-pojo-function * * @example * ```typescript * const pickAB = filterKeysOnPOJOFunction(['a', 'b']); * pickAB({ a: 1, b: 2, c: 3 }); * // { a: 1, b: 2 } * * const omitAB = filterKeysOnPOJOFunction(['a', 'b'], true); * omitAB({ a: 1, b: 2, c: 3 }); * // { c: 3 } * ``` * * @__NO_SIDE_EFFECTS__ */ export declare function filterKeysOnPOJOFunction(keysToFilter: Iterable, invertFilter?: boolean): FilterTuplesOnPOJOFunction; /** * Filter predicate type used with {@link Object.entries} to filter key/value pairs. */ export type FilterTuplesOnPOJOFilter = Parameters>['filter']>['0']; /** * Function that returns a new object containing only the key/value pairs that pass a pre-configured filter. * For `Record` types, returns `Record`. For other object types, returns `Partial`. */ export type FilterTuplesOnPOJOFunction = T extends Record ? (input: T) => Record : (input: T) => Partial; /** * Creates a {@link FilterTuplesOnPOJOFunction} from a raw entry filter predicate. * * The returned function iterates all entries of the input object, applies the predicate, * and returns a new object containing only the entries that pass. * * @param filterTupleOnObject - Predicate applied to each `[key, value]` entry. * @returns Filters entries on any input POJO. * * @dbxUtil * @dbxUtilCategory object * @dbxUtilKind factory * @dbxUtilTags object, pojo, tuples, filter, predicate, factory * @dbxUtilRelated filter-keys-on-pojo-function, filter-from-pojo-function * * @example * ```typescript * const keepStrings = filterTuplesOnPOJOFunction(([, value]) => typeof value === 'string'); * keepStrings({ a: 'hello', b: 42, c: 'world' }); * // { a: 'hello', c: 'world' } * ``` * * @__NO_SIDE_EFFECTS__ */ export declare function filterTuplesOnPOJOFunction(filterTupleOnObject: FilterTuplesOnPOJOFilter): FilterTuplesOnPOJOFunction; /** * Callback invoked for each matching key/value pair during iteration. * * @param tuple - The `[key, value]` pair. * @param index - The index of the tuple among filtered results. * @param object - The original object being iterated. * @param context - An optional context value passed through from the caller. */ export type ForEachKeyValueOnPOJOTupleFunction = (tuple: KeyValueTuple, index: number, object: T, context: C) => void; /** * Function that iterates filtered key/value pairs on a POJO. * When context type `C` is `void`, no context argument is needed. Otherwise, a context must be provided. */ export type ForEachKeyValueOnPOJOFunction = C extends void ? ForEachKeyValueOnPOJOFunctionWithoutContext : ForEachKeyValueOnPOJOFunctionWithContext; /** * Variant that takes only the object (no context). */ export type ForEachKeyValueOnPOJOFunctionWithoutContext = (object: T) => void; /** * Variant that takes both the object and a context value. */ export type ForEachKeyValueOnPOJOFunctionWithContext = (object: T, context: C) => void; /** * Configuration for {@link forEachKeyValueOnPOJOFunction}. */ export type ForEachKeyValueOnPOJOConfig = { /** * Optional filter controlling which key/value pairs are iterated. * When not provided, all key/value pairs are iterated. */ filter?: FilterKeyValueTuplesInput; /** * Callback invoked for each matching key/value pair. */ forEach: ForEachKeyValueOnPOJOTupleFunction; }; /** * Creates a reusable function that iterates over filtered key/value pairs of a POJO, * invoking a callback for each matching pair. * * This is the low-level building block used by {@link filterFromPOJOFunction}, {@link findPOJOKeysFunction}, * {@link countPOJOKeysFunction}, {@link assignValuesToPOJOFunction}, and {@link valuesFromPOJOFunction}. * * When no filter is provided, all key/value pairs are iterated. * * @param config - The filter and forEach callback configuration. * @param config.forEach - Callback invoked for each key/value pair that passes the filter. * @param config.filter - Optional filter controlling which key/value pairs are iterated; when omitted, all pairs are visited. * @returns Iterates matching key/value pairs on any input object. * * @dbxUtil * @dbxUtilCategory object * @dbxUtilKind factory * @dbxUtilTags object, pojo, for-each, iterate, factory, callback * @dbxUtilRelated find-pojo-keys-function, count-pojo-keys-function, filter-key-value-tuples-function * * @example * ```typescript * const logDefined = forEachKeyValueOnPOJOFunction, void>({ * filter: KeyValueTypleValueFilter.UNDEFINED, * forEach: ([key, value]) => console.log(key, value) * }); * logDefined({ a: 1, b: undefined, c: 'test' }); * // logs: 'a' 1, 'c' 'test' * ``` * * @__NO_SIDE_EFFECTS__ */ export declare function forEachKeyValueOnPOJOFunction({ forEach, filter }: ForEachKeyValueOnPOJOConfig): ForEachKeyValueOnPOJOFunction;