-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge.ts
More file actions
319 lines (293 loc) · 9.44 KB
/
merge.ts
File metadata and controls
319 lines (293 loc) · 9.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import { isObject, isPlainObject } from './is-object.js';
import { isBuiltIn } from './type-guards.js';
const hasOwnProperty = Object.prototype.hasOwnProperty;
export function merge<A extends object, B extends object, C extends object>(
target: A,
source: [B, C],
options?: merge.Options,
): A & B & C;
export function merge<
A extends object,
B extends object,
C extends object,
D extends object,
>(target: A, source: [B, C, D], options?: merge.Options): A & B & C & D;
export function merge<
A extends object,
B extends object,
C extends object,
D extends object,
E extends object,
>(target: A, source: [B, C, D, E], options?: merge.Options): A & B & C & D & E;
export function merge<A, B, C, D, F>(
target: A,
source: [B, C, D, F],
options?: merge.Options,
): A & B & C & D & F;
export function merge<A extends object, B extends object>(
target: A,
source: B,
options?: merge.Options,
): A & B;
export function merge(
targetObject: any,
sourceObject: any,
options?: merge.Options,
): any {
if (!(isObject(targetObject) || typeof targetObject === 'function')) {
throw new TypeError('"target" argument must be an object');
}
if (sourceObject == null) return targetObject;
if (
!(
isObject(sourceObject) ||
typeof sourceObject === 'function' ||
Array.isArray(sourceObject)
)
) {
throw new TypeError(
'"target" argument must be an object or array of objects',
);
}
const keepExisting = options?.keepExisting;
const keepExistingFn =
typeof options?.keepExisting === 'function'
? options?.keepExisting
: undefined;
const filterFn = options?.filter;
const ignoreUndefined = options?.ignoreUndefined ?? true;
const ignoreNulls = options?.ignoreNulls;
const deep = options?.deep;
const deepFull = deep === 'full';
const deepFn =
typeof options?.deep === 'function' ? options?.deep : undefined;
const copyDescriptors = options?.copyDescriptors;
const mergeArrays = options?.mergeArrays;
const mergeArraysUnique = options?.mergeArrays === 'unique';
const mergeArraysFn =
typeof options?.mergeArrays === 'function'
? options?.mergeArrays
: undefined;
const _merge = (target: any, source: any, parentPath: string = '') => {
if (!isObject(source)) return;
const keys: (string | symbol)[] = Object.getOwnPropertyNames(source);
if (options?.symbolKeys ?? true)
keys.push(...Object.getOwnPropertySymbols(source));
let key: string | symbol | number;
let descriptor: PropertyDescriptor | undefined;
let srcVal: any;
let _goDeep = false;
if (isPlainObject(target))
Object.setPrototypeOf(target, Object.getPrototypeOf(source));
const ignoreFn = options?.ignoreSource;
let i = 0;
const len = keys.length;
for (i = 0; i < len; i++) {
key = keys[i];
/** Should not overwrite __proto__ and constructor properties */
if (key === '__proto__' || key === 'constructor') continue;
if (copyDescriptors) {
descriptor = Object.getOwnPropertyDescriptor(source, key);
if (descriptor?.get || descriptor?.set) {
Object.defineProperty(target, key, descriptor);
continue;
}
}
srcVal = source[key];
if (
ignoreFn?.(srcVal, {
key,
source,
target,
path: parentPath + (parentPath ? '.' : '') + String(key),
})
) {
continue;
}
_goDeep = !!(
deep &&
typeof srcVal === 'object' &&
(!isBuiltIn(srcVal) || Array.isArray(srcVal))
);
if (_goDeep) {
if (deepFn)
_goDeep = deepFn(srcVal, {
key,
source,
target,
path: parentPath + (parentPath ? '.' : '') + String(key),
});
else
_goDeep = deepFull || isPlainObject(srcVal) || Array.isArray(srcVal);
}
if (!_goDeep && keepExisting && hasOwnProperty.call(target, key)) {
if (!keepExistingFn) continue;
if (
keepExistingFn(srcVal, {
key,
source,
target,
path: parentPath + (parentPath ? '.' : '') + String(key),
})
) {
continue;
}
}
if (
filterFn &&
!filterFn(srcVal, {
key,
source,
target,
path: parentPath + (parentPath ? '.' : '') + String(key),
})
) {
continue;
}
if (ignoreUndefined && srcVal === undefined) {
continue;
}
if (ignoreNulls && srcVal === null) {
continue;
}
if (_goDeep) {
/** Array */
if (Array.isArray(srcVal)) {
if (
Array.isArray(target[key]) &&
(mergeArrays ||
mergeArraysFn?.(srcVal, {
key,
source,
target,
path: parentPath + (parentPath ? '.' : '') + String(key),
}))
) {
target[key] = _arrayClone(
target[key],
parentPath + (parentPath ? '.' : '') + String(key),
);
} else target[key] = [];
target[key].push(
..._arrayClone(
srcVal,
parentPath + (parentPath ? '.' : '') + String(key),
),
);
if (mergeArraysUnique) target[key] = Array.from(new Set(target[key]));
continue;
} else {
/** Object */
if (!isObject(target[key])) target[key] = {};
_merge(
target[key],
srcVal,
parentPath + (parentPath ? '.' : '') + String(key),
);
continue;
}
}
if (copyDescriptors) {
descriptor = { ...Object.getOwnPropertyDescriptor(source, key) };
descriptor.value = srcVal;
Object.defineProperty(target, key, descriptor);
continue;
}
target[key] = srcVal;
}
return target;
};
const _arrayClone = (arr: any[], curPath: string): any[] => {
return arr.map((x: any, index) => {
if (Array.isArray(x)) return _arrayClone(x, curPath + '[' + index + ']');
if (typeof x === 'object' && !isBuiltIn(x))
return _merge({}, x, curPath + '[' + index + ']');
return x;
});
};
const sources = Array.isArray(sourceObject) ? sourceObject : [sourceObject];
for (const src of sources) {
_merge(targetObject, src);
}
return targetObject;
}
/**
* @namespace
*/
export namespace merge {
export type CallbackFn = (value: any, ctx: CallbackContext) => boolean;
export interface CallbackContext {
source: any;
target: any;
key: string | symbol | number;
path: string;
}
export interface Options {
/**
* Optional variable that determines the depth of an operation or inclusion behavior.
*
* - If set to `true`, it enables a deep operation for only native js objects, excluding classes.
* - If set to `'full'`, it enables a deep operation for all objects, including classes, excluding built-in objects
* - If assigned a `NodeCallback` function, it provides a custom callback mechanism for handling the operation.
*
* This variable can be used to define the level of depth or customization for a given process.
* @default false
*/
deep?: boolean | 'full' | CallbackFn;
/**
* Indicates whether symbol keys should be included.
* If set to `true`, properties with symbol keys will be considered.
* If `false` or `undefined`, symbol keys will be ignored.
* @default true
*/
symbolKeys?: boolean;
/**
* Specifies the behavior for merging arrays during a particular operation.
*
* When set to `true`, all array elements will be deeply merged, preserving all duplicates.
* When set to `'unique'`, only unique elements will be preserved in the merged array.
* If a callback function (`CallbackFn`) is provided, it determines the custom merging logic for the arrays.
*/
mergeArrays?: boolean | 'unique' | CallbackFn;
/**
* Determines whether to retain pre-existing values.
* If set to `true`, existing entities are preserved without modification.
* If set to `false`, existing entities may be replaced or overridden by new ones.
* Alternatively, can be assigned a callback function (`CallbackFn`) that dynamically resolves whether to keep existing entities based on custom logic.
*/
keepExisting?: boolean | CallbackFn;
/**
* A boolean flag that determines whether property descriptors
* should be copied when transferring properties from one object
* to another.
*
* If set to true, both the value and descriptor metadata
* (e.g., writable, configurable, enumerable) of a property
* will be copied. If set to false or undefined, only the
* property values will be copied, without preserving descriptor
* details.
*
* This is typically used when needing to retain detailed control
* over property attributes during object manipulation.
*/
copyDescriptors?: boolean;
/**
* Ignores the source field if callback returns true
*/
ignoreSource?: CallbackFn;
/**
* Ignore fields which values are "undefined"
* @default true
*/
ignoreUndefined?: boolean;
/**
* Ignore fields which values are "null"
* @default false
*/
ignoreNulls?: boolean;
/**
* Ignores both target and source field if callback returns true
*/
filter?: CallbackFn;
}
}