es-toolkit 1.14.0 → 1.15.0-dev.416

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.
Files changed (58) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/_chunk/{initial-BTrQy1.js → initial-CBsbzo.js} +0 -25
  3. package/dist/_chunk/{isFunction-Cw_tLG.js → isFunction-D0hq6d.js} +4 -0
  4. package/dist/_chunk/isObjectLike-BeLCsr.js +263 -0
  5. package/dist/array/_internal/compareValues.mjs +22 -0
  6. package/dist/array/index.js +38 -2
  7. package/dist/array/orderBy.mjs +2 -9
  8. package/dist/browser.global.js +1 -1
  9. package/dist/browser.global.js.map +1 -1
  10. package/dist/compat/_internal/getPath.mjs +26 -0
  11. package/dist/compat/_internal/isKey.mjs +16 -0
  12. package/dist/compat/array/orderBy.d.mts +34 -0
  13. package/dist/compat/array/orderBy.d.ts +34 -0
  14. package/dist/compat/array/orderBy.mjs +50 -0
  15. package/dist/compat/index.d.mts +5 -1
  16. package/dist/compat/index.d.ts +5 -1
  17. package/dist/compat/index.js +233 -51
  18. package/dist/compat/index.mjs +6 -2
  19. package/dist/compat/object/cloneDeep.mjs +29 -0
  20. package/dist/compat/object/merge.d.mts +216 -0
  21. package/dist/compat/object/merge.d.ts +216 -0
  22. package/dist/compat/object/merge.mjs +8 -0
  23. package/dist/compat/object/mergeWith.d.mts +261 -0
  24. package/dist/compat/object/mergeWith.d.ts +261 -0
  25. package/dist/compat/object/mergeWith.mjs +75 -0
  26. package/dist/compat/predicate/isPlainObject.mjs +1 -1
  27. package/dist/compat/predicate/isSymbol.d.mts +17 -0
  28. package/dist/compat/predicate/isSymbol.d.ts +17 -0
  29. package/dist/compat/predicate/isSymbol.mjs +5 -0
  30. package/dist/index.d.mts +4 -0
  31. package/dist/index.d.ts +4 -0
  32. package/dist/index.js +18 -13
  33. package/dist/index.mjs +4 -0
  34. package/dist/object/index.d.mts +2 -0
  35. package/dist/object/index.d.ts +2 -0
  36. package/dist/object/index.js +42 -241
  37. package/dist/object/index.mjs +2 -0
  38. package/dist/object/merge.d.mts +43 -0
  39. package/dist/object/merge.d.ts +43 -0
  40. package/dist/object/merge.mjs +22 -0
  41. package/dist/object/mergeWith.d.mts +51 -0
  42. package/dist/object/mergeWith.d.ts +51 -0
  43. package/dist/object/mergeWith.mjs +26 -0
  44. package/dist/predicate/index.d.mts +1 -0
  45. package/dist/predicate/index.d.ts +1 -0
  46. package/dist/predicate/index.js +6 -1
  47. package/dist/predicate/index.mjs +1 -0
  48. package/dist/predicate/isSymbol.d.mts +11 -0
  49. package/dist/predicate/isSymbol.d.ts +11 -0
  50. package/dist/predicate/isSymbol.mjs +5 -0
  51. package/dist/string/index.d.mts +1 -0
  52. package/dist/string/index.d.ts +1 -0
  53. package/dist/string/index.js +6 -0
  54. package/dist/string/index.mjs +1 -0
  55. package/dist/string/pascalCase.d.mts +17 -0
  56. package/dist/string/pascalCase.d.ts +17 -0
  57. package/dist/string/pascalCase.mjs +9 -0
  58. package/package.json +1 -1
package/dist/index.d.ts CHANGED
@@ -81,6 +81,8 @@ export { flattenObject } from './object/flattenObject.js';
81
81
  export { mapKeys } from './object/mapKeys.js';
82
82
  export { mapValues } from './object/mapValues.js';
83
83
  export { cloneDeep } from './object/cloneDeep.js';
84
+ export { merge } from './object/merge.js';
85
+ export { mergeWith } from './object/mergeWith.js';
84
86
  export { isEqual } from './predicate/isEqual.js';
