From 120f97be2601fcf47294d9f4ab1f7c06f7bc6c05 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 10 Nov 2019 13:36:35 -0500 Subject: [PATCH] Refactor apiOptionsSet --- ext/bg/js/api.js | 34 ++++++++++++++-------------------- 1 file 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); }