svelte-settings
Version:
> [!WARNING] > This project is a work in progress. Do not use it in any of your projects yet.
28 lines (27 loc) • 810 B
JavaScript
export function extractDefaults(items) {
const result = {};
function assign(path, value) {
let obj = result;
for (let i = 0; i < path.length - 1; i++) {
const key = path[i];
obj = obj[key] ??= {};
}
obj[path.at(-1)] = value;
}
function walk(config, path = []) {
for (const item of config) {
if ('visible' in item && item.visible)
continue;
if ('default' in item) {
assign([...path, item.id], item.default);
}
if ('children' in item) {
walk(
// @ts-expect-error defaults not existing doesn't matter
item.children, [...path, item.id]);
}
}
}
walk(items);
return result;
}