@dereekb/util 13.0.5 → 13.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/fetch/package.json +2 -2
- package/index.cjs.js +387 -58
- package/index.esm.js +387 -58
- package/package.json +1 -1
- package/src/lib/object/object.filter.pojo.d.ts +476 -62
- package/test/package.json +2 -2
|
@@ -2,18 +2,53 @@ import { type ArrayOrValue } from './../array/array';
|
|
|
2
2
|
import { type Maybe } from '../value/maybe.type';
|
|
3
3
|
import { type FilterKeyValueTuplesInput, type KeyValueTuple, type KeyValueTupleFilter, KeyValueTypleValueFilter } from './object.filter.tuple';
|
|
4
4
|
/**
|
|
5
|
-
* Assigns all
|
|
5
|
+
* Assigns all non-filtered values from one or more source objects into the target object.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
7
|
+
* Builds a template from the source objects (in order, so later sources win) and applies it to the target.
|
|
8
|
+
* By default, undefined values in the source objects are excluded from the template, so they will not override existing target values.
|
|
9
|
+
*
|
|
10
|
+
* @param target - The object to override values on.
|
|
11
|
+
* @param config - Configuration for the override operation.
|
|
12
|
+
* @param config.copy - Whether to return a shallow copy instead of mutating the target. Defaults to `false`.
|
|
13
|
+
* @param config.from - One or more source objects whose values will be applied to the target.
|
|
14
|
+
* @param config.filter - Optional filter to control which key/value pairs from sources are included. Defaults to filtering out `undefined` values.
|
|
15
|
+
* @returns The modified target (or a copy if `copy` is `true`).
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* const target = { a: 1, b: 2 };
|
|
20
|
+
* overrideInObject(target, { from: [{ a: 10, c: 3 }] });
|
|
21
|
+
* // target is now { a: 10, b: 2, c: 3 }
|
|
22
|
+
*
|
|
23
|
+
* // undefined values in source are ignored by default:
|
|
24
|
+
* overrideInObject(target, { from: [{ a: undefined, b: 99 }] });
|
|
25
|
+
* // target.a remains 10, target.b becomes 99
|
|
26
|
+
* ```
|
|
8
27
|
*/
|
|
9
28
|
export declare function overrideInObject<T extends object>(target: Partial<T>, { copy, from, filter }: {
|
|
10
29
|
copy?: boolean;
|
|
11
30
|
from: ArrayOrValue<Partial<T>>;
|
|
12
31
|
filter?: KeyValueTupleFilter<T>;
|
|
13
32
|
}): Partial<T>;
|
|
33
|
+
/**
|
|
34
|
+
* Applies a pre-built template of values to a target object, returning the modified target.
|
|
35
|
+
*/
|
|
14
36
|
export type OverrideInObjectFunction<T> = (target: Partial<T>) => Partial<T>;
|
|
37
|
+
/**
|
|
38
|
+
* Factory that takes an array of source objects and produces an {@link OverrideInObjectFunction}
|
|
39
|
+
* that applies the merged template to any target.
|
|
40
|
+
*/
|
|
15
41
|
export type OverrideInObjectFunctionFactory<T> = (from: Partial<T>[]) => OverrideInObjectFunction<T>;
|
|
42
|
+
/**
|
|
43
|
+
* Configuration for {@link overrideInObjectFunctionFactory}.
|
|
44
|
+
*/
|
|
16
45
|
export interface OverrideInObjectFunctionFactoryConfig<T extends object> {
|
|
46
|
+
/**
|
|
47
|
+
* Filter controlling which key/value pairs from sources are included in the template.
|
|
48
|
+
*
|
|
49
|
+
* Accepts a {@link KeyValueTypleValueFilter} enum value or a {@link KeyValueTupleFilter} object.
|
|
50
|
+
* When not provided, the default filter removes `undefined` values (i.e., `KeyValueTypleValueFilter.UNDEFINED`).
|
|
51
|
+
*/
|
|
17
52
|
filter?: FilterKeyValueTuplesInput<T>;
|
|
18
53
|
/**
|
|
19
54
|
* Whether or not to return a copy of the input value, rather than change it directly.
|
|
@@ -32,183 +67,562 @@ export interface OverrideInObjectFunctionFactoryConfig<T extends object> {
|
|
|
32
67
|
*/
|
|
33
68
|
dynamic?: boolean;
|
|
34
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Creates an {@link OverrideInObjectFunctionFactory} that merges source objects into a template,
|
|
72
|
+
* then applies that template to target objects.
|
|
73
|
+
*
|
|
74
|
+
* The template is built by iterating the source objects in order (later sources override earlier ones),
|
|
75
|
+
* filtering each through the configured filter. When applied to a target, the template's values overwrite
|
|
76
|
+
* corresponding keys on the target.
|
|
77
|
+
*
|
|
78
|
+
* By default, `undefined` values in sources are excluded from the template.
|
|
79
|
+
*
|
|
80
|
+
* @param config - Configuration controlling filtering, copying, and caching behavior.
|
|
81
|
+
* @returns A factory function that accepts source objects and returns an override function.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* const factory = overrideInObjectFunctionFactory({ copy: true });
|
|
86
|
+
* const overrideFn = factory([{ color: 'red' }, { size: 10 }]);
|
|
87
|
+
* const result = overrideFn({ color: 'blue', size: 5 });
|
|
88
|
+
* // result is { color: 'red', size: 10 } (a new copy)
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
35
91
|
export declare function overrideInObjectFunctionFactory<T extends object>({ filter, copy, dynamic }: OverrideInObjectFunctionFactoryConfig<T>): OverrideInObjectFunctionFactory<T>;
|
|
36
92
|
/**
|
|
37
|
-
* Merges all input objects into
|
|
93
|
+
* Merges all input objects into a single object.
|
|
94
|
+
*
|
|
95
|
+
* Objects are applied left-to-right, so the right-most (last) item in the array has the highest priority.
|
|
96
|
+
* `Maybe` values (null/undefined entries in the array) are silently ignored.
|
|
97
|
+
*
|
|
98
|
+
* By default, `undefined` values within each source object are excluded from the merge,
|
|
99
|
+
* meaning an `undefined` value will not overwrite a previously set value.
|
|
38
100
|
*
|
|
39
|
-
*
|
|
101
|
+
* @param objects - Array of objects (or null/undefined) to merge together.
|
|
102
|
+
* @param filter - Optional filter controlling which key/value pairs are included. Defaults to filtering out `undefined` values (`KeyValueTypleValueFilter.UNDEFINED`).
|
|
103
|
+
* @returns A new object containing the merged result.
|
|
40
104
|
*
|
|
41
|
-
* @
|
|
105
|
+
* @example
|
|
106
|
+
* ```typescript
|
|
107
|
+
* const result = mergeObjects([{ a: 1, b: 2 }, { b: 3, c: 4 }]);
|
|
108
|
+
* // result is { a: 1, b: 3, c: 4 }
|
|
109
|
+
*
|
|
110
|
+
* // undefined values in sources do not override:
|
|
111
|
+
* const result2 = mergeObjects([{ a: 1 }, { a: undefined, b: 2 }]);
|
|
112
|
+
* // result2 is { a: 1, b: 2 }
|
|
113
|
+
* ```
|
|
42
114
|
*/
|
|
43
115
|
export declare function mergeObjects<T extends object>(objects: Maybe<Partial<T>>[], filter?: FilterKeyValueTuplesInput<T>): Partial<T>;
|
|
44
116
|
/**
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
* The order of overrides is kept, so the right-most item in the array will have priority over all objects before it.
|
|
117
|
+
* Function that merges an array of partial objects into a single object.
|
|
118
|
+
* Objects are applied left-to-right, so the last item in the array has the highest priority.
|
|
48
119
|
*/
|
|
49
120
|
export type MergeObjectsFunction<T extends object> = (objects: Maybe<Partial<T>>[]) => Partial<T>;
|
|
121
|
+
/**
|
|
122
|
+
* Creates a reusable {@link MergeObjectsFunction} with a pre-configured filter.
|
|
123
|
+
*
|
|
124
|
+
* Useful when you need to merge objects multiple times with the same filter configuration.
|
|
125
|
+
* By default, `undefined` values are filtered out of the result (`KeyValueTypleValueFilter.UNDEFINED`).
|
|
126
|
+
*
|
|
127
|
+
* @param filter - Optional filter controlling which key/value pairs are included. Defaults to filtering out `undefined` values.
|
|
128
|
+
* @returns A reusable merge function.
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* ```typescript
|
|
132
|
+
* const merge = mergeObjectsFunction();
|
|
133
|
+
* const result = merge([{ a: 1 }, { b: 2 }, { a: 3 }]);
|
|
134
|
+
* // result is { a: 3, b: 2 }
|
|
135
|
+
*
|
|
136
|
+
* // With null filter to also exclude null values:
|
|
137
|
+
* const mergeNoNulls = mergeObjectsFunction(KeyValueTypleValueFilter.NULL);
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
50
140
|
export declare function mergeObjectsFunction<T extends object>(filter?: FilterKeyValueTuplesInput<T>): MergeObjectsFunction<T>;
|
|
51
141
|
/**
|
|
52
|
-
* Returns a copy of the input object with all undefined
|
|
142
|
+
* Returns a copy of the input object with all `undefined` values removed.
|
|
143
|
+
* When `filterNull` is `true`, `null` values are also removed.
|
|
53
144
|
*
|
|
54
|
-
* @
|
|
55
|
-
*
|
|
145
|
+
* This is a convenience wrapper around {@link filterOnlyUndefinedValues} and {@link filterNullAndUndefinedValues}.
|
|
146
|
+
*
|
|
147
|
+
* @param obj - The object to filter.
|
|
148
|
+
* @param filterNull - If `true`, both `null` and `undefined` values are removed. Defaults to `false`.
|
|
149
|
+
* @returns A shallow copy of the object with filtered values removed.
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```typescript
|
|
153
|
+
* filterUndefinedValues({ a: 1, b: undefined, c: null });
|
|
154
|
+
* // { a: 1, c: null }
|
|
155
|
+
*
|
|
156
|
+
* filterUndefinedValues({ a: 1, b: undefined, c: null }, true);
|
|
157
|
+
* // { a: 1 }
|
|
158
|
+
* ```
|
|
56
159
|
*/
|
|
57
160
|
export declare function filterUndefinedValues<T extends object>(obj: T, filterNull?: boolean): T;
|
|
58
161
|
/**
|
|
59
|
-
*
|
|
162
|
+
* Pre-built filter that returns a copy of the input object with all `undefined` values removed.
|
|
163
|
+
* Keys with `null` or other falsy values are retained.
|
|
60
164
|
*
|
|
61
|
-
* @
|
|
62
|
-
*
|
|
165
|
+
* @example
|
|
166
|
+
* ```typescript
|
|
167
|
+
* filterOnlyUndefinedValues({ a: 1, b: undefined, c: null });
|
|
168
|
+
* // { a: 1, c: null }
|
|
169
|
+
* ```
|
|
63
170
|
*/
|
|
64
171
|
export declare const filterOnlyUndefinedValues: GeneralFilterFromPOJOFunction;
|
|
65
172
|
/**
|
|
66
|
-
*
|
|
173
|
+
* Pre-built filter that returns a copy of the input object with all `null` and `undefined` values removed.
|
|
174
|
+
* Keys with other falsy values (0, false, '') are retained.
|
|
67
175
|
*
|
|
68
|
-
* @
|
|
69
|
-
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```typescript
|
|
178
|
+
* filterNullAndUndefinedValues({ a: 1, b: undefined, c: null, d: 0 });
|
|
179
|
+
* // { a: 1, d: 0 }
|
|
180
|
+
* ```
|
|
70
181
|
*/
|
|
71
182
|
export declare const filterNullAndUndefinedValues: GeneralFilterFromPOJOFunction;
|
|
72
183
|
/**
|
|
73
|
-
*
|
|
184
|
+
* Pre-built filter that returns a copy of the input object with all empty values removed.
|
|
185
|
+
* Empty values include `null`, `undefined`, empty strings (`''`), empty arrays (`[]`), and empty objects (`{}`).
|
|
74
186
|
*
|
|
75
|
-
* @
|
|
76
|
-
*
|
|
187
|
+
* @example
|
|
188
|
+
* ```typescript
|
|
189
|
+
* filterEmptyPojoValues({ a: 1, b: '', c: [], d: null, e: 'hello' });
|
|
190
|
+
* // { a: 1, e: 'hello' }
|
|
191
|
+
* ```
|
|
77
192
|
*/
|
|
78
193
|
export declare const filterEmptyPojoValues: GeneralFilterFromPOJOFunction;
|
|
79
194
|
/**
|
|
80
|
-
*
|
|
195
|
+
* Pre-built filter that returns a copy of the input object with all falsy and empty values removed.
|
|
196
|
+
* Removes `null`, `undefined`, `0`, `false`, `''`, empty arrays, and empty objects.
|
|
81
197
|
*
|
|
82
|
-
* @
|
|
83
|
-
*
|
|
198
|
+
* @example
|
|
199
|
+
* ```typescript
|
|
200
|
+
* filterFalsyAndEmptyValues({ a: 1, b: false, c: 0, d: '', e: 'hello' });
|
|
201
|
+
* // { a: 1, e: 'hello' }
|
|
202
|
+
* ```
|
|
84
203
|
*/
|
|
85
204
|
export declare const filterFalsyAndEmptyValues: GeneralFilterFromPOJOFunction;
|
|
86
205
|
/**
|
|
87
|
-
*
|
|
206
|
+
* Pre-built function that returns all keys from a POJO whose values are not `undefined`.
|
|
207
|
+
* Keys with `null` or other falsy values are included in the result.
|
|
88
208
|
*
|
|
89
|
-
* @
|
|
90
|
-
*
|
|
209
|
+
* @example
|
|
210
|
+
* ```typescript
|
|
211
|
+
* allNonUndefinedKeys({ a: 'test', b: undefined, c: null, d: 0 });
|
|
212
|
+
* // ['a', 'c', 'd']
|
|
213
|
+
* ```
|
|
91
214
|
*/
|
|
92
215
|
export declare const allNonUndefinedKeys: GeneralFindPOJOKeysFunction;
|
|
93
216
|
/**
|
|
94
|
-
*
|
|
217
|
+
* Pre-built function that returns all keys from a POJO whose values are not falsy or empty.
|
|
218
|
+
* Excludes keys with `null`, `undefined`, `0`, `false`, `''`, empty arrays, or empty objects.
|
|
95
219
|
*
|
|
96
|
-
* @
|
|
97
|
-
*
|
|
220
|
+
* @example
|
|
221
|
+
* ```typescript
|
|
222
|
+
* allFalsyOrEmptyKeys({ a: 'test', b: false, c: 0, d: null });
|
|
223
|
+
* // ['a']
|
|
224
|
+
* ```
|
|
98
225
|
*/
|
|
99
226
|
export declare const allFalsyOrEmptyKeys: GeneralFindPOJOKeysFunction;
|
|
100
227
|
/**
|
|
101
|
-
*
|
|
228
|
+
* Pre-built function that returns all keys from a POJO whose values are not `null` or `undefined`.
|
|
229
|
+
* Keys with other falsy values (0, false, '') are included.
|
|
102
230
|
*
|
|
103
|
-
* @
|
|
104
|
-
*
|
|
231
|
+
* @example
|
|
232
|
+
* ```typescript
|
|
233
|
+
* allMaybeSoKeys({ a: 'test', b: undefined, c: null, d: 0 });
|
|
234
|
+
* // ['a', 'd']
|
|
235
|
+
* ```
|
|
105
236
|
*/
|
|
106
237
|
export declare const allMaybeSoKeys: GeneralFindPOJOKeysFunction;
|
|
107
238
|
/**
|
|
108
|
-
* Finds keys from the POJO
|
|
239
|
+
* Finds and returns keys from the POJO whose values pass the given filter.
|
|
240
|
+
*
|
|
241
|
+
* The filter determines which values are considered "matching" — matched values have their keys included in the result.
|
|
242
|
+
* For example, using `KeyValueTypleValueFilter.UNDEFINED` returns all keys whose values are NOT `undefined`.
|
|
109
243
|
*
|
|
110
|
-
* @param obj
|
|
111
|
-
* @param filter
|
|
112
|
-
* @returns
|
|
244
|
+
* @param obj - The object to inspect.
|
|
245
|
+
* @param filter - A {@link FilterKeyValueTuplesInput} controlling which key/value pairs match. Required (no default).
|
|
246
|
+
* @returns Array of keys whose values pass the filter.
|
|
247
|
+
*
|
|
248
|
+
* @example
|
|
249
|
+
* ```typescript
|
|
250
|
+
* findPOJOKeys({ a: 1, b: undefined, c: null }, KeyValueTypleValueFilter.NULL);
|
|
251
|
+
* // ['a'] — only 'a' has a non-null, non-undefined value
|
|
252
|
+
* ```
|
|
113
253
|
*/
|
|
114
254
|
export declare function findPOJOKeys<T extends object>(obj: T, filter: FilterKeyValueTuplesInput<T>): (keyof T)[];
|
|
115
255
|
/**
|
|
116
|
-
*
|
|
256
|
+
* Function that returns an array of keys from a POJO whose values pass a pre-configured filter.
|
|
117
257
|
*/
|
|
118
258
|
export type FindPOJOKeysFunction<T> = (obj: T) => (keyof T)[];
|
|
259
|
+
/**
|
|
260
|
+
* Generic version of {@link FindPOJOKeysFunction} that accepts any object type.
|
|
261
|
+
*/
|
|
119
262
|
export type GeneralFindPOJOKeysFunction = <T extends object>(obj: T) => (keyof T)[];
|
|
263
|
+
/**
|
|
264
|
+
* Creates a reusable {@link FindPOJOKeysFunction} with a pre-configured filter.
|
|
265
|
+
*
|
|
266
|
+
* The returned function inspects each key/value pair on the input object and collects keys
|
|
267
|
+
* whose values pass the filter.
|
|
268
|
+
*
|
|
269
|
+
* @param filter - A {@link FilterKeyValueTuplesInput} controlling which values match. Required (no default).
|
|
270
|
+
* @returns A reusable function that returns matching keys from any input object.
|
|
271
|
+
*
|
|
272
|
+
* @example
|
|
273
|
+
* ```typescript
|
|
274
|
+
* const findDefinedKeys = findPOJOKeysFunction(KeyValueTypleValueFilter.UNDEFINED);
|
|
275
|
+
* findDefinedKeys({ a: 1, b: undefined, c: 'hello' });
|
|
276
|
+
* // ['a', 'c']
|
|
277
|
+
* ```
|
|
278
|
+
*/
|
|
120
279
|
export declare function findPOJOKeysFunction<T extends object>(filter: FilterKeyValueTuplesInput<T>): FindPOJOKeysFunction<T>;
|
|
121
280
|
/**
|
|
122
|
-
*
|
|
281
|
+
* Counts the number of keys on the POJO whose values pass the given filter.
|
|
123
282
|
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
* @
|
|
283
|
+
* By default, counts all keys whose values are not `undefined` (`KeyValueTypleValueFilter.UNDEFINED`).
|
|
284
|
+
*
|
|
285
|
+
* @param obj - The object to inspect.
|
|
286
|
+
* @param filter - A {@link FilterKeyValueTuplesInput} controlling which values are counted. Defaults to `KeyValueTypleValueFilter.UNDEFINED`.
|
|
287
|
+
* @returns The number of keys whose values pass the filter.
|
|
288
|
+
*
|
|
289
|
+
* @example
|
|
290
|
+
* ```typescript
|
|
291
|
+
* countPOJOKeys({ a: 1, b: undefined, c: null });
|
|
292
|
+
* // 2 — 'a' and 'c' are not undefined
|
|
293
|
+
*
|
|
294
|
+
* countPOJOKeys({ a: 1, b: undefined, c: null }, KeyValueTypleValueFilter.NULL);
|
|
295
|
+
* // 1 — only 'a' is not null or undefined
|
|
296
|
+
* ```
|
|
127
297
|
*/
|
|
128
298
|
export declare function countPOJOKeys<T extends object>(obj: T, filter?: FilterKeyValueTuplesInput<T>): number;
|
|
129
299
|
/**
|
|
130
|
-
*
|
|
300
|
+
* Function that counts the number of keys on an object whose values pass a pre-configured filter.
|
|
131
301
|
*/
|
|
132
302
|
export type CountPOJOKeysFunction<T> = (obj: T) => number;
|
|
303
|
+
/**
|
|
304
|
+
* Creates a reusable {@link CountPOJOKeysFunction} with a pre-configured filter.
|
|
305
|
+
*
|
|
306
|
+
* By default, counts all keys whose values are not `undefined` (`KeyValueTypleValueFilter.UNDEFINED`).
|
|
307
|
+
*
|
|
308
|
+
* @param filter - A {@link FilterKeyValueTuplesInput} controlling which values are counted. Defaults to `KeyValueTypleValueFilter.UNDEFINED`.
|
|
309
|
+
* @returns A reusable function that counts matching keys on any input object.
|
|
310
|
+
*
|
|
311
|
+
* @example
|
|
312
|
+
* ```typescript
|
|
313
|
+
* const countDefined = countPOJOKeysFunction();
|
|
314
|
+
* countDefined({ a: 1, b: undefined, c: 'test' });
|
|
315
|
+
* // 2
|
|
316
|
+
* ```
|
|
317
|
+
*/
|
|
133
318
|
export declare function countPOJOKeysFunction<T extends object>(filter?: FilterKeyValueTuplesInput<T>): CountPOJOKeysFunction<T>;
|
|
319
|
+
/**
|
|
320
|
+
* Configuration for {@link filterFromPOJO} and {@link filterFromPOJOFunction}.
|
|
321
|
+
*/
|
|
134
322
|
export interface FilterFromPOJO<T extends object> {
|
|
323
|
+
/**
|
|
324
|
+
* Whether to return a shallow copy of the object instead of mutating the original.
|
|
325
|
+
* Defaults to `false` when used via {@link filterFromPOJOFunction}, but the pre-built
|
|
326
|
+
* constants (e.g., {@link filterOnlyUndefinedValues}) use `true`.
|
|
327
|
+
*/
|
|
135
328
|
copy?: boolean;
|
|
329
|
+
/**
|
|
330
|
+
* Filter determining which values to remove. Accepts a {@link KeyValueTypleValueFilter} enum or a {@link KeyValueTupleFilter} object (without `inverse`).
|
|
331
|
+
* Defaults to `{ valueFilter: KeyValueTypleValueFilter.UNDEFINED }`, which removes `undefined` values.
|
|
332
|
+
*/
|
|
136
333
|
filter?: Omit<FilterKeyValueTuplesInput<T>, 'inverse'>;
|
|
137
334
|
}
|
|
138
335
|
/**
|
|
139
|
-
* Removes values
|
|
336
|
+
* Removes values from the input object based on the filter configuration.
|
|
337
|
+
*
|
|
338
|
+
* By default, removes `undefined` values and does NOT copy the object (mutates in place).
|
|
339
|
+
* Pass `{ copy: true }` to get a new object without modifying the original.
|
|
340
|
+
*
|
|
341
|
+
* @param obj - The POJO to filter values from.
|
|
342
|
+
* @param config - Configuration for filtering and copying behavior. Defaults to removing `undefined` values without copying.
|
|
343
|
+
* @returns The filtered object (either the mutated original or a shallow copy, depending on `config.copy`).
|
|
140
344
|
*
|
|
141
|
-
* @
|
|
142
|
-
*
|
|
345
|
+
* @example
|
|
346
|
+
* ```typescript
|
|
347
|
+
* // Remove undefined values (default):
|
|
348
|
+
* filterFromPOJO({ a: 1, b: undefined, c: 'hello' });
|
|
349
|
+
* // { a: 1, c: 'hello' }
|
|
350
|
+
*
|
|
351
|
+
* // Remove null and undefined values:
|
|
352
|
+
* filterFromPOJO({ a: 1, b: null, c: undefined }, { filter: { valueFilter: KeyValueTypleValueFilter.NULL } });
|
|
353
|
+
* // { a: 1 }
|
|
354
|
+
* ```
|
|
143
355
|
*/
|
|
144
356
|
export declare function filterFromPOJO<T extends object>(obj: T, config?: FilterFromPOJO<T>): T;
|
|
145
357
|
/**
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
* @param obj POJO to remove undefined values from.
|
|
149
|
-
* @param copy Whether or not to return a copy of the input object. Default is true.
|
|
358
|
+
* Function that removes filtered values from an input object.
|
|
359
|
+
* Accepts an optional `copyOverride` parameter to control copy behavior per-call.
|
|
150
360
|
*/
|
|
151
361
|
export type FilterFromPOJOFunction<T> = (input: T, copyOverride?: Maybe<boolean>) => T;
|
|
362
|
+
/**
|
|
363
|
+
* Generic version of {@link FilterFromPOJOFunction} that accepts any object type.
|
|
364
|
+
*/
|
|
152
365
|
export type GeneralFilterFromPOJOFunction<X = object> = <T extends X>(input: T) => T;
|
|
366
|
+
/**
|
|
367
|
+
* Creates a reusable {@link FilterFromPOJOFunction} with a pre-configured filter and copy behavior.
|
|
368
|
+
*
|
|
369
|
+
* The returned function removes key/value pairs that match the filter from the input object.
|
|
370
|
+
* Internally, the filter is inverted so that matching values are deleted while non-matching values are retained.
|
|
371
|
+
*
|
|
372
|
+
* By default, removes `undefined` values (`KeyValueTypleValueFilter.UNDEFINED`) and does not copy (`copy: false`).
|
|
373
|
+
*
|
|
374
|
+
* @param config - Configuration for filtering and copying. Defaults to `{ copy: false, filter: { valueFilter: KeyValueTypleValueFilter.UNDEFINED } }`.
|
|
375
|
+
* @returns A reusable filter function. The returned function also accepts a `copyOverride` argument to override the copy behavior per-call.
|
|
376
|
+
*
|
|
377
|
+
* @example
|
|
378
|
+
* ```typescript
|
|
379
|
+
* // Default: removes undefined values, mutates in place
|
|
380
|
+
* const filterUndef = filterFromPOJOFunction();
|
|
381
|
+
* const obj = { a: 1, b: undefined };
|
|
382
|
+
* filterUndef(obj); // obj is now { a: 1 }
|
|
383
|
+
*
|
|
384
|
+
* // With copy and null filter:
|
|
385
|
+
* const filterNulls = filterFromPOJOFunction({ copy: true, filter: { valueFilter: KeyValueTypleValueFilter.NULL } });
|
|
386
|
+
* const result = filterNulls({ a: 1, b: null, c: undefined });
|
|
387
|
+
* // result is { a: 1 }, original is unchanged
|
|
388
|
+
* ```
|
|
389
|
+
*/
|
|
153
390
|
export declare function filterFromPOJOFunction<T extends object>({ copy, filter: inputFilter }?: FilterFromPOJO<T>): FilterFromPOJOFunction<T>;
|
|
154
391
|
/**
|
|
155
|
-
*
|
|
392
|
+
* Pre-built {@link FilterFromPOJOFunction} that removes `undefined` values by mutating the input object (no copy).
|
|
393
|
+
*
|
|
394
|
+
* Used internally as the default filter for {@link overrideInObjectFunctionFactory} when no filter is provided.
|
|
395
|
+
*
|
|
396
|
+
* @example
|
|
397
|
+
* ```typescript
|
|
398
|
+
* const obj = { a: 1, b: undefined };
|
|
399
|
+
* defaultFilterFromPOJOFunctionNoCopy(obj);
|
|
400
|
+
* // obj is now { a: 1 }
|
|
401
|
+
* ```
|
|
156
402
|
*/
|
|
157
403
|
export declare const defaultFilterFromPOJOFunctionNoCopy: FilterFromPOJOFunction<object>;
|
|
404
|
+
/**
|
|
405
|
+
* Assigns filtered values from `obj` onto `target`.
|
|
406
|
+
*
|
|
407
|
+
* By default, only non-`undefined` values from `obj` are assigned, and a copy of `target` is returned (the original is not mutated).
|
|
408
|
+
*
|
|
409
|
+
* @param target - The object to assign values onto.
|
|
410
|
+
* @param obj - The source object whose matching values will be assigned.
|
|
411
|
+
* @param input - Optional filter/copy configuration. Defaults to `KeyValueTypleValueFilter.UNDEFINED` with `copy: true`.
|
|
412
|
+
* @returns The target with values assigned (either a copy or the original, depending on configuration).
|
|
413
|
+
*
|
|
414
|
+
* @example
|
|
415
|
+
* ```typescript
|
|
416
|
+
* const target = { a: 1, b: 2 };
|
|
417
|
+
* const result = assignValuesToPOJO(target, { a: 10, b: undefined });
|
|
418
|
+
* // result is { a: 10, b: 2 } (copy), target unchanged
|
|
419
|
+
* ```
|
|
420
|
+
*/
|
|
158
421
|
export declare function assignValuesToPOJO<T extends object, K extends keyof T = keyof T>(target: T, obj: T, input?: AssignValuesToPOJOFunctionInput<T, K>): T;
|
|
422
|
+
/**
|
|
423
|
+
* Overload that always returns a copy of the target with values assigned.
|
|
424
|
+
*/
|
|
159
425
|
export type AssignValuesToPOJOCopyFunction<T> = (target: T, obj: T, returnCopy: true) => T;
|
|
426
|
+
/**
|
|
427
|
+
* Overload that mutates and returns the original target with values assigned.
|
|
428
|
+
*/
|
|
160
429
|
export type AssignValuesToPOJONoCopyFunction<T> = <I extends T>(target: I, obj: T, returnCopy: false) => I;
|
|
161
430
|
/**
|
|
162
|
-
*
|
|
431
|
+
* Function that assigns filtered values from a source object onto a target object.
|
|
163
432
|
*
|
|
164
|
-
*
|
|
433
|
+
* Accepts an optional third argument `returnCopy` to override the default copy behavior per-call.
|
|
434
|
+
* The `_returnCopyByDefault` property indicates the configured default copy behavior.
|
|
165
435
|
*/
|
|
166
436
|
export type AssignValuesToPOJOFunction<T> = ((target: T, obj: T, returnCopy?: boolean) => T) & AssignValuesToPOJOCopyFunction<T> & AssignValuesToPOJONoCopyFunction<T> & {
|
|
167
437
|
readonly _returnCopyByDefault: boolean;
|
|
168
438
|
};
|
|
439
|
+
/**
|
|
440
|
+
* Configuration for {@link assignValuesToPOJOFunction}.
|
|
441
|
+
*
|
|
442
|
+
* Can be a simple {@link KeyValueTypleValueFilter} enum value, or a {@link KeyValueTupleFilter} object
|
|
443
|
+
* with an additional `copy` property. When a simple enum value is provided, `copy` defaults to `true`.
|
|
444
|
+
*/
|
|
169
445
|
export type AssignValuesToPOJOFunctionInput<T extends object = object, K extends keyof T = keyof T> = KeyValueTypleValueFilter | (KeyValueTupleFilter<T, K> & {
|
|
170
446
|
/**
|
|
171
|
-
* Whether or not to copy the object before assigning values,
|
|
447
|
+
* Whether or not to copy the target object before assigning values, returning the new object.
|
|
172
448
|
*
|
|
173
449
|
* True by default.
|
|
174
450
|
*/
|
|
175
451
|
copy?: boolean;
|
|
176
452
|
});
|
|
177
453
|
/**
|
|
178
|
-
* Creates a AssignValuesToPOJOFunction
|
|
454
|
+
* Creates a reusable {@link AssignValuesToPOJOFunction} with a pre-configured filter and copy behavior.
|
|
455
|
+
*
|
|
456
|
+
* The returned function assigns key/value pairs from a source object onto a target, skipping pairs
|
|
457
|
+
* that are filtered out. By default, `undefined` values are filtered out (`KeyValueTypleValueFilter.UNDEFINED`)
|
|
458
|
+
* and the target is copied before assignment (`copy: true`).
|
|
459
|
+
*
|
|
460
|
+
* @param input - Filter and copy configuration. Defaults to `KeyValueTypleValueFilter.UNDEFINED` (filter undefined, copy target).
|
|
461
|
+
* @returns A reusable assign function with a `_returnCopyByDefault` property indicating the configured copy behavior.
|
|
179
462
|
*
|
|
180
|
-
* @
|
|
181
|
-
*
|
|
463
|
+
* @example
|
|
464
|
+
* ```typescript
|
|
465
|
+
* // Default: filters undefined, returns a copy
|
|
466
|
+
* const assign = assignValuesToPOJOFunction();
|
|
467
|
+
* const target = { a: 1, b: 2 };
|
|
468
|
+
* const result = assign(target, { a: 10, b: undefined });
|
|
469
|
+
* // result is { a: 10, b: 2 }, target is unchanged
|
|
470
|
+
*
|
|
471
|
+
* // With NULL filter and no copy:
|
|
472
|
+
* const assignNoNulls = assignValuesToPOJOFunction({ valueFilter: KeyValueTypleValueFilter.NULL, copy: false });
|
|
473
|
+
* ```
|
|
182
474
|
*/
|
|
183
475
|
export declare function assignValuesToPOJOFunction<T extends object, K extends keyof T = keyof T>(input?: AssignValuesToPOJOFunctionInput<T, K>): AssignValuesToPOJOFunction<T>;
|
|
184
476
|
/**
|
|
185
|
-
*
|
|
477
|
+
* Extracts values from the POJO whose key/value pairs pass the given filter, returning them as an array.
|
|
478
|
+
*
|
|
479
|
+
* By default, only non-`undefined` values are included (`KeyValueTypleValueFilter.UNDEFINED`).
|
|
480
|
+
*
|
|
481
|
+
* @param target - The object to extract values from.
|
|
482
|
+
* @param filter - A {@link FilterKeyValueTuplesInput} controlling which values are included. Defaults to `KeyValueTypleValueFilter.UNDEFINED`.
|
|
483
|
+
* @returns Array of values whose key/value pairs passed the filter.
|
|
484
|
+
*
|
|
485
|
+
* @example
|
|
486
|
+
* ```typescript
|
|
487
|
+
* valuesFromPOJO({ a: 1, b: undefined, c: 'hello' });
|
|
488
|
+
* // [1, 'hello']
|
|
489
|
+
*
|
|
490
|
+
* valuesFromPOJO({ a: 1, b: null, c: 'hello' }, KeyValueTypleValueFilter.NULL);
|
|
491
|
+
* // [1, 'hello'] — null excluded
|
|
492
|
+
* ```
|
|
186
493
|
*/
|
|
187
494
|
export declare function valuesFromPOJO<O = unknown, I extends object = object>(target: I, filter?: FilterKeyValueTuplesInput<I>): O[];
|
|
188
495
|
/**
|
|
189
|
-
*
|
|
496
|
+
* Function that extracts values from a POJO whose key/value pairs pass a pre-configured filter.
|
|
190
497
|
*/
|
|
191
498
|
export type ValuesFromPOJOFunction<O = unknown, I extends object = object> = (obj: I) => O[];
|
|
499
|
+
/**
|
|
500
|
+
* Creates a reusable {@link ValuesFromPOJOFunction} with a pre-configured filter.
|
|
501
|
+
*
|
|
502
|
+
* The returned function iterates each key/value pair on the input object, collects values
|
|
503
|
+
* from pairs that pass the filter, and returns them as an array.
|
|
504
|
+
*
|
|
505
|
+
* By default, only non-`undefined` values are included (`KeyValueTypleValueFilter.UNDEFINED`).
|
|
506
|
+
*
|
|
507
|
+
* @param filter - A {@link FilterKeyValueTuplesInput} controlling which values are included. Defaults to `KeyValueTypleValueFilter.UNDEFINED`.
|
|
508
|
+
* @returns A reusable function that extracts matching values from any input object.
|
|
509
|
+
*
|
|
510
|
+
* @example
|
|
511
|
+
* ```typescript
|
|
512
|
+
* const getDefinedValues = valuesFromPOJOFunction();
|
|
513
|
+
* getDefinedValues({ a: 1, b: undefined, c: 'test' });
|
|
514
|
+
* // [1, 'test']
|
|
515
|
+
*
|
|
516
|
+
* const getNonNullValues = valuesFromPOJOFunction(KeyValueTypleValueFilter.NULL);
|
|
517
|
+
* getNonNullValues({ a: 1, b: null, c: undefined });
|
|
518
|
+
* // [1]
|
|
519
|
+
* ```
|
|
520
|
+
*/
|
|
192
521
|
export declare function valuesFromPOJOFunction<O = unknown, I extends object = object>(filter?: FilterKeyValueTuplesInput<I>): ValuesFromPOJOFunction<O, I>;
|
|
193
522
|
/**
|
|
194
|
-
*
|
|
523
|
+
* Creates a {@link FilterTuplesOnPOJOFunction} that retains only keys present in `keysToFilter`,
|
|
524
|
+
* or excludes them if `invertFilter` is `true`.
|
|
195
525
|
*
|
|
196
|
-
*
|
|
197
|
-
*
|
|
526
|
+
* Always returns a new object (never mutates the input).
|
|
527
|
+
*
|
|
528
|
+
* @param keysToFilter - The set of keys to include (or exclude when inverted).
|
|
529
|
+
* @param invertFilter - When `true`, keys in `keysToFilter` are excluded instead of included. Defaults to `false`.
|
|
530
|
+
* @returns A function that filters keys on any input POJO.
|
|
531
|
+
*
|
|
532
|
+
* @example
|
|
533
|
+
* ```typescript
|
|
534
|
+
* const pickAB = filterKeysOnPOJOFunction(['a', 'b']);
|
|
535
|
+
* pickAB({ a: 1, b: 2, c: 3 });
|
|
536
|
+
* // { a: 1, b: 2 }
|
|
537
|
+
*
|
|
538
|
+
* const omitAB = filterKeysOnPOJOFunction(['a', 'b'], true);
|
|
539
|
+
* omitAB({ a: 1, b: 2, c: 3 });
|
|
540
|
+
* // { c: 3 }
|
|
541
|
+
* ```
|
|
198
542
|
*/
|
|
199
543
|
export declare function filterKeysOnPOJOFunction<T extends object>(keysToFilter: Iterable<string>, invertFilter?: boolean): FilterTuplesOnPOJOFunction<T>;
|
|
544
|
+
/**
|
|
545
|
+
* Filter predicate type used with {@link Object.entries} to filter key/value pairs.
|
|
546
|
+
*/
|
|
200
547
|
export type FilterTuplesOnPOJOFilter<T extends object> = Parameters<ReturnType<typeof Object.entries<T>>['filter']>['0'];
|
|
201
548
|
/**
|
|
202
|
-
* Function that
|
|
549
|
+
* Function that returns a new object containing only the key/value pairs that pass a pre-configured filter.
|
|
550
|
+
* For `Record<string, V>` types, returns `Record<string, V>`. For other object types, returns `Partial<T>`.
|
|
203
551
|
*/
|
|
204
552
|
export type FilterTuplesOnPOJOFunction<T extends object> = T extends Record<string, infer I> ? (input: T) => Record<string, I> : (input: T) => Partial<T>;
|
|
553
|
+
/**
|
|
554
|
+
* Creates a {@link FilterTuplesOnPOJOFunction} from a raw entry filter predicate.
|
|
555
|
+
*
|
|
556
|
+
* The returned function iterates all entries of the input object, applies the predicate,
|
|
557
|
+
* and returns a new object containing only the entries that pass.
|
|
558
|
+
*
|
|
559
|
+
* @param filterTupleOnObject - Predicate applied to each `[key, value]` entry.
|
|
560
|
+
* @returns A function that filters entries on any input POJO.
|
|
561
|
+
*
|
|
562
|
+
* @example
|
|
563
|
+
* ```typescript
|
|
564
|
+
* const keepStrings = filterTuplesOnPOJOFunction(([, value]) => typeof value === 'string');
|
|
565
|
+
* keepStrings({ a: 'hello', b: 42, c: 'world' });
|
|
566
|
+
* // { a: 'hello', c: 'world' }
|
|
567
|
+
* ```
|
|
568
|
+
*/
|
|
205
569
|
export declare function filterTuplesOnPOJOFunction<T extends object>(filterTupleOnObject: FilterTuplesOnPOJOFilter<T>): FilterTuplesOnPOJOFunction<T>;
|
|
570
|
+
/**
|
|
571
|
+
* Callback invoked for each matching key/value pair during iteration.
|
|
572
|
+
*
|
|
573
|
+
* @param tuple - The `[key, value]` pair.
|
|
574
|
+
* @param index - The index of the tuple among filtered results.
|
|
575
|
+
* @param object - The original object being iterated.
|
|
576
|
+
* @param context - An optional context value passed through from the caller.
|
|
577
|
+
*/
|
|
206
578
|
export type ForEachKeyValueOnPOJOTupleFunction<T extends object, C = unknown, K extends keyof T = keyof T> = (tuple: KeyValueTuple<T, K>, index: number, object: T, context: C) => void;
|
|
579
|
+
/**
|
|
580
|
+
* Function that iterates filtered key/value pairs on a POJO.
|
|
581
|
+
* When context type `C` is `void`, no context argument is needed. Otherwise, a context must be provided.
|
|
582
|
+
*/
|
|
207
583
|
export type ForEachKeyValueOnPOJOFunction<T extends object, C = unknown> = C extends void ? ForEachKeyValueOnPOJOFunctionWithoutContext<T> : ForEachKeyValueOnPOJOFunctionWithContext<T, C>;
|
|
584
|
+
/**
|
|
585
|
+
* Variant that takes only the object (no context).
|
|
586
|
+
*/
|
|
208
587
|
export type ForEachKeyValueOnPOJOFunctionWithoutContext<T extends object> = (object: T) => void;
|
|
588
|
+
/**
|
|
589
|
+
* Variant that takes both the object and a context value.
|
|
590
|
+
*/
|
|
209
591
|
export type ForEachKeyValueOnPOJOFunctionWithContext<T extends object, C = unknown> = (object: T, context: C) => void;
|
|
592
|
+
/**
|
|
593
|
+
* Configuration for {@link forEachKeyValueOnPOJOFunction}.
|
|
594
|
+
*/
|
|
210
595
|
export type ForEachKeyValueOnPOJOConfig<T extends object, C = unknown, K extends keyof T = keyof T> = {
|
|
596
|
+
/**
|
|
597
|
+
* Optional filter controlling which key/value pairs are iterated.
|
|
598
|
+
* When not provided, all key/value pairs are iterated.
|
|
599
|
+
*/
|
|
211
600
|
filter?: FilterKeyValueTuplesInput<T, K>;
|
|
601
|
+
/**
|
|
602
|
+
* Callback invoked for each matching key/value pair.
|
|
603
|
+
*/
|
|
212
604
|
forEach: ForEachKeyValueOnPOJOTupleFunction<T, C, K>;
|
|
213
605
|
};
|
|
606
|
+
/**
|
|
607
|
+
* Creates a reusable function that iterates over filtered key/value pairs of a POJO,
|
|
608
|
+
* invoking a callback for each matching pair.
|
|
609
|
+
*
|
|
610
|
+
* This is the low-level building block used by {@link filterFromPOJOFunction}, {@link findPOJOKeysFunction},
|
|
611
|
+
* {@link countPOJOKeysFunction}, {@link assignValuesToPOJOFunction}, and {@link valuesFromPOJOFunction}.
|
|
612
|
+
*
|
|
613
|
+
* When no filter is provided, all key/value pairs are iterated.
|
|
614
|
+
*
|
|
615
|
+
* @param config - The filter and forEach callback configuration.
|
|
616
|
+
* @returns A function that iterates matching key/value pairs on any input object.
|
|
617
|
+
*
|
|
618
|
+
* @example
|
|
619
|
+
* ```typescript
|
|
620
|
+
* const logDefined = forEachKeyValueOnPOJOFunction<Record<string, unknown>, void>({
|
|
621
|
+
* filter: KeyValueTypleValueFilter.UNDEFINED,
|
|
622
|
+
* forEach: ([key, value]) => console.log(key, value)
|
|
623
|
+
* });
|
|
624
|
+
* logDefined({ a: 1, b: undefined, c: 'test' });
|
|
625
|
+
* // logs: 'a' 1, 'c' 'test'
|
|
626
|
+
* ```
|
|
627
|
+
*/
|
|
214
628
|
export declare function forEachKeyValueOnPOJOFunction<T extends object, C = unknown, K extends keyof T = keyof T>({ forEach, filter }: ForEachKeyValueOnPOJOConfig<T, C, K>): ForEachKeyValueOnPOJOFunction<T, C>;
|