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);
|
const options = await apiOptionsGet(optionsContext);
|
||||||
|
|
||||||
function getValuePaths(obj) {
|
function getValuePaths(obj) {
|
||||||
let valuePaths = [];
|
const valuePaths = [];
|
||||||
let nodes = [{
|
const nodes = [{obj, path: []}];
|
||||||
obj,
|
|
||||||
path: []
|
|
||||||
}];
|
|
||||||
while (nodes.length > 0) {
|
while (nodes.length > 0) {
|
||||||
let node = nodes.pop();
|
const node = nodes.pop();
|
||||||
Object.keys(node.obj).forEach((key) => {
|
for (const key of Object.keys(node.obj)) {
|
||||||
let path = node.path.concat(key);
|
const path = node.path.concat(key);
|
||||||
let value = node.obj[key];
|
const obj = node.obj[key];
|
||||||
if (typeof value === 'object') {
|
if (obj !== null && typeof obj === 'object') {
|
||||||
nodes.unshift({
|
nodes.unshift({obj, path});
|
||||||
obj: value,
|
|
||||||
path: path
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
valuePaths.push([value, path]);
|
valuePaths.push([obj, path]);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
return valuePaths;
|
return valuePaths;
|
||||||
}
|
}
|
||||||
|
|
||||||
function modifyOption(path, value, options) {
|
function modifyOption(path, value, options) {
|
||||||
let pivot = options;
|
let pivot = options;
|
||||||
for (let pathKey of path.slice(0, -1)) {
|
for (const key of path.slice(0, -1)) {
|
||||||
if (!(pathKey in pivot)) {
|
if (!pivot.hasOwnProperty(key)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
pivot = pivot[pathKey];
|
pivot = pivot[key];
|
||||||
}
|
}
|
||||||
pivot[path[path.length - 1]] = value;
|
pivot[path[path.length - 1]] = value;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let [value, path] of getValuePaths(changedOptions)) {
|
for (const [value, path] of getValuePaths(changedOptions)) {
|
||||||
modifyOption(path, value, options);
|
modifyOption(path, value, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user