bigbluebutton-Github/bigbluebutton-html5/imports/utils/deepMerge.js

32 lines
698 B
JavaScript
Raw Normal View History

2017-06-19 21:13:35 +08:00
import { Match } from 'meteor/check';
export default function deepMerge(merge, ...sources) {
if (!sources.length) return merge;
2017-06-19 21:13:35 +08:00
const source = sources.shift();
let merged = merge;
if (Match.test(merged, Array)) {
2017-06-19 21:13:35 +08:00
if (Match.test(source, Array)) {
merged.push(...source);
2017-06-19 21:13:35 +08:00
} else {
merged.push(source);
2017-06-19 21:13:35 +08:00
}
} else if (Match.test(merged, Object)) {
2017-06-19 21:13:35 +08:00
if (Match.test(source, Object)) {
Object.keys(source).forEach((key) => {
if (!merged[key]) {
merged[key] = source[key];
2017-06-19 21:13:35 +08:00
} else {
deepMerge(merged[key], source[key]);
2017-06-19 21:13:35 +08:00
}
});
}
} else {
merged = source;
2017-06-19 21:13:35 +08:00
}
return deepMerge(merged, ...sources);
2017-06-19 21:13:35 +08:00
}