Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@hapi/hoek/lib/merge.js
1126 views
1
'use strict';
2
3
const Assert = require('./assert');
4
const Clone = require('./clone');
5
const Utils = require('./utils');
6
7
8
const internals = {};
9
10
11
module.exports = internals.merge = function (target, source, options) {
12
13
Assert(target && typeof target === 'object', 'Invalid target value: must be an object');
14
Assert(source === null || source === undefined || typeof source === 'object', 'Invalid source value: must be null, undefined, or an object');
15
16
if (!source) {
17
return target;
18
}
19
20
options = Object.assign({ nullOverride: true, mergeArrays: true }, options);
21
22
if (Array.isArray(source)) {
23
Assert(Array.isArray(target), 'Cannot merge array onto an object');
24
if (!options.mergeArrays) {
25
target.length = 0; // Must not change target assignment
26
}
27
28
for (let i = 0; i < source.length; ++i) {
29
target.push(Clone(source[i], { symbols: options.symbols }));
30
}
31
32
return target;
33
}
34
35
const keys = Utils.keys(source, options);
36
for (let i = 0; i < keys.length; ++i) {
37
const key = keys[i];
38
if (key === '__proto__' ||
39
!Object.prototype.propertyIsEnumerable.call(source, key)) {
40
41
continue;
42
}
43
44
const value = source[key];
45
if (value &&
46
typeof value === 'object') {
47
48
if (target[key] === value) {
49
continue; // Can occur for shallow merges
50
}
51
52
if (!target[key] ||
53
typeof target[key] !== 'object' ||
54
(Array.isArray(target[key]) !== Array.isArray(value)) ||
55
value instanceof Date ||
56
(Buffer && Buffer.isBuffer(value)) || // $lab:coverage:ignore$
57
value instanceof RegExp) {
58
59
target[key] = Clone(value, { symbols: options.symbols });
60
}
61
else {
62
internals.merge(target[key], value, options);
63
}
64
}
65
else {
66
if (value !== null &&
67
value !== undefined) { // Explicit to preserve empty strings
68
69
target[key] = value;
70
}
71
else if (options.nullOverride) {
72
target[key] = value;
73
}
74
}
75
}
76
77
return target;
78
};
79
80