Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add deep-merge util #1503

Merged
merged 14 commits into from
Sep 23, 2020
Merged
1 change: 1 addition & 0 deletions packages/opentelemetry-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ export * from './trace/sampler/ParentOrElseSampler';
export * from './trace/sampler/ProbabilitySampler';
export * from './trace/TraceState';
export * from './trace/IdGenerator';
export * from './utils/deep-merge';
export * from './utils/url';
export * from './utils/wrap';
67 changes: 67 additions & 0 deletions packages/opentelemetry-core/src/utils/deep-merge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Deeply merges a source object onto a target object.
* If a given property is an array in both source and target, the
* source property replaces the target property entirely.
* @param target
* @param source
* @param maxDepth avoids an infinite CPU loop. Defaults to 10.
* @returns a deeply merged object
*/
export function deepMerge(
naseemkullah marked this conversation as resolved.
Show resolved Hide resolved
target: Record<string, any>,
source: Record<string, any>,
maxDepth = 10
) {
const merged = target;
if (maxDepth === 0) {
throw new Error('Max depth exceeded.');
}
for (const [prop, value] of Object.entries(source)) {
if (bothPropsAreObjects(target, source, prop)) {
if (bothPropsAreArrays(target, source, prop)) {
merged[prop] = value;
} else {
merged[prop] = deepMerge(target[prop], value, maxDepth - 1);
}
} else {
merged[prop] = value;
}
}
return merged;
}

function bothPropsAreObjects(
target: Record<string, any>,
source: Record<string, any>,
prop: string
) {
return propIsObject(target, prop) && propIsObject(source, prop);
}

function propIsObject(object: any, prop: string) {
return typeof object[prop] === 'object' && object[prop] !== null;
}

function bothPropsAreArrays(
target: Record<string, any>,
source: Record<string, any>,
prop: string
) {
return Array.isArray(source[prop]) && Array.isArray(target[prop]);
}
80 changes: 80 additions & 0 deletions packages/opentelemetry-core/test/utils/deep-merge.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as assert from 'assert';
import { deepMerge } from '../../src/utils/deep-merge';

describe('deepMerge', () => {
it('should deep merge two objects', () => {
naseemkullah marked this conversation as resolved.
Show resolved Hide resolved
const target = { a: 1, b: 1, d: 1, e: [4, 5, 6], f: { g: 1, h: 2 } };
const source = { a: 1, b: 2, c: 2, e: [1], f: { h: 1, i: 1 } };
const expected = {
a: 1,
b: 2,
c: 2,
d: 1,
e: [1],
f: { g: 1, h: 1, i: 1 },
};
const merged = deepMerge(target, source);
assert.deepEqual(merged, expected);
});

it('should replace array-props', () => {
const target = { a: [4, 5, 6] };
const source = { a: [1] };
const expected = {
a: [1],
};
const merged = deepMerge(target, source);
assert.deepEqual(merged, expected);
});

it('should override a primitive target prop by a structural source prop', () => {
const merged = deepMerge({ a: 1, b: 'c' }, { a: { b: 1 }, b: [{ d: 1 }] });
assert.deepEqual(merged, { a: { b: 1 }, b: [{ d: 1 }] });
});

it('should nullify and undefine as per source props', () => {
const target = { a: 1, b: 1, d: 1, e: [4, 5, 6], f: { g: 1, h: 2 } };
const source = {
a: null,
b: undefined,
c: 2,
e: [1, null, undefined],
f: { h: null, i: undefined },
};
const expected = {
a: null,
b: undefined,
c: 2,
d: 1,
e: [1, null, undefined],
f: { g: 1, h: null, i: undefined },
};
const merged = deepMerge(target, source);
assert.deepEqual(merged, expected);
});

it('should respect the max depth', () => {
assert.throws(() => {
deepMerge(
{ a: { a: { a: { a: { a: { a: 1 } } } } } },
{ a: { a: { a: { a: { a: { a: 1 } } } } } },
5
);
});
});
});