jsm-core
Version:
Core library for JSM project
28 lines (27 loc) • 876 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.flattenConfig = flattenConfig;
/**
* Flattens a nested configuration object into a key-value pair object
* with keys represented in dot notation.
*
* @param {object} config - The nested configuration object.
* @returns {Record<string, any>} - The flattened configuration object.
*/
function flattenConfig(config) {
const result = {};
function recurse(obj, currentKey) {
for (const key in obj) {
const value = obj[key];
const newKey = currentKey ? `${currentKey}.${key}` : key;
if (value && typeof value === "object" && !Array.isArray(value)) {
recurse(value, newKey);
}
else {
result[newKey] = value;
}
}
}
recurse(config, "");
return result;
}