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
45 changes: 45 additions & 0 deletions packages/opentelemetry-core/src/utils/deep-merge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.
*/
function deepMerge(
target: { [prop: string]: any },
naseemkullah marked this conversation as resolved.
Show resolved Hide resolved
source: { [prop: string]: any }
) {
const merged = target;
for (const prop in source) {
dyladan marked this conversation as resolved.
Show resolved Hide resolved
if (propIsArrayInBoth(source, prop, target)) {
merged[prop] = source[prop].concat(target[prop]);
} else if (propIsObj(source, prop)) {
merged[prop] = deepMerge(target[prop], source[prop]);
} else {
merged[prop] = source[prop];
}
}
return merged;
}

function propIsObj(source: { [prop: string]: any }, prop: string) {
return Object.prototype.toString.call(source[prop]) === '[object Object]';
naseemkullah marked this conversation as resolved.
Show resolved Hide resolved
}

function propIsArrayInBoth(
source: { [prop: string]: any },
prop: string,
target: { [prop: string]: any }
) {
return Array.isArray(source[prop]) && Array.isArray(target[prop]);
}

export default deepMerge;
34 changes: 34 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,34 @@
/*
* 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, 2, 3], f: { h: 1, i: 1 } };
const expected = {
a: 1,
b: 2,
c: 2,
d: 1,
e: [1, 2, 3, 4, 5, 6],
f: { g: 1, h: 1, i: 1 },
};
const merged = deepMerge(target, source);
assert.deepEqual(merged, expected);
});
});