mirror of
https://github.com/AsgardEternal/SquadJS.git
synced 2024-09-28 17:34:25 -05:00
50d92691c0
Allows to use multiple JSON config files that will be merged into a single configuration
28 lines
765 B
JavaScript
28 lines
765 B
JavaScript
function isObject(item) {
|
|
return item && typeof item === 'object' && !Array.isArray(item);
|
|
}
|
|
|
|
export default class ConfigTools {
|
|
static mergeConfigs(target, ...sources) {
|
|
if (!sources.length) return target;
|
|
const source = sources.shift();
|
|
|
|
if (isObject(target) && isObject(source)) {
|
|
for (const key in source) {
|
|
if (isObject(source[key])) {
|
|
if (!target[key]) Object.assign(target, { [key]: {} });
|
|
ConfigTools.mergeConfigs(target[key], source[key]);
|
|
} else {
|
|
Object.assign(target, { [key]: source[key] });
|
|
}
|
|
}
|
|
}
|
|
|
|
if (Array.isArray(target) && Array.isArray(source)) {
|
|
target = [...target, ...source];
|
|
}
|
|
|
|
return ConfigTools.mergeConfigs(target, ...sources);
|
|
}
|
|
}
|