aboutsummaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-11-10 13:36:35 -0500
committertoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-11-10 13:36:35 -0500
commit120f97be2601fcf47294d9f4ab1f7c06f7bc6c05 (patch)
tree5f7b5e0471ef4746b3e6f1f5640197fe7376c44f /ext
parentaa92855b37ac7b87f6c0f989d10b7208b543d154 (diff)
Refactor apiOptionsSet
Diffstat (limited to 'ext')
-rw-r--r--ext/bg/js/api.js34
1 files changed, 14 insertions, 20 deletions
diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js
index 3209cc31..9fed99a1 100644
--- a/ext/bg/js/api.js
+++ b/ext/bg/js/api.js
@@ -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);
}