@homer0/object-utils 4.0.0 → 5.0.0

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/dist/index.d.mts DELETED
@@ -1,406 +0,0 @@
1
- /**
2
- * Creates a deep copy of a given object.
3
- *
4
- * @param target The object to copy.
5
- * @template T The type of the input object, which will be the same as the one
6
- * returned.
7
- */
8
- declare const copy: <T>(target: T) => T;
9
-
10
- /**
11
- * The callback that will be used to replace a key.
12
- *
13
- * @param substring The captured string to replace.
14
- * @param args The list of groups captured by the expression.
15
- */
16
- type FormatKeysReplaceFn = (substring: string, ...args: string[]) => string;
17
- type FormatKeysOptions = {
18
- /**
19
- * The object to transform.
20
- */
21
- target: unknown;
22
- /**
23
- * A regular expression to match the keys to transform.
24
- */
25
- search: RegExp;
26
- /**
27
- * The callback that will be used to replace the matched substring. Think of `search` and
28
- * `replace` as the parameters of `String.replace`.
29
- */
30
- replace: FormatKeysReplaceFn;
31
- /**
32
- * A list of keys or paths where the transformation will be made. If not specified, the
33
- * function will use all the keys from the object.
34
- */
35
- include?: string[];
36
- /**
37
- * A list of keys or paths where the transformation won't be made.
38
- */
39
- exclude?: string[];
40
- /**
41
- * The delimiter that will separate the path components for both `include` and `exclude`.
42
- */
43
- pathDelimiter?: string;
44
- };
45
- type FormatKeysExtensionOptions = Omit<FormatKeysOptions, 'search' | 'replace'>;
46
- /**
47
- * Formats all the keys on an object using a way similar to `.replace(regexp, ...)` but
48
- * that also works recursively and with _"object paths"_.
49
- *
50
- * @param options The set of options to customize the transformation.
51
- * @template T The type of the returned object.
52
- * @example
53
- *
54
- * const target = {
55
- * prop_one: 'Charito!',
56
- * };
57
- * console.log(
58
- * formatKeys({
59
- * target,
60
- * // Find all the keys with snake case.
61
- * search: /([a-z])_([a-z])/g,
62
- * // Using the same .replace style callback, replace it with lower camel case.
63
- * replace: (_, firstLetter, secondLetter) => {
64
- * const newSecondLetter = secondLetter.toUpperCase();
65
- * return `${firstLetter}${newSecondLetter}`;
66
- * },
67
- * }),
68
- * );
69
- * // Will output { propOne: 'Charito!}.
70
- *
71
- */
72
- declare const formatKeys: <T = Record<string, unknown>>(options: FormatKeysOptions) => T;
73
-
74
- /**
75
- * A shorthand method for {@link formatKeys} that transforms the keys from `dash-case`
76
- * to `lowerCamelCase`.
77
- *
78
- * @param options The options to use.
79
- * @template T The type of the returned object.
80
- */
81
- declare const dashToLowerCamelKeys: <T>(options: FormatKeysExtensionOptions) => T;
82
-
83
- /**
84
- * A shorthand method for {@link formatKeys} that transforms the keys from `dash-case`
85
- * to `snake_case`.
86
- *
87
- * @param options The options to use.
88
- * @template T The type of the returned object.
89
- */
90
- declare const dashToSnakeKeys: <T>(options: FormatKeysExtensionOptions) => T;
91
-
92
- /**
93
- * Used in the {@link extract} function to specify a path for a property, or an "origin path"
94
- * and an "extraction path".
95
- */
96
- type extractionPath = string | Record<string, string>;
97
- type ExtractOptions = {
98
- /**
99
- * The object from where the property/properties will be extracted.
100
- */
101
- target: unknown;
102
- /**
103
- * This can be a single path or a list of them. And for this method, the paths are not
104
- * only strings but can also be an object with a single key, that would be the path to
105
- * where to "do the extraction", and the value the path on the target object.
106
- */
107
- paths: extractionPath | extractionPath[];
108
- /**
109
- * The delimiter that will separate the path components.
110
- */
111
- pathDelimiter?: string;
112
- /**
113
- * Whether or not to throw an error when the path is invalid. If this is `false`, the
114
- * method will silently fail an empty object.
115
- */
116
- failWithError?: boolean;
117
- };
118
- /**
119
- * Extracts a property or properties from an object in order to create a new one.
120
- *
121
- * @param options The options to use.
122
- * @template T The type of the returned object.
123
- * @example
124
- *
125
- * const target = {
126
- * name: {
127
- * first: 'Pilar',
128
- * },
129
- * age: 3,
130
- * address: {
131
- * planet: 'earth',
132
- * something: 'else',
133
- * },
134
- * };
135
- * console.log(
136
- * extract({
137
- * target: obj,
138
- * paths: [{ name: 'name.first' }, 'age', 'address.planet'],
139
- * }),
140
- * );
141
- * // Will output { name: 'Pilar', age: 3, address: { planet: 'earth' } }
142
- *
143
- */
144
- declare const extract: <T = Record<string, unknown>>(options: ExtractOptions) => T | undefined;
145
-
146
- /**
147
- * Checks whether a key for an array or an Object should be flattened.
148
- *
149
- * @param key The key to check inside its parent.
150
- * @param value The value of the key.
151
- */
152
- type ShouldFlattenFn = (key: string, value: unknown) => boolean;
153
- type FlatOptions = {
154
- /**
155
- * The object to transform.
156
- */
157
- target: unknown;
158
- /**
159
- * A custom prefix to be added before the name of the properties. This can be used on
160
- * custom cases and it's also used when the method calls itself in order to flatten a
161
- * sub object.
162
- */
163
- prefix?: string;
164
- /**
165
- * The delimiter that will separate the path components.
166
- */
167
- pathDelimiter?: string;
168
- /**
169
- * A custom function that can be used in order to tell the function whether an Object or
170
- * an Array property should be flatten or not.
171
- */
172
- shouldFlatten?: ShouldFlattenFn;
173
- };
174
- /**
175
- * Flattens an object properties into a single level dictionary.
176
- *
177
- * @param options The options to use.
178
- * @template T The type of the returned object.
179
- * @example
180
- *
181
- * const target = {
182
- * propOne: {
183
- * propOneSub: 'Charito!',
184
- * },
185
- * propTwo: '!!!',
186
- * };
187
- * console.log(flat({ target });
188
- * // Will output { 'propOne.propOneSub': 'Charito!', propTwo: '!!!' }
189
- *
190
- */
191
- declare const flat: <T = Record<string, unknown>>(options: FlatOptions) => T;
192
-
193
- type GetOptions = {
194
- /**
195
- * The object from where the property will be read.
196
- */
197
- target: unknown;
198
- /**
199
- * The path to the property.
200
- */
201
- path: string;
202
- /**
203
- * The delimiter that will separate the path components.
204
- */
205
- pathDelimiter?: string;
206
- /**
207
- * Whether or not to throw an error when the path is invalid. If this is `false`,
208
- * the method will silently fail and return `undefined`.
209
- */
210
- failWithError?: boolean;
211
- };
212
- /**
213
- * Returns the value of an object property using a path.
214
- *
215
- * @param options The options to use.
216
- * @template T The type of the returned object.
217
- * @throws {Error} If the path is invalid and `failWithError` is set to `true`.
218
- * @example
219
- *
220
- * const obj = {
221
- * propOne: {
222
- * propOneSub: 'Charito!',
223
- * },
224
- * propTwo: '!!!',
225
- * };
226
- * console.log(get({ target: obj, path: 'propOne.propOneSub' }));
227
- * // Will output 'Charito!'
228
- *
229
- */
230
- declare function get<T>(options: GetOptions): T | undefined;
231
- declare function get<T>(options: unknown, path: string): T | undefined;
232
-
233
- /**
234
- * A shorthand method for {@link formatKeys} that transforms the keys from
235
- * `lowerCamelCase` to `dash-case`.
236
- *
237
- * @param options The options to use.
238
- * @template T The type of the returned object.
239
- */
240
- declare const lowerCamelToDashKeys: <T>(options: FormatKeysExtensionOptions) => T;
241
-
242
- /**
243
- * A shorthand method for {@link formatKeys} that transforms the keys from
244
- * `lowerCamelCase` to `snake_case`.
245
- *
246
- * @param options The options to use.
247
- * @template T The type of the returned object.
248
- */
249
- declare const lowerCamelToSnakeKeys: <T>(options: FormatKeysExtensionOptions) => T;
250
-
251
- /**
252
- * This method makes a deep merge of a list of objects into a new one. The method also
253
- * supports arrays: if the first element is an array, it will be merged into an array,
254
- * otherwise, it will be an object.
255
- *
256
- * @param targets The list of objects to merge.
257
- * @template T The type of the merged object that will be returned.
258
- * @example
259
- *
260
- * const objA = { a: 'first' };
261
- * const objB = { b: 'second' };
262
- * console.log(merge(objA, objB));
263
- * // Will output { a: 'first', b: 'second' }
264
- *
265
- * @example
266
- *
267
- * const arrA = [{ a: 'first' }];
268
- * const arrB = [{ b: 'second' }];
269
- * console.log(merge(objA, objB));
270
- * // Will output [{ a: 'first', b: 'second' }]
271
- *
272
- */
273
- declare const merge: <T = unknown>(...targets: unknown[]) => T;
274
-
275
- type RemoveOptions = {
276
- /**
277
- * The object from where the property will be removed.
278
- */
279
- target: unknown;
280
- /**
281
- * The path to the property.
282
- */
283
- path: string;
284
- /**
285
- * The delimiter that will separate the path components.
286
- */
287
- pathDelimiter?: string;
288
- /**
289
- * Whether or not to throw an error when the path is invalid. If this is `false`, the
290
- * function will silently fail.
291
- */
292
- failWithError?: boolean;
293
- /**
294
- * If this flag is `true` and after removing the property the parent object is empty,
295
- * it will remove it recursively until a non empty parent object is found.
296
- */
297
- cleanEmptyProperties?: boolean;
298
- };
299
- /**
300
- * Deletes a property of an object using a path.
301
- *
302
- * @param options The options to use.
303
- * @template T The type of the returned object.
304
- * @example
305
- *
306
- * const target = {
307
- * propOne: {
308
- * propOneSub: 'Charito!',
309
- * },
310
- * propTwo: '!!!',
311
- * };
312
- * console.log(remove({ target, path: 'propOne.propOneSub' }));
313
- * // Will output { propTwo: '!!!' }
314
- *
315
- */
316
- declare function remove<T = unknown>(options: RemoveOptions): T | undefined;
317
- declare function remove<T = unknown>(options: unknown, path: string): T | undefined;
318
-
319
- type SetOptions = {
320
- /**
321
- * The object where the property will be set.
322
- */
323
- target: unknown;
324
- /**
325
- * The path for the property.
326
- */
327
- path: string;
328
- /**
329
- * The value to set on the property.
330
- */
331
- value: unknown;
332
- /**
333
- * The delimiter that will separate the path components.
334
- */
335
- pathDelimiter?: string;
336
- /**
337
- * Whether or not to throw an error when the path is invalid. If this is `false`, the method
338
- * will silently fail and return `undefined`.
339
- */
340
- failWithError?: boolean;
341
- };
342
- /**
343
- * Sets a property on an object using a path. If the path doesn't exist, it will be
344
- * created.
345
- *
346
- * @param options The options to use.
347
- * @template T The type of the returned object.
348
- * @throws {Error} If one of the path components is for a non-object property and
349
- * `failWithError` is set to `true`.
350
- * @example
351
- *
352
- * const target = {};
353
- * console.log(set({ target, path: 'some.prop.path', value: 'some-value' }));
354
- * // Will output { some: { prop: { path: 'some-value' } } }
355
- *
356
- */
357
- declare function set<T = Record<string, unknown>>(options: SetOptions): T | undefined;
358
- declare function set<T = Record<string, unknown>>(options: SetOptions | unknown, path: string, value: unknown): T | undefined;
359
-
360
- /**
361
- * A shorthand method for {@link formatKeys} that transforms the keys from `snake_case` to
362
- * `dash-case`.
363
- *
364
- * @param options The options to use.
365
- * @template T The type of the returned object.
366
- */
367
- declare const snakeToDashKeys: <T>(options: FormatKeysExtensionOptions) => T;
368
-
369
- /**
370
- * A shorthand method for {@link formatKeys} that transforms the keys from `snake_case` to
371
- * `lowerCamelCase`.
372
- *
373
- * @param options The options to use.
374
- * @template T The type of the returned object.
375
- */
376
- declare const snakeToLowerCamelKeys: <T>(options: FormatKeysExtensionOptions) => T;
377
-
378
- type UnflatOptions = {
379
- /**
380
- * The object to transform.
381
- */
382
- target: unknown;
383
- /**
384
- * The delimiter that will separate the path components.
385
- */
386
- pathDelimiter?: string;
387
- };
388
- /**
389
- * This method does the exact opposite from `flat`: It takes an already flatten object and
390
- * restores it structure.
391
- *
392
- * @param options The options to use.
393
- * @template T The type of the returned object.
394
- * @example
395
- *
396
- * const target = {
397
- * 'propOne.propOneSub': 'Charito!',
398
- * propTwo: '!!!',
399
- * };
400
- * console.log(unflat({ target }));
401
- * // Will output { propOne: { propOneSub: 'Charito!' }, 'propTwo': '!!!' }
402
- *
403
- */
404
- declare const unflat: <T = Record<string, unknown>>(options: UnflatOptions) => T;
405
-
406
- export { type ExtractOptions, type FlatOptions, type FormatKeysExtensionOptions, type FormatKeysOptions, type FormatKeysReplaceFn, type GetOptions, type RemoveOptions, type SetOptions, type ShouldFlattenFn, type UnflatOptions, copy, dashToLowerCamelKeys, dashToSnakeKeys, extract, type extractionPath, flat, formatKeys, get, lowerCamelToDashKeys, lowerCamelToSnakeKeys, merge, remove, set, snakeToDashKeys, snakeToLowerCamelKeys, unflat };