aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuchen Pei <hi@ypei.me>2022-07-28 17:32:12 +1000
committerYuchen Pei <hi@ypei.me>2022-07-28 17:32:12 +1000
commitb488bb3a6188c2ade143b5ea4cddf8866579536a (patch)
treeb27201c721933bef908a685a6f96fe59b2d5e9ed
parenta6b8bfef1a92c168d0c541d28c4ea834462bbf64 (diff)
clean up common/Storage.js
-rw-r--r--common/Storage.js38
1 files changed, 14 insertions, 24 deletions
diff --git a/common/Storage.js b/common/Storage.js
index 6254d66..5eaff77 100644
--- a/common/Storage.js
+++ b/common/Storage.js
@@ -25,13 +25,14 @@
*/
'use strict';
-var Storage = {
+const jssha = require('jssha');
+
+const Storage = {
ARRAY: {
async load(key, array = undefined) {
- if (array === undefined) {
- array = (await browser.storage.local.get(key))[key];
- }
- return array ? new Set(array) : new Set();
+ const result = array === undefined ?
+ (await browser.storage.local.get(key))[key] : array;
+ return result ? new Set(result) : new Set();
},
async save(key, list) {
return await browser.storage.local.set({ [key]: [...list] });
@@ -40,7 +41,7 @@ var Storage = {
CSV: {
async load(key) {
- let csv = (await browser.storage.local.get(key))[key];
+ const csv = (await browser.storage.local.get(key))[key];
return csv ? new Set(csv.split(/\s*,\s*/)) : new Set();
},
@@ -77,7 +78,7 @@ class ListStore {
return hash.startsWith('(') ? hash : `(${hash})`;
}
static urlItem(url) {
- let queryPos = url.indexOf('?');
+ const queryPos = url.indexOf('?');
return queryPos === -1 ? url : url.substring(0, queryPos);
}
static siteItem(url) {
@@ -108,24 +109,14 @@ class ListStore {
}
async store(...items) {
- let size = this.items.size;
- let changed = false;
- for (let item of items) {
- if (size !== this.items.add(item).size) {
- changed = true;
- }
- }
- return changed && await this.save();
+ const size = this.items.size;
+ const changed = items.reduce((current, item) => size !== this.items.add(item).size || current, false);
+ changed && await this.save();
}
async remove(...items) {
- let changed = false;
- for (let item of items) {
- if (this.items.delete(item)) {
- changed = true;
- }
- }
- return changed && await this.save();
+ const changed = items.reduce((current, item) => this.items.delete(item) || current, false);
+ changed && await this.save();
}
contains(item) {
@@ -134,12 +125,11 @@ class ListStore {
}
function hash(source) {
- var shaObj = new jssha('SHA-256', 'TEXT')
+ const shaObj = new jssha('SHA-256', 'TEXT')
shaObj.update(source);
return shaObj.getHash('HEX');
}
if (typeof module === 'object') {
module.exports = { ListStore, Storage, hash };
- var jssha = require('jssha');
}