respond-framework
Version:
create as fast you think
52 lines (48 loc) • 2.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.nestAllSettings = nestAllSettings;
exports.nestFocusedSettings = nestFocusedSettings;
var _sliceBranch = require("../../../createModule/helpers/sliceBranch.js");
var _nestAtBranch = require("./nestAtBranch.js");
var _findClosestAncestorWith = require("../../../createModule/helpers/findClosestAncestorWith.js");
function nestAllSettings(configs, settings) {
const references = new Map();
return Object.keys(settings).reduce((acc, b) => {
let mod = settings[b];
if (references.has(mod)) return acc; // inherited settings share the same reference, and won't need to appear more than once
references.set(mod, true);
mod = ignoreDefaults(configs[b], mod);
const clean = JSON.parse(JSON.stringify(mod)); // remove undefined keys
if (Object.keys(clean).length === 0) return acc;
if (b === '') return {
...clean
}; // top module's settings becomes the root object, and it will be first, as settings form is pre-sorted ancestors first
(0, _nestAtBranch.default)(b, clean, acc); // if a parent reference is ignored because it shares the same replay settings as the grand parent, a grand child with its own replays will be nested in an empty parent object by this function
return acc;
}, {});
}
const ignoreDefaults = (config, mod) => Object.keys(mod).reduce((acc, k) => {
const v = mod[k];
if (v === config[k].defaultValueDevelopment) return acc;
acc[k] = v;
return acc;
}, {});
function nestFocusedSettings(configs, settings, focusedBranch, respond) {
const nestedSettings = nestAllSettings(configs, settings);
const mod = (0, _sliceBranch.default)(respond.top, focusedBranch);
const hasDb = mod.db || mod.replays?.noDbDependency;
if (hasDb) return (0, _sliceBranch.default)(nestedSettings, focusedBranch) ?? {}; // undefined could happen if all settings undefined, due to "remove undefined keys" above
const depMod = (0, _findClosestAncestorWith.default)('db', focusedBranch, respond) ?? respond.top;
const depBranch = depMod.branchAbsolute;
const depSettings = (0, _sliceBranch.default)(nestedSettings, depBranch) ?? {};
const focusedSettings = (0, _sliceBranch.default)(nestedSettings, focusedBranch) ?? {};
depSettings.settingsBranchDependency = depBranch; // branch needs to be assigned if branch can't be inferred by the module that the __tests__ folder is in -- due to inherting db from a parent module
return removeOutOfBandBranches(depMod, depSettings, focusedSettings, depBranch, focusedBranch);
}
const removeOutOfBandBranches = (depMod, depSettings, focusedSettings, depBranch, focusedBranch) => {
depMod.moduleKeys.forEach(k => delete depSettings[k]);
const relativeFocusedBranch = (0, _sliceBranch.strip)(depBranch, focusedBranch);
return (0, _nestAtBranch.default)(relativeFocusedBranch, focusedSettings, depSettings);
};