const getPaths = (obj, basePath = []) => _.reduce( (memo, path) => _.isPlainObject(obj[path]) ? [...memo, ...getPaths(obj[path], [...basePath, path])] : [...memo, [...basePath, path]], [], _.keys(obj) )
const createAllModules = (dependencyGraph, existingModules, moduleDefinitions) => f.flow( f.keys, f.reduce( (accumulatedModules, name) => createModule(name, dependencyGraph, accumulatedModules, moduleDefinitions), existingModules ) )(dependencyGraph)
/** * Get an object with {key: configueValue(key)} * @param args the keys to form object with * @returns {object} the forged object */ function getObject(...args) { const keys = args.length === 1 && Array.isArray(_.first(args)) ? _.first(args) : args; return _.reduce( (memo, key) => { const [fromKey, toKey] = Array.isArray(key) ? key : [key, key]; return _.set(toKey, this.get(fromKey), memo); }, {}, keys ); }
const configueTemplate = (configue, defaults = {}) => (chains, ...keys) => { return _.reduce( (acc, [chain, key]) => { const base = acc + chain; if (!key) return base; return base + configue.get(formatKey(key), _.get(key, defaults)); }, '', _.zip(chains, keys) ); }
// returns new modules hash const createModule = (name, dependencyGraph, existingModules, moduleDefinitions) => { // check if module already exists const existingDependency = existingModules[name]; if (existingDependency) { return existingModules; } // create dependencies recursively const moduleDependencies = dependencyGraph[name]; const updatedDependencies = f.reduce( (accumulatedModules, name) => f.assign( accumulatedModules, createModule(name, dependencyGraph, accumulatedModules, moduleDefinitions) ), existingModules )(moduleDependencies); // create module const createdModule = moduleDefinitions[name](updatedDependencies); // return dependencies with new module return f.assign(updatedDependencies, { [name]: createdModule }); }
const populateObj = (configue, obj, paths) => _.reduce((memo, path) => _.set(path, configue.getFirst(_.get(path, obj)), memo), {}, paths)