From e807d41eb78ec0133629e79d04c8065eb8adaf29 Mon Sep 17 00:00:00 2001 From: tophf Date: Wed, 15 Mar 2017 14:37:29 +0300 Subject: [PATCH] Refactor manage::codeIsEqual -> storage::styleSectionsEqual --- manage.js | 55 ++---------------------------------------------------- storage.js | 31 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 53 deletions(-) diff --git a/manage.js b/manage.js index 97f826fe..955f9e41 100644 --- a/manage.js +++ b/manage.js @@ -1,3 +1,4 @@ +/* globals styleSectionsEqual */ var lastUpdatedStyleId = null; var installed; @@ -257,7 +258,7 @@ function checkUpdate(element, callback) { chrome.runtime.sendMessage({method: "getStyles", id: id}, function(styles) { var style = styles[0]; var needsUpdate = false; - if (!forceUpdate && codeIsEqual(style.sections, serverJson.sections)) { + if (!forceUpdate && styleSectionsEqual(style, serverJson)) { handleNeedsUpdate("no", id, serverJson); } else { handleNeedsUpdate("yes", id, serverJson); @@ -368,58 +369,6 @@ function doUpdate(event) { chrome.runtime.sendMessage(updatedCode, function () {}); } -function codeIsEqual(a, b) { - if (a.length != b.length) { - return false; - } - var properties = ["code", "urlPrefixes", "urls", "domains", "regexps"]; - for (var i = 0; i < a.length; i++) { - var found = false; - for (var j = 0; j < b.length; j++) { - var allEquals = properties.every(function(property) { - return jsonEquals(a[i], b[j], property); - }); - if (allEquals) { - found = true; - break; - } - } - if (!found) { - return false; - } - } - return true; -} - -function jsonEquals(a, b, property) { - var aProp = a[property], typeA = getType(aProp); - var bProp = b[property], typeB = getType(bProp); - if (typeA != typeB) { - // consider empty arrays equivalent to lack of property - if ((typeA == "undefined" || (typeA == "array" && aProp.length == 0)) && (typeB == "undefined" || (typeB == "array" && bProp.length == 0))) { - return true; - } - return false; - } - if (typeA == "undefined") { - return true; - } - if (typeA == "array") { - if (aProp.length != bProp.length) { - return false; - } - for (var i = 0; i < aProp.length; i++) { - if (bProp.indexOf(aProp[i]) == -1) { - return false; - } - } - return true; - } - if (typeA == "string") { - return aProp == bProp; - } -} - function searchStyles(immediately) { var query = document.getElementById("search").value.toLocaleLowerCase(); if (query == (searchStyles.lastQuery || "")) { diff --git a/storage.js b/storage.js index 8ff97738..1a0a1fce 100644 --- a/storage.js +++ b/storage.js @@ -574,3 +574,34 @@ function getSync() { } } } + +function styleSectionsEqual(styleA, styleB) { + if (styleA.sections.length != styleB.sections.length) { + return false; + } + const properties = ['code', 'urlPrefixes', 'urls', 'domains', 'regexps']; + return styleA.sections.every(sectionA => + styleB.sections.some(sectionB => + properties.every(property => sectionEquals(sectionA, sectionB, property)) + ) + ); + + function sectionEquals(a, b, property) { + const aProp = a[property], typeA = getType(aProp); + const bProp = b[property], typeB = getType(bProp); + if (typeA != typeB) { + // consider empty arrays equivalent to lack of property + return ((typeA == 'undefined' || (typeA == 'array' && aProp.length == 0)) && + (typeB == 'undefined' || (typeB == 'array' && bProp.length == 0))); + } + if (typeA == 'undefined') { + return true; + } + if (typeA == 'array') { + return aProp.length == bProp.length && aProp.every(item => bProp.includes(item)); + } + if (typeA == 'string') { + return aProp == bProp; + } + } +}