85
87
  export { isNil } from './predicate/isNil.js';
86
88
  export { isNotNil } from './predicate/isNotNil.js';
@@ -92,6 +94,7 @@ export { isFunction } from './predicate/isFunction.js';
92
94
  export { isTypedArray } from './predicate/isTypedArray.js';
93
95
  export { isPrimitive } from './predicate/isPrimitive.js';
94
96
  export { isBoolean } from './predicate/isBoolean.js';
97
+ export { isSymbol } from './predicate/isSymbol.js';
95
98
  export { delay } from './promise/delay.js';
96
99
  export { withTimeout } from './promise/withTimeout.js';
97
100
  export { camelCase } from './string/camelCase.js';
@@ -100,3 +103,4 @@ export { kebabCase } from './string/kebabCase.js';
100
103
  export { lowerCase } from './string/lowerCase.js';
101
104
  export { startCase } from './string/startCase.js';
102
105
  export { capitalize } from './string/capitalize.js';
106
+ export { pascalCase } from './string/pascalCase.js';
package/dist/index.js CHANGED
@@ -2,14 +2,15 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
- const initial = require('./_chunk/initial-BTrQy1.js');
5
+ const initial = require('./_chunk/initial-CBsbzo.js');
6
6
  const array_index = require('./array/index.js');
7
7
  const promise_index = require('./_chunk/index-CwRt_M.js');
8
8
  const function_index = require('./function/index.js');
9
9
  const math_index = require('./math/index.js');
10
10
  const randomInt = require('./_chunk/randomInt-CF7bZK.js');
11
+ const isObjectLike = require('./_chunk/isObjectLike-BeLCsr.js');
11
12
  const object_index = require('./object/index.js');
12
- const isFunction = require('./_chunk/isFunction-Cw_tLG.js');
13
+ const isFunction = require('./_chunk/isFunction-D0hq6d.js');
13
14
  const isTypedArray = require('./_chunk/isTypedArray-BBEkFl.js');
14
15
  const predicate_index = require('./predicate/index.js');
15
16
  const string_index = require('./string/index.js');
@@ -41,7 +42,6 @@ exports.keyBy = initial.keyBy;
41
42
  exports.last = initial.last;
42
43
  exports.maxBy = initial.maxBy;
43
44
  exports.minBy = initial.minBy;
44
- exports.orderBy = initial.orderBy;
45
45
  exports.partition = initial.partition;
46
46
  exports.sample = initial.sample;
47
47
  exports.sampleSize = initial.sampleSize;
@@ -68,6 +68,7 @@ exports.zip = initial.zip;
68
68
  exports.zipObject = initial.zipObject;
69
69
  exports.zipWith = initial.zipWith;
70
70
  exports.flattenDeep = array_index.flattenDeep;
71
+ exports.orderBy = array_index.orderBy;
71
72
  exports.AbortError = promise_index.AbortError;
72
73
  exports.TimeoutError = promise_index.TimeoutError;
73
74
  exports.delay = promise_index.delay;
@@ -91,16 +92,18 @@ exports.sum = math_index.sum;
91
92
  exports.sumBy = math_index.sumBy;
92
93
  exports.random = randomInt.random;
93
94
  exports.randomInt = randomInt.randomInt;
94
- exports.clone = object_index.clone;
95
- exports.cloneDeep = object_index.cloneDeep;
96
- exports.flattenObject = object_index.flattenObject;
97
- exports.invert = object_index.invert;
98
- exports.mapKeys = object_index.mapKeys;
99
- exports.mapValues = object_index.mapValues;
100
- exports.omit = object_index.omit;
101
- exports.omitBy = object_index.omitBy;
102
- exports.pick = object_index.pick;
103
- exports.pickBy = object_index.pickBy;
95
+ exports.clone = isObjectLike.clone;
96
+ exports.cloneDeep = isObjectLike.cloneDeep;
97
+ exports.flattenObject = isObjectLike.flattenObject;
98
+ exports.invert = isObjectLike.invert;
99
+ exports.mapKeys = isObjectLike.mapKeys;
100
+ exports.mapValues = isObjectLike.mapValues;
101
+ exports.omit = isObjectLike.omit;
102
+ exports.omitBy = isObjectLike.omitBy;
103
+ exports.pick = isObjectLike.pick;
104
+ exports.pickBy = isObjectLike.pickBy;
105
+ exports.merge = object_index.merge;
106
+ exports.mergeWith = object_index.mergeWith;
104
107
  exports.isEqual = isFunction.isEqual;
105
108
  exports.isFunction = isFunction.isFunction;
106
109
  exports.isLength = isFunction.isLength;
@@ -112,9 +115,11 @@ exports.isPlainObject = isTypedArray.isPlainObject;
112
115
  exports.isPrimitive = isTypedArray.isPrimitive;
113
116
  exports.isTypedArray = isTypedArray.isTypedArray;
114
117
  exports.isBoolean = predicate_index.isBoolean;
118
+ exports.isSymbol = predicate_index.isSymbol;
115
119
  exports.camelCase = string_index.camelCase;
116
120
  exports.capitalize = string_index.capitalize;
117
121
  exports.kebabCase = string_index.kebabCase;
118
122
  exports.lowerCase = string_index.lowerCase;
123
+ exports.pascalCase = string_index.pascalCase;
119
124
  exports.snakeCase = string_index.snakeCase;
120
125
  exports.startCase = string_index.startCase;
package/dist/index.mjs CHANGED
@@ -81,6 +81,8 @@ export { flattenObject } from './object/flattenObject.mjs';
81
81
  export { mapKeys } from './object/mapKeys.mjs';
82
82
  export { mapValues } from './object/mapValues.mjs';
83
83
  export { cloneDeep } from './object/cloneDeep.mjs';
84
+ export { merge } from './object/merge.mjs';
85
+ export { mergeWith } from './object/mergeWith.mjs';
84
86
  export { isEqual } from './predicate/isEqual.mjs';
85
87
  export { isNil } from './predicate/isNil.mjs';
86
88
  export { isNotNil } from './predicate/isNotNil.mjs';
@@ -92,6 +94,7 @@ export { isFunction } from './predicate/isFunction.mjs';
92
94
  export { isTypedArray } from './predicate/isTypedArray.mjs';
93
95
  export { isPrimitive } from './predicate/isPrimitive.mjs';
94
96
  export { isBoolean } from './predicate/isBoolean.mjs';
97
+ export { isSymbol } from './predicate/isSymbol.mjs';
95
98
  export { delay } from './promise/delay.mjs';
96
99
  export { withTimeout } from './promise/withTimeout.mjs';
97
100
  export { camelCase } from './string/camelCase.mjs';
@@ -100,3 +103,4 @@ export { kebabCase } from './string/kebabCase.mjs';
100
103
  export { lowerCase } from './string/lowerCase.mjs';
101
104
  export { startCase } from './string/startCase.mjs';
102
105
  export { capitalize } from './string/capitalize.mjs';
106
+ export { pascalCase } from './string/pascalCase.mjs';
@@ -8,3 +8,5 @@ export { flattenObject } from './flattenObject.mjs';
8
8
  export { mapKeys } from './mapKeys.mjs';
9
9
  export { mapValues } from './mapValues.mjs';
10
10
  export { cloneDeep } from './cloneDeep.mjs';
11
+ export { merge } from './merge.mjs';
12
+ export { mergeWith } from './mergeWith.mjs';
@@ -8,3 +8,5 @@ export { flattenObject } from './flattenObject.js';
8
8
  export { mapKeys } from './mapKeys.js';
9
9
  export { mapValues } from './mapValues.js';
10
10
  export { cloneDeep } from './cloneDeep.js';
11
+ export { merge } from './merge.js';
12
+ export { mergeWith } from './mergeWith.js';
@@ -2,258 +2,59 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
- const isTypedArray = require('../_chunk/isTypedArray-BBEkFl.js');
5
+ const isObjectLike = require('../_chunk/isObjectLike-BeLCsr.js');
6
6
 
7
- function omit(obj, keys) {
8
- const result = { ...obj };
9
- for (const key of keys) {
10
- delete result[key];
11
- }
12
- return result;
13
- }
14
-
15
- function omitBy(obj, shouldOmit) {
16
- const result = {};
17
- for (const [key, value] of Object.entries(obj)) {
18
- if (shouldOmit(value, key)) {
19
- continue;
20
- }
21
- result[key] = value;
22
- }
23
- return result;
24
- }
25
-
26
- function pick(obj, keys) {
27
- const result = {};
28
- for (const key of keys) {
29
- result[key] = obj[key];
30
- }
31
- return result;
32
- }
33
-
34
- function pickBy(obj, shouldPick) {
35
- const result = {};
36
- for (const [key, value] of Object.entries(obj)) {
37
- if (!shouldPick(value, key)) {
38
- continue;
7
+ function merge(target, source) {
8
+ const sourceKeys = Object.keys(source);
9
+ for (let i = 0; i < sourceKeys.length; i++) {
10
+ const key = sourceKeys[i];
11
+ const sourceValue = source[key];
12
+ const targetValue = target[key];
13
+ if (Array.isArray(sourceValue)) {
14
+ target[key] = merge(targetValue ?? [], sourceValue);
39
15
  }
40
- result[key] = value;
41
- }
42
- return result;
43
- }
44
-
45
- function invert(obj) {
46
- const result = {};
47
- const keys = Object.keys(obj);
48
- for (let i = 0; i < keys.length; i++) {
49
- const key = keys[i];
50
- const value = obj[key];
51
- result[value] = key;
52
- }
53
- return result;
54
- }
55
-
56
- function clone(obj) {
57
- if (isPrimitive(obj)) {
58
- return obj;
59
- }
60
- if (Array.isArray(obj)) {
61
- return obj.slice();
62
- }
63
- if (obj instanceof Date) {
64
- return new Date(obj.getTime());
65
- }
66
- if (obj instanceof RegExp) {
67
- return new RegExp(obj.source, obj.flags);
68
- }
69
- if (obj instanceof Map) {
70
- const result = new Map();
71
- for (const [key, value] of obj) {
72
- result.set(key, value);
16
+ else if (isObjectLike.isObjectLike(targetValue) && isObjectLike.isObjectLike(sourceValue)) {
17
+ target[key] = merge(targetValue ?? {}, sourceValue);
73
18
  }
74
- return result;
75
- }
76
- if (obj instanceof Set) {
77
- const result = new Set();
78
- for (const value of obj) {
79
- result.add(value);
19
+ else if (targetValue === undefined || sourceValue !== undefined) {
20
+ target[key] = sourceValue;
80
21
  }
81
- return result;
82
22
  }
83
- if (typeof obj === 'object') {
84
- const prototype = Object.getPrototypeOf(obj);
85
- const result = Object.create(prototype);
86
- return Object.assign(result, obj);
87
- }
88
- return obj;
89
- }
90
- function isPrimitive(value) {
91
- return value == null || (typeof value !== 'object' && typeof value !== 'function');
23
+ return target;
92
24
  }
93
25
 
94
- function flattenObject(object) {
95
- return flattenObjectImpl(object);
96
- }
97
- function flattenObjectImpl(object, prefix = '') {
98
- const result = {};
99
- const keys = Object.keys(object);
100
- for (let i = 0; i < keys.length; i++) {
101
- const key = keys[i];
102
- const value = object[key];
103
- const prefixedKey = prefix ? `${prefix}.${key}` : key;
104
- if (isTypedArray.isPlainObject(value) && Object.keys(value).length > 0) {
105
- Object.assign(result, flattenObjectImpl(value, prefixedKey));
106
- continue;
26
+ function mergeWith(target, source, merge) {
27
+ const sourceKeys = Object.keys(source);
28
+ for (let i = 0; i < sourceKeys.length; i++) {
29
+ const key = sourceKeys[i];
30
+ const sourceValue = source[key];
31
+ const targetValue = target[key];
32
+ const merged = merge(targetValue, sourceValue, key, target, source);
33
+ if (merged != null) {
34
+ target[key] = merged;
107
35
  }
108
- if (Array.isArray(value)) {
109
- for (let index = 0; index < value.length; index++) {
110
- result[`${prefixedKey}.${index}`] = value[index];
111
- }
112
- continue;
36
+ else if (Array.isArray(sourceValue)) {
37
+ target[key] = mergeWith(targetValue ?? [], sourceValue, merge);
113
38
  }
114
- result[prefixedKey] = value;
115
- }
116
- return result;
117
- }
118
-
119
- function mapKeys(object, getNewKey) {
120
- const result = {};
121
- const keys = Object.keys(object);
122
- for (let i = 0; i < keys.length; i++) {
123
- const key = keys[i];
124
- const value = object[key];
125
- result[getNewKey(value, key, object)] = value;
126
- }
127
- return result;
128
- }
129
-
130
- function mapValues(object, getNewValue) {
131
- const result = {};
132
- const keys = Object.keys(object);
133
- for (let i = 0; i < keys.length; i++) {
134
- const key = keys[i];
135
- const value = object[key];
136
- result[key] = getNewValue(value, key, object);
137
- }
138
- return result;
139
- }
140
-
141
- function cloneDeep(obj) {
142
- return cloneDeepImpl(obj);
143
- }
144
- function cloneDeepImpl(obj, stack = new Map()) {
145
- if (isTypedArray.isPrimitive(obj)) {
146
- return obj;
147
- }
148
- if (stack.has(obj)) {
149
- return stack.get(obj);
150
- }
151
- if (Array.isArray(obj)) {
152
- const result = new Array(obj.length);
153
- stack.set(obj, result);
154
- for (let i = 0; i < obj.length; i++) {
155
- result[i] = cloneDeepImpl(obj[i], stack);
156
- }
157
- if (Object.prototype.hasOwnProperty.call(obj, 'index')) {
158
- result.index = obj.index;
159
- }
160
- if (Object.prototype.hasOwnProperty.call(obj, 'input')) {
161
- result.input = obj.input;
162
- }
163
- return result;
164
- }
165
- if (obj instanceof Date) {
166
- return new Date(obj.getTime());
167
- }
168
- if (obj instanceof RegExp) {
169
- const result = new RegExp(obj.source, obj.flags);
170
- result.lastIndex = obj.lastIndex;
171
- return result;
172
- }
173
- if (obj instanceof Map) {
174
- const result = new Map();
175
- stack.set(obj, result);
176
- for (const [key, value] of obj.entries()) {
177
- result.set(key, cloneDeepImpl(value, stack));
39
+ else if (isObjectLike.isObjectLike(targetValue) && isObjectLike.isObjectLike(sourceValue)) {
40
+ target[key] = mergeWith(targetValue ?? {}, sourceValue, merge);
178
41
  }
179
- return result;
180
- }
181
- if (obj instanceof Set) {
182
- const result = new Set();
183
- stack.set(obj, result);
184
- for (const value of obj.values()) {
185
- result.add(cloneDeepImpl(value, stack));
186
- }
187
- return result;
188
- }
189
- if (typeof Buffer !== 'undefined' && Buffer.isBuffer(obj)) {
190
- return obj.subarray();
191
- }
192
- if (isTypedArray.isTypedArray(obj)) {
193
- const result = new (Object.getPrototypeOf(obj).constructor)(obj.length);
194
- stack.set(obj, result);
195
- for (let i = 0; i < obj.length; i++) {
196
- result[i] = cloneDeepImpl(obj[i], stack);
197
- }
198
- return result;
199
- }
200
- if (obj instanceof ArrayBuffer || (typeof SharedArrayBuffer !== 'undefined' && obj instanceof SharedArrayBuffer)) {
201
- return obj.slice(0);
202
- }
203
- if (obj instanceof DataView) {
204
- const result = new DataView(obj.buffer.slice(0));
205
- stack.set(obj, result);
206
- copyProperties(result, obj, stack);
207
- return result;
208
- }
209
- if (typeof File !== 'undefined' && obj instanceof File) {
210
- const result = new File([obj], obj.name, { type: obj.type });
211
- stack.set(obj, result);
212
- copyProperties(result, obj, stack);
213
- return result;
214
- }
215
- if (obj instanceof Blob) {
216
- const result = new Blob([obj], { type: obj.type });
217
- stack.set(obj, result);
218
- copyProperties(result, obj, stack);
219
- return result;
220
- }
221
- if (obj instanceof Error) {
222
- const result = new obj.constructor();
223
- stack.set(obj, result);
224
- result.message = obj.message;
225
- result.name = obj.name;
226
- result.stack = obj.stack;
227
- result.cause = obj.cause;
228
- copyProperties(result, obj, stack);
229
- return result;
230
- }
231
- if (typeof obj === 'object' && obj !== null) {
232
- const result = {};
233
- stack.set(obj, result);
234
- copyProperties(result, obj, stack);
235
- return result;
236
- }
237
- return obj;
238
- }
239
- function copyProperties(target, source, stack) {
240
- const keys = Object.keys(source);
241
- for (let i = 0; i < keys.length; i++) {
242
- const key = keys[i];
243
- const descriptor = Object.getOwnPropertyDescriptor(source, key);
244
- if (descriptor?.writable || descriptor?.set) {
245
- target[key] = cloneDeepImpl(source[key], stack);
42
+ else if (targetValue === undefined || sourceValue !== undefined) {
43
+ target[key] = sourceValue;
246
44
  }
247
45
  }
46
+ return target;
248
47
  }
249
48
 
250
- exports.clone = clone;
251
- exports.cloneDeep = cloneDeep;
252
- exports.flattenObject = flattenObject;
253
- exports.invert = invert;
254
- exports.mapKeys = mapKeys;
255
- exports.mapValues = mapValues;
256
- exports.omit = omit;
257
- exports.omitBy = omitBy;
258
- exports.pick = pick;
259
- exports.pickBy = pickBy;
49
+ exports.clone = isObjectLike.clone;
50
+ exports.cloneDeep = isObjectLike.cloneDeep;
51
+ exports.flattenObject = isObjectLike.flattenObject;
52
+ exports.invert = isObjectLike.invert;
53
+ exports.mapKeys = isObjectLike.mapKeys;
54
+ exports.mapValues = isObjectLike.mapValues;
55
+ exports.omit = isObjectLike.omit;
56
+ exports.omitBy = isObjectLike.omitBy;
57
+ exports.pick = isObjectLike.pick;
58
+ exports.pickBy = isObjectLike.pickBy;
59
+ exports.merge = merge;
60
+ exports.mergeWith = mergeWith;
@@ -8,3 +8,5 @@ export { flattenObject } from './flattenObject.mjs';
8
8
  export { mapKeys } from './mapKeys.mjs';
9
9
  export { mapValues } from './mapValues.mjs';
10
10
  export { cloneDeep } from './cloneDeep.mjs';
11
+ export { merge } from './merge.mjs';
12
+ export { mergeWith } from './mergeWith.mjs';
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Merges the properties of the source object into the target object.
3
+ *
4
+ * This function performs a deep merge, meaning nested objects and arrays are merged recursively.
5
+ * If a property in the source object is an array or an object and the corresponding property in the target object is also an array or object, they will be merged.
6
+ * If a property in the source object is undefined, it will not overwrite a defined property in the target object.
7
+ *
8
+ * Note that this function mutates the target object.
9
+ *
10
+ * @param {T} target - The target object into which the source object properties will be merged. This object is modified in place.
11
+ * @param {S} source - The source object whose properties will be merged into the target object.
12
+ * @returns {T & S} The updated target object with properties from the source object merged in.
13
+ *
14
+ * @template T - Type of the target object.
15
+ * @template S - Type of the source object.
16
+ *
17
+ * @example
18
+ * const target = { a: 1, b: { x: 1, y: 2 } };
19
+ * const source = { b: { y: 3, z: 4 }, c: 5 };
20
+ *
21
+ * const result = merge(target, source);
22
+ * console.log(result);
23
+ * // Output: { a: 1, b: { x: 1, y: 3, z: 4 }, c: 5 }
24
+ *
25
+ * @example
26
+ * const target = { a: [1, 2], b: { x: 1 } };
27
+ * const source = { a: [3], b: { y: 2 } };
28
+ *
29
+ * const result = merge(target, source);
30
+ * console.log(result);
31
+ * // Output: { a: [3, 2], b: { x: 1, y: 2 } }
32
+ *
33
+ * @example
34
+ * const target = { a: null };
35
+ * const source = { a: [1, 2, 3] };
36
+ *
37
+ * const result = merge(target, source);
38
+ * console.log(result);
39
+ * // Output: { a: [1, 2, 3] }
40
+ */
41
+ declare function merge<T, S>(target: T, source: S): T & S;
42
+
43
+ export { merge };
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Merges the properties of the source object into the target object.
3
+ *
4
+ * This function performs a deep merge, meaning nested objects and arrays are merged recursively.
5
+ * If a property in the source object is an array or an object and the corresponding property in the target object is also an array or object, they will be merged.
6
+ * If a property in the source object is undefined, it will not overwrite a defined property in the target object.
7
+ *
8
+ * Note that this function mutates the target object.
9
+ *
10
+ * @param {T} target - The target object into which the source object properties will be merged. This object is modified in place.
11
+ * @param {S} source - The source object whose properties will be merged into the target object.
12
+ * @returns {T & S} The updated target object with properties from the source object merged in.
13
+ *
14
+ * @template T - Type of the target object.
15
+ * @template S - Type of the source object.
16
+ *
17
+ * @example
18
+ * const target = { a: 1, b: { x: 1, y: 2 } };
19
+ * const source = { b: { y: 3, z: 4 }, c: 5 };
20
+ *
21
+ * const result = merge(target, source);
22
+ * console.log(result);
23
+ * // Output: { a: 1, b: { x: 1, y: 3, z: 4 }, c: 5 }
24
+ *
25
+ * @example
26
+ * const target = { a: [1, 2], b: { x: 1 } };
27
+ * const source = { a: [3], b: { y: 2 } };
28
+ *
29
+ * const result = merge(target, source);
30
+ * console.log(result);
31
+ * // Output: { a: [3, 2], b: { x: 1, y: 2 } }
32
+ *
33
+ * @example
34
+ * const target = { a: null };
35
+ * const source = { a: [1, 2, 3] };
36
+ *
37
+ * const result = merge(target, source);
38
+ * console.log(result);
39
+ * // Output: { a: [1, 2, 3] }
40
+ */
41
+ declare function merge<T, S>(target: T, source: S): T & S;
42
+
43
+ export { merge };
@@ -0,0 +1,22 @@
1
+ import { isObjectLike } from '../compat/predicate/isObjectLike.mjs';
2
+
3
+ function merge(target, source) {
4
+ const sourceKeys = Object.keys(source);
5
+ for (let i = 0; i < sourceKeys.length; i++) {
6
+ const key = sourceKeys[i];
7
+ const sourceValue = source[key];
8
+ const targetValue = target[key];
9
+ if (Array.isArray(sourceValue)) {
10
+ target[key] = merge(targetValue ?? [], sourceValue);
11
+ }
12
+ else if (isObjectLike(targetValue) && isObjectLike(sourceValue)) {
13
+ target[key] = merge(targetValue ?? {}, sourceValue);
14
+ }
15
+ else if (targetValue === undefined || sourceValue !== undefined) {
16
+ target[key] = sourceValue;
17
+ }
18
+ }
19
+ return target;
20
+ }
21
+
22
+ export { merge };
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Merges the properties of the source object into the target object.
3
+ *
4
+ * You can provide a custom `merge` function to control how properties are merged. It should return the value to be set in the target object.
5
+ *
6
+ * If it returns `undefined`, a default deep merge will be applied for arrays and objects:
7
+ *
8
+ * - If a property in the source object is an array or an object and the corresponding property in the target object is also an array or object, they will be merged.
9
+ * - If a property in the source object is undefined, it will not overwrite a defined property in the target object.
10
+ *
11
+ * Note that this function mutates the target object.
12
+ *
13
+ * @param {T} target - The target object into which the source object properties will be merged. This object is modified in place.
14
+ * @param {S} source - The source object whose properties will be merged into the target object.
15
+ * @param {(targetValue: any, sourceValue: any, key: string, target: T, source: S) => any} merge - A custom merge function that defines how properties should be combined. It receives the following arguments:
16
+ * - `targetValue`: The current value of the property in the target object.
17
+ * - `sourceValue`: The value of the property in the source object.
18
+ * - `key`: The key of the property being merged.
19
+ * - `target`: The target object.
20
+ * - `source`: The source object.
21
+ *
22
+ * @returns {T & S} The updated target object with properties from the source object merged in.
23
+ *
24
+ * @template T - Type of the target object.
25
+ * @template S - Type of the source object.
26
+ *
27
+ * @example
28
+ * const target = { a: 1, b: 2 };
29
+ * const source = { b: 3, c: 4 };
30
+ *
31
+ * mergeWith(target, source, (targetValue, sourceValue) => {
32
+ * if (typeof targetValue === 'number' && typeof sourceValue === 'number') {
33
+ * return targetValue + sourceValue;
34
+ * }
35
+ * });
36
+ * // Returns { a: 1, b: 5, c: 4 }
37
+ * @example
38
+ * const target = { a: [1], b: [2] };
39
+ * const source = { a: [3], b: [4] };
40
+ *
41
+ * const result = mergeWith(target, source, (objValue, srcValue) => {
42
+ * if (Array.isArray(objValue)) {
43
+ * return objValue.concat(srcValue);
44
+ * }
45
+ * });
46
+ *
47
+ * expect(result).toEqual({ a: [1, 3], b: [2, 4] });
48
+ */
49
+ declare function mergeWith<T, S>(target: T, source: S, merge: (targetValue: any, sourceValue: any, key: string, target: T, source: S) => any): T & S;
50
+
51
+ export { mergeWith };
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Merges the properties of the source object into the target object.
3
+ *
4
+ * You can provide a custom `merge` function to control how properties are merged. It should return the value to be set in the target object.
5
+ *
6
+ * If it returns `undefined`, a default deep merge will be applied for arrays and objects:
7
+ *
8
+ * - If a property in the source object is an array or an object and the corresponding property in the target object is also an array or object, they will be merged.
9
+ * - If a property in the source object is undefined, it will not overwrite a defined property in the target object.
10
+ *
11
+ * Note that this function mutates the target object.
12
+ *
13
+ * @param {T} target - The target object into which the source object properties will be merged. This object is modified in place.
14
+ * @param {S} source - The source object whose properties will be merged into the target object.
15
+ * @param {(targetValue: any, sourceValue: any, key: string, target: T, source: S) => any} merge - A custom merge function that defines how properties should be combined. It receives the following arguments:
16
+ * - `targetValue`: The current value of the property in the target object.
17
+ * - `sourceValue`: The value of the property in the source object.
18
+ * - `key`: The key of the property being merged.
19
+ * - `target`: The target object.
20
+ * - `source`: The source object.
21
+ *
22
+ * @returns {T & S} The updated target object with properties from the source object merged in.
23
+ *
24
+ * @template T - Type of the target object.
25
+ * @template S - Type of the source object.
26
+ *
27
+ * @example
28
+ * const target = { a: 1, b: 2 };
29
+ * const source = { b: 3, c: 4 };
30
+ *
31
+ * mergeWith(target, source, (targetValue, sourceValue) => {
32
+ * if (typeof targetValue === 'number' && typeof sourceValue === 'number') {
33
+ * return targetValue + sourceValue;
34
+ * }
35
+ * });
36
+ * // Returns { a: 1, b: 5, c: 4 }
37
+ * @example
38
+ * const target = { a: [1], b: [2] };
39
+ * const source = { a: [3], b: [4] };
40
+ *
41
+ * const result = mergeWith(target, source, (objValue, srcValue) => {
42
+ * if (Array.isArray(objValue)) {
43
+ * return objValue.concat(srcValue);
44
+ * }
45
+ * });
46
+ *
47
+ * expect(result).toEqual({ a: [1, 3], b: [2, 4] });
48
+ */
49
+ declare function mergeWith<T, S>(target: T, source: S, merge: (targetValue: any, sourceValue: any, key: string, target: T, source: S) => any): T & S;
50
+
51
+ export { mergeWith };