module-composer
Version:
Bring order to chaos. Level up your JS application architecture with Module Composer, a tiny but powerful module composition utility based on functional dependency injection.
19 lines (15 loc) • 752 B
JavaScript
const isPlainObject = require('./is-plain-object');
module.exports = (obj, opts = {}) => {
const recurse = (obj, parentKey = '', currentDepth = 0) => {
return Object.entries(obj).reduce((acc, [key, val]) => {
const done = !isPlainObject(val);
const newKey = parentKey && opts.delimiter ? parentKey + opts.delimiter + key : key;
if (done) return { ...acc, [newKey]: val };
const changes = recurse(val, opts.delimiter ? newKey : '', currentDepth + 1);
const collision = Object.keys(changes).find(key => acc[key]);
if (collision) throw new Error(`Collision: ${collision}`);
return { ...acc, ...changes };
}, {});
};
return recurse(obj);
};