From 2374c88e1bfae9100bbfc715ac19993f1670983e Mon Sep 17 00:00:00 2001 From: tophf Date: Thu, 4 Jan 2018 16:43:54 +0300 Subject: [PATCH] extract styleSectionsEqual --- background/storage.js | 40 +--------------------------------------- js/sections-equal.js | 38 ++++++++++++++++++++++++++++++++++++++ manage.html | 1 + manifest.json | 1 + 4 files changed, 41 insertions(+), 39 deletions(-) create mode 100644 js/sections-equal.js diff --git a/background/storage.js b/background/storage.js index f3403356..e9378d27 100644 --- a/background/storage.js +++ b/background/storage.js @@ -1,4 +1,4 @@ -/* global getStyleWithNoCode */ +/* global getStyleWithNoCode styleSectionsEqual */ 'use strict'; const RX_NAMESPACE = new RegExp([/[\s\r\n]*/, @@ -539,44 +539,6 @@ function styleCodeEmpty(code) { } -function styleSectionsEqual({sections: a}, {sections: b}) { - if (!a || !b) { - return undefined; - } - if (a.length !== b.length) { - return false; - } - // order of sections should be identical to account for the case of multiple - // sections matching the same URL because the order of rules is part of cascading - return a.every((sectionA, index) => propertiesEqual(sectionA, b[index])); - - function propertiesEqual(secA, secB) { - for (const name of ['urlPrefixes', 'urls', 'domains', 'regexps']) { - if (!equalOrEmpty(secA[name], secB[name], 'every', arrayMirrors)) { - return false; - } - } - return equalOrEmpty(secA.code, secB.code, 'substr', (a, b) => a === b); - } - - function equalOrEmpty(a, b, telltale, comparator) { - const typeA = a && typeof a[telltale] === 'function'; - const typeB = b && typeof b[telltale] === 'function'; - return ( - (a === null || a === undefined || (typeA && !a.length)) && - (b === null || b === undefined || (typeB && !b.length)) - ) || typeA && typeB && a.length === b.length && comparator(a, b); - } - - function arrayMirrors(array1, array2) { - return ( - array1.every(el => array2.includes(el)) && - array2.every(el => array1.includes(el)) - ); - } -} - - function invalidateCache({added, updated, deletedId} = {}) { if (!cachedStyles.list) { return; diff --git a/js/sections-equal.js b/js/sections-equal.js new file mode 100644 index 00000000..457e8978 --- /dev/null +++ b/js/sections-equal.js @@ -0,0 +1,38 @@ +'use strict'; + +function styleSectionsEqual({sections: a}, {sections: b}) { + if (!a || !b) { + return undefined; + } + if (a.length !== b.length) { + return false; + } + // order of sections should be identical to account for the case of multiple + // sections matching the same URL because the order of rules is part of cascading + return a.every((sectionA, index) => propertiesEqual(sectionA, b[index])); + + function propertiesEqual(secA, secB) { + for (const name of ['urlPrefixes', 'urls', 'domains', 'regexps']) { + if (!equalOrEmpty(secA[name], secB[name], 'every', arrayMirrors)) { + return false; + } + } + return equalOrEmpty(secA.code, secB.code, 'substr', (a, b) => a === b); + } + + function equalOrEmpty(a, b, telltale, comparator) { + const typeA = a && typeof a[telltale] === 'function'; + const typeB = b && typeof b[telltale] === 'function'; + return ( + (a === null || a === undefined || (typeA && !a.length)) && + (b === null || b === undefined || (typeB && !b.length)) + ) || typeA && typeB && a.length === b.length && comparator(a, b); + } + + function arrayMirrors(array1, array2) { + return ( + array1.every(el => array2.includes(el)) && + array2.every(el => array1.includes(el)) + ); + } +} diff --git a/manage.html b/manage.html index 0a0ea0dc..aa307c5f 100644 --- a/manage.html +++ b/manage.html @@ -162,6 +162,7 @@ + diff --git a/manifest.json b/manifest.json index 0962f7c6..c573e30a 100644 --- a/manifest.json +++ b/manifest.json @@ -22,6 +22,7 @@ "scripts": [ "js/messaging.js", "js/storage-util.js", + "js/sections-equal.js", "background/storage.js", "js/prefs.js", "js/script-loader.js",