Refactor apiOptionsSet
This commit is contained in:
parent
aa92855b37
commit
120f97be26
@ -25,42 +25,36 @@ async function apiOptionsSet(changedOptions, optionsContext, source) {
|
||||
const options = await apiOptionsGet(optionsContext);
|
||||
|
||||
function getValuePaths(obj) {
|
||||
let valuePaths = [];
|
||||
let nodes = [{
|
||||
obj,
|
||||
path: []
|
||||
}];
|
||||
const valuePaths = [];
|
||||
const nodes = [{obj, path: []}];
|
||||
while (nodes.length > 0) {
|
||||
let node = nodes.pop();
|
||||
Object.keys(node.obj).forEach((key) => {
|
||||
let path = node.path.concat(key);
|
||||
let value = node.obj[key];
|
||||
if (typeof value === 'object') {
|
||||
nodes.unshift({
|
||||
obj: value,
|
||||
path: path
|
||||
});
|
||||
const node = nodes.pop();
|
||||
for (const key of Object.keys(node.obj)) {
|
||||
const path = node.path.concat(key);
|
||||
const obj = node.obj[key];
|
||||
if (obj !== null && typeof obj === 'object') {
|
||||
nodes.unshift({obj, path});
|
||||
} else {
|
||||
valuePaths.push([value, path]);
|
||||
valuePaths.push([obj, path]);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
return valuePaths;
|
||||
}
|
||||
|
||||
function modifyOption(path, value, options) {
|
||||
let pivot = options;
|
||||
for (let pathKey of path.slice(0, -1)) {
|
||||
if (!(pathKey in pivot)) {
|
||||
for (const key of path.slice(0, -1)) {
|
||||
if (!pivot.hasOwnProperty(key)) {
|
||||
return false;
|
||||
}
|
||||
pivot = pivot[pathKey];
|
||||
pivot = pivot[key];
|
||||
}
|
||||
pivot[path[path.length - 1]] = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
for (let [value, path] of getValuePaths(changedOptions)) {
|
||||
for (const [value, path] of getValuePaths(changedOptions)) {
|
||||
modifyOption(path, value, options);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user