aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-01-07 21:36:20 -0500
committerGitHub <noreply@github.com>2021-01-07 21:36:20 -0500
commitb20622b2c84ce3ca1781c7bf8e10fed0af1e5001 (patch)
tree938cdc95f289cdfd482aff77a6ebe44aff80e889 /ext/bg/js
parent7d706df66b4cd68d6ac430c2e3e8b9f3a7f4b6ae (diff)
Core refactor (#1207)
* Copy set intersection functions * Remove unused functions * Simplify url check * Remove parseUrl * Simplify stringReverse * Remove hasOwn due to infrequent use * Rename errorToJson/jsonToError to de/serializeError For clarity on intended use. * Fix time argument on timeout * Add missing return value * Throw an error for unexpected argument values * Add documentation comments
Diffstat (limited to 'ext/bg/js')
-rw-r--r--ext/bg/js/audio-downloader.js2
-rw-r--r--ext/bg/js/backend.js16
-rw-r--r--ext/bg/js/options.js2
-rw-r--r--ext/bg/js/settings/dictionary-import-controller.js2
-rw-r--r--ext/bg/js/settings/generic-setting-controller.js2
-rw-r--r--ext/bg/js/template-renderer-proxy.js2
-rw-r--r--ext/bg/js/translator.js32
7 files changed, 42 insertions, 16 deletions
diff --git a/ext/bg/js/audio-downloader.js b/ext/bg/js/audio-downloader.js
index 9b34be56..19e9c368 100644
--- a/ext/bg/js/audio-downloader.js
+++ b/ext/bg/js/audio-downloader.js
@@ -189,7 +189,7 @@ class AudioDownloader {
throw new Error('No custom URL defined');
}
const data = {expression, reading};
- const url = customSourceUrl.replace(/\{([^}]*)\}/g, (m0, m1) => (hasOwn(data, m1) ? `${data[m1]}` : m0));
+ const url = customSourceUrl.replace(/\{([^}]*)\}/g, (m0, m1) => (Object.prototype.hasOwnProperty.call(data, m1) ? `${data[m1]}` : m0));
return {type: 'url', details: {url}};
}
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index 690f6a3c..db83d0b7 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -305,7 +305,7 @@ class Backend {
try {
this._validatePrivilegedMessageSender(sender);
} catch (error) {
- callback({error: errorToJson(error)});
+ callback({error: serializeError(error)});
return false;
}
}
@@ -628,7 +628,7 @@ class Backend {
}
_onApiLog({error, level, context}) {
- yomichan.log(jsonToError(error), level, context);
+ yomichan.log(deserializeError(error), level, context);
}
_onApiLogIndicatorClear() {
@@ -667,7 +667,7 @@ class Backend {
const result = this._modifySetting(target);
results.push({result: clone(result)});
} catch (e) {
- results.push({error: errorToJson(e)});
+ results.push({error: serializeError(e)});
}
}
await this._saveOptions(source);
@@ -681,7 +681,7 @@ class Backend {
const result = this._getSetting(target);
results.push({result: clone(result)});
} catch (e) {
- results.push({error: errorToJson(e)});
+ results.push({error: serializeError(e)});
}
}
return results;
@@ -730,8 +730,10 @@ class Backend {
const isTabMatch = (url2) => {
if (url2 === null || !url2.startsWith(baseUrl)) { return false; }
- const {baseUrl: baseUrl2, queryParams: queryParams2} = parseUrl(url2);
- return baseUrl2 === baseUrl && (queryParams2.mode === mode || (!queryParams2.mode && mode === 'existingOrNewTab'));
+ const parsedUrl = new URL(url2);
+ const baseUrl2 = `${parsedUrl.origin}${parsedUrl.pathname}`;
+ const mode2 = parsedUrl.searchParams.get('mode');
+ return baseUrl2 === baseUrl && (mode2 === mode || (!mode2 && mode === 'existingOrNewTab'));
};
const openInTab = async () => {
@@ -1052,7 +1054,7 @@ class Backend {
const cleanup = (error) => {
if (port === null) { return; }
if (error !== null) {
- port.postMessage({type: 'error', data: errorToJson(error)});
+ port.postMessage({type: 'error', data: serializeError(error)});
}
if (!hasStarted) {
port.onMessage.removeListener(onMessage);
diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js
index 16168e38..adf3cfd4 100644
--- a/ext/bg/js/options.js
+++ b/ext/bg/js/options.js
@@ -343,7 +343,7 @@ class OptionsUtil {
const combine = (target, source) => {
for (const key in source) {
- if (!hasOwn(target, key)) {
+ if (!Object.prototype.hasOwnProperty.call(target, key)) {
target[key] = source[key];
}
}
diff --git a/ext/bg/js/settings/dictionary-import-controller.js b/ext/bg/js/settings/dictionary-import-controller.js
index f392e5a9..436a056c 100644
--- a/ext/bg/js/settings/dictionary-import-controller.js
+++ b/ext/bg/js/settings/dictionary-import-controller.js
@@ -338,7 +338,7 @@ class DictionaryImportController {
const errors = [];
for (const {error} of results) {
if (typeof error !== 'undefined') {
- errors.push(jsonToError(error));
+ errors.push(deserializeError(error));
}
}
return errors;
diff --git a/ext/bg/js/settings/generic-setting-controller.js b/ext/bg/js/settings/generic-setting-controller.js
index 95be975e..0d24c429 100644
--- a/ext/bg/js/settings/generic-setting-controller.js
+++ b/ext/bg/js/settings/generic-setting-controller.js
@@ -113,7 +113,7 @@ class GenericSettingController {
_transformResults(values, targets) {
return values.map((value, i) => {
const error = value.error;
- if (error) { return jsonToError(error); }
+ if (error) { return deserializeError(error); }
const {metadata: {transforms}, element} = targets[i];
const result = this._applyTransforms(value.result, transforms, 'post', element);
return {result};
diff --git a/ext/bg/js/template-renderer-proxy.js b/ext/bg/js/template-renderer-proxy.js
index 507d8067..04b53ce7 100644
--- a/ext/bg/js/template-renderer-proxy.js
+++ b/ext/bg/js/template-renderer-proxy.js
@@ -127,7 +127,7 @@ class TemplateRendererProxy {
cleanup();
const {error} = response;
if (error) {
- reject(jsonToError(error));
+ reject(deserializeError(error));
} else {
resolve(response.result);
}
diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js
index 8cc520a8..7242ec56 100644
--- a/ext/bg/js/translator.js
+++ b/ext/bg/js/translator.js
@@ -980,6 +980,30 @@ class Translator {
}
}
+ _areSetsEqual(set1, set2) {
+ if (set1.size !== set2.size) {
+ return false;
+ }
+
+ for (const value of set1) {
+ if (!set2.has(value)) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ _getSetIntersection(set1, set2) {
+ const result = [];
+ for (const value of set1) {
+ if (set2.has(value)) {
+ result.push(value);
+ }
+ }
+ return result;
+ }
+
// Reduction functions
_getTermTagsScoreSum(termTags) {
@@ -1181,11 +1205,11 @@ class Translator {
_createMergedGlossaryTermDefinition(source, rawSource, definitions, expressions, readings, allExpressions, allReadings) {
const only = [];
- if (!areSetsEqual(expressions, allExpressions)) {
- only.push(...getSetIntersection(expressions, allExpressions));
+ if (!this._areSetsEqual(expressions, allExpressions)) {
+ only.push(...this._getSetIntersection(expressions, allExpressions));
}
- if (!areSetsEqual(readings, allReadings)) {
- only.push(...getSetIntersection(readings, allReadings));
+ if (!this._areSetsEqual(readings, allReadings)) {
+ only.push(...this._getSetIntersection(readings, allReadings));
}
const sourceTermExactMatchCount = this._getSourceTermMatchCountSum(definitions);