Refactor manage::codeIsEqual -> storage::styleSectionsEqual

This commit is contained in:
tophf 2017-03-15 14:37:29 +03:00
parent d49622dd27
commit e807d41eb7
2 changed files with 33 additions and 53 deletions

View File

@ -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 || "")) {

View File

@ -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;
}
}
}