mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 01:38:56 -04:00
[Integration Assistant] Improve sample merge functionality (#190656)
## Summary Adds a few extra checks when doing deep recursive merges, will test a few things when it comes to the overhead it adds before merging. ### For maintainers - [x] This was checked for breaking API changes and was [labeled appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)
This commit is contained in:
parent
d69e598e30
commit
d971c6a10e
1 changed files with 18 additions and 15 deletions
|
@ -55,22 +55,25 @@ function isEmptyValue(value: unknown): boolean {
|
|||
|
||||
function merge(target: Record<string, any>, source: Record<string, any>): Record<string, unknown> {
|
||||
for (const [key, sourceValue] of Object.entries(source)) {
|
||||
const targetValue = target[key];
|
||||
if (Array.isArray(sourceValue)) {
|
||||
// Directly assign arrays
|
||||
target[key] = sourceValue;
|
||||
} else if (
|
||||
typeof sourceValue === 'object' &&
|
||||
sourceValue !== null &&
|
||||
!Array.isArray(targetValue)
|
||||
) {
|
||||
if (typeof targetValue !== 'object' || isEmptyValue(targetValue)) {
|
||||
target[key] = merge({}, sourceValue);
|
||||
} else {
|
||||
target[key] = merge(targetValue, sourceValue);
|
||||
if (key !== '__proto__' && key !== 'constructor') {
|
||||
if (Object.prototype.hasOwnProperty.call(target, key)) {
|
||||
const targetValue = target[key];
|
||||
if (Array.isArray(sourceValue)) {
|
||||
target[key] = sourceValue;
|
||||
} else if (
|
||||
typeof sourceValue === 'object' &&
|
||||
sourceValue !== null &&
|
||||
typeof targetValue === 'object' &&
|
||||
targetValue !== null &&
|
||||
!Array.isArray(targetValue)
|
||||
) {
|
||||
target[key] = merge(targetValue, sourceValue);
|
||||
} else if (isEmptyValue(targetValue) && !isEmptyValue(sourceValue)) {
|
||||
target[key] = sourceValue;
|
||||
}
|
||||
} else if (!isEmptyValue(sourceValue)) {
|
||||
target[key] = sourceValue;
|
||||
}
|
||||
} else if (!(key in target) || (isEmptyValue(targetValue) && !isEmptyValue(sourceValue))) {
|
||||
target[key] = sourceValue;
|
||||
}
|
||||
}
|
||||
return target;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue