Skip to content

Commit

Permalink
Object utilities
Browse files Browse the repository at this point in the history
* Create utilities

* Rename old isObject

* Use new isObject

* Remove old function

* Add additional function

* Simplify for now

* Rename function for clarity

* Rename function

* Expand type

* Update eslint

<rikaitan.link>H4sIAAAAAAAAAwXBQQEAIAgDwL9pUBAxDuDWP4J3sJNCi6C+W9V1HNU07iSWaE9AvGV8rHloACkAAAA=</rikaitan.link>
  • Loading branch information
toasted-nutbread authored and tatsumoto-ren committed Feb 28, 2024
1 parent 18e0139 commit 8af46a9
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 47 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@
"ext/js/core/json.js",
"ext/js/core/log-utilities.js",
"ext/js/core/log.js",
"ext/js/core/object-utilities.js",
"ext/js/core/to-error.js",
"ext/js/core/utilities.js",
"ext/js/data/anki-util.js",
Expand Down
15 changes: 8 additions & 7 deletions ext/js/background/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import {ExtensionError} from '../core/extension-error.js';
import {fetchJson, fetchText} from '../core/fetch-utilities.js';
import {logErrorLevelToNumber} from '../core/log-utilities.js';
import {log} from '../core/log.js';
import {clone, deferPromise, isObject, promiseTimeout} from '../core/utilities.js';
import {isObjectNotArray} from '../core/object-utilities.js';
import {clone, deferPromise, promiseTimeout} from '../core/utilities.js';
import {isNoteDataValid} from '../data/anki-util.js';
import {OptionsUtil} from '../data/options-util.js';
import {getAllPermissions, hasPermissions, hasRequiredPermissionsForOptions} from '../data/permissions-util.js';
Expand Down Expand Up @@ -222,12 +223,12 @@ export class Backend {
* @returns {void}
*/
_prepareInternalSync() {
if (isObject(chrome.commands) && isObject(chrome.commands.onCommand)) {
if (isObjectNotArray(chrome.commands) && isObjectNotArray(chrome.commands.onCommand)) {
const onCommand = this._onWebExtensionEventWrapper(this._onCommand.bind(this));
chrome.commands.onCommand.addListener(onCommand);
}

if (isObject(chrome.tabs) && isObject(chrome.tabs.onZoomChange)) {
if (isObjectNotArray(chrome.tabs) && isObjectNotArray(chrome.tabs.onZoomChange)) {
const onZoomChange = this._onWebExtensionEventWrapper(this._onZoomChange.bind(this));
chrome.tabs.onZoomChange.addListener(onZoomChange);
}
Expand Down Expand Up @@ -1094,7 +1095,7 @@ export class Backend {
}

// chrome.windows not supported (e.g. on Firefox mobile)
if (!isObject(chrome.windows)) {
if (!isObjectNotArray(chrome.windows)) {
throw new Error('Window creation not supported');
}

Expand Down Expand Up @@ -1561,7 +1562,7 @@ export class Backend {
*/
_getBrowserIconTitle() {
return (
isObject(chrome.action) &&
isObjectNotArray(chrome.action) &&
typeof chrome.action.getTitle === 'function' ?
new Promise((resolve) => { chrome.action.getTitle({}, resolve); }) :
Promise.resolve('')
Expand All @@ -1573,7 +1574,7 @@ export class Backend {
*/
_updateBadge() {
let title = this._defaultBrowserActionTitle;
if (title === null || !isObject(chrome.action)) {
if (title === null || !isObjectNotArray(chrome.action)) {
// Not ready or invalid
return;
}
Expand Down Expand Up @@ -2580,7 +2581,7 @@ export class Backend {
* @returns {boolean}
*/
_canObservePermissionsChanges() {
return isObject(chrome.permissions) && isObject(chrome.permissions.onAdded) && isObject(chrome.permissions.onRemoved);
return isObjectNotArray(chrome.permissions) && isObjectNotArray(chrome.permissions.onAdded) && isObjectNotArray(chrome.permissions.onRemoved);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions ext/js/background/offscreen-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/

import {ExtensionError} from '../core/extension-error.js';
import {isObject} from '../core/utilities.js';
import {isObjectNotArray} from '../core/object-utilities.js';
import {base64ToArrayBuffer} from '../data/sandbox/array-buffer-util.js';

/**
Expand Down Expand Up @@ -123,7 +123,7 @@ export class OffscreenProxy {
if (typeof runtimeError !== 'undefined') {
throw new Error(runtimeError.message);
}
if (!isObject(response)) {
if (!isObjectNotArray(response)) {
throw new Error('Offscreen document did not respond');
}
const responseError = response.error;
Expand Down
7 changes: 4 additions & 3 deletions ext/js/comm/frame-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import {deferPromise, generateId, isObject} from '../core/utilities.js';
import {isObjectNotArray} from '../core/object-utilities.js';
import {deferPromise, generateId} from '../core/utilities.js';

export class FrameClient {
constructor() {
Expand Down Expand Up @@ -121,9 +122,9 @@ export class FrameClient {
*/
const onMessageInner = async (message) => {
try {
if (!isObject(message)) { return; }
if (!isObjectNotArray(message)) { return; }
const {action, params} = message;
if (!isObject(params)) { return; }
if (!isObjectNotArray(params)) { return; }
await frameLoadedPromise;
if (timer === null) { return; } // Done

Expand Down
32 changes: 32 additions & 0 deletions ext/js/core/object-utilities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2024 Ajatt-Tools and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

/**
* @param {unknown} value
* @returns {value is Record<string, unknown>}
*/
export function isObjectNotArray(value) {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}

/**
* @param {unknown} value
* @returns {value is Record<string|number|symbol, unknown>}
*/
export function isObject(value) {
return typeof value === 'object' && value !== null;
}
9 changes: 0 additions & 9 deletions ext/js/core/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

/**
* Checks whether a given value is a non-array object.
* @param {unknown} value The value to check.
* @returns {boolean} `true` if the value is an object and not an array, `false` otherwise.
*/
export function isObject(value) {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}

/**
* Converts any string into a form that can be passed into the RegExp constructor.
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
Expand Down
4 changes: 2 additions & 2 deletions ext/js/data/anki-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import {isObject} from '../core/utilities.js';
import {isObjectNotArray} from '../core/object-utilities.js';

/** @type {RegExp} @readonly */
const markerPattern = /\{([\w-]+)\}/g;
Expand Down Expand Up @@ -74,7 +74,7 @@ export function cloneFieldMarkerPattern(global) {
* @returns {boolean} `true` if the note is valid, `false` otherwise.
*/
export function isNoteDataValid(note) {
if (!isObject(note)) { return false; }
if (!isObjectNotArray(note)) { return false; }
const {fields, deckName, modelName} = note;
return (
typeof deckName === 'string' &&
Expand Down
5 changes: 3 additions & 2 deletions ext/js/data/options-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

import {fetchJson, fetchText} from '../core/fetch-utilities.js';
import {parseJson} from '../core/json.js';
import {escapeRegExp, isObject} from '../core/utilities.js';
import {isObjectNotArray} from '../core/object-utilities.js';
import {escapeRegExp} from '../core/utilities.js';
import {TemplatePatcher} from '../templates/template-patcher.js';
import {JsonSchema} from './json-schema.js';

Expand Down Expand Up @@ -70,7 +71,7 @@ export class OptionsUtil {
// Remove invalid profiles
const profiles = /** @type {unknown[]} */ (options.profiles);
for (let i = profiles.length - 1; i >= 0; --i) {
if (!isObject(profiles[i])) {
if (!isObjectNotArray(profiles[i])) {
profiles.splice(i, 1);
}
}
Expand Down
3 changes: 1 addition & 2 deletions ext/js/display/display-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/

import {ExtensionError} from '../core/extension-error.js';
import {isObject} from '../core/utilities.js';
import {getDisambiguations, getGroupedPronunciations, getTermFrequency, groupKanjiFrequencies, groupTermFrequencies, groupTermTags, isNonNounVerbOrAdjective} from '../dictionary/dictionary-data-util.js';
import {HtmlTemplateCollection} from '../dom/html-template-collection.js';
import {distributeFurigana, getKanaMorae, getPitchCategory, isCodePointKanji} from '../language/ja/japanese.js';
Expand Down Expand Up @@ -257,7 +256,7 @@ export class DisplayGenerator {
if (error instanceof DocumentFragment || error instanceof Node) {
div.appendChild(error);
} else {
let message = isObject(error) && typeof error.message === 'string' ? error.message : `${error}`;
let message = error.message;
let link = null;
if (error instanceof ExtensionError && error.data !== null && typeof error.data === 'object') {
const {referenceUrl} = /** @type {import('core').UnknownObject} */ (error.data);
Expand Down
9 changes: 5 additions & 4 deletions ext/js/display/display-history.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
*/

import {EventDispatcher} from '../core/event-dispatcher.js';
import {generateId, isObject} from '../core/utilities.js';
import {isObjectNotArray} from '../core/object-utilities.js';
import {generateId} from '../core/utilities.js';

/**
* @augments EventDispatcher<import('display-history').Events>
Expand All @@ -38,12 +39,12 @@ export class DisplayHistory extends EventDispatcher {

const historyState = history.state;
const {id, state} = (
typeof historyState === 'object' && historyState !== null ?
isObjectNotArray(historyState) ?
historyState :
{id: null, state: null}
);
/** @type {?import('display-history').EntryState} */
const stateObject = typeof state === 'object' || state === null ? state : null;
const stateObject = isObjectNotArray(state) ? state : null;
/** @type {import('display-history').Entry} */
this._current = this._createHistoryEntry(id, location.href, stateObject, null, null);
}
Expand Down Expand Up @@ -189,7 +190,7 @@ export class DisplayHistory extends EventDispatcher {
_updateStateFromHistory() {
let state = history.state;
let id = null;
if (isObject(state)) {
if (isObjectNotArray(state)) {
id = state.id;
if (typeof id === 'string') {
const entry = this._historyMap.get(id);
Expand Down
4 changes: 2 additions & 2 deletions ext/js/input/hotkey-help-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/

import {parseJson} from '../core/json.js';
import {isObject} from '../core/utilities.js';
import {isObjectNotArray} from '../core/object-utilities.js';
import {HotkeyUtil} from './hotkey-util.js';

export class HotkeyHelpController {
Expand Down Expand Up @@ -93,7 +93,7 @@ export class HotkeyHelpController {
*/
_getAllCommands() {
return new Promise((resolve, reject) => {
if (!(isObject(chrome.commands) && typeof chrome.commands.getAll === 'function')) {
if (!(isObjectNotArray(chrome.commands) && typeof chrome.commands.getAll === 'function')) {
resolve([]);
return;
}
Expand Down
16 changes: 8 additions & 8 deletions ext/js/pages/settings/backup-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import {Dexie} from '../../../lib/dexie.js';
import {parseJson} from '../../core/json.js';
import {log} from '../../core/log.js';
import {isObjectNotArray} from '../../core/object-utilities.js';
import {toError} from '../../core/to-error.js';
import {isObject} from '../../core/utilities.js';
import {OptionsUtil} from '../../data/options-util.js';
import {getAllPermissions} from '../../data/permissions-util.js';
import {arrayBufferUtf8Decode} from '../../data/sandbox/array-buffer-util.js';
Expand Down Expand Up @@ -354,7 +354,7 @@ export class BackupController {
const warnings = [];

const anki = options.anki;
if (isObject(anki)) {
if (isObjectNotArray(anki)) {
const fieldTemplates = anki.fieldTemplates;
if (typeof fieldTemplates === 'string') {
warnings.push('anki.fieldTemplates contains a non-default value');
Expand All @@ -372,12 +372,12 @@ export class BackupController {
}

const audio = options.audio;
if (isObject(audio)) {
if (isObjectNotArray(audio)) {
const sources = audio.sources;
if (Array.isArray(sources)) {
for (let i = 0, ii = sources.length; i < ii; ++i) {
const source = sources[i];
if (!isObject(source)) { continue; }
if (!isObjectNotArray(source)) { continue; }
const {url} = source;
if (typeof url === 'string' && url.length > 0 && !this._isLocalhostUrl(url)) {
warnings.push(`audio.sources[${i}].url uses a non-localhost URL`);
Expand All @@ -403,9 +403,9 @@ export class BackupController {
const profiles = optionsFull.profiles;
if (Array.isArray(profiles)) {
for (const profile of profiles) {
if (!isObject(profile)) { continue; }
if (!isObjectNotArray(profile)) { continue; }
const options = profile.options;
if (!isObject(options)) { continue; }
if (!isObjectNotArray(options)) { continue; }

const warnings2 = this._settingsImportSanitizeProfileOptions(options, dryRun);
for (const warning of warnings2) {
Expand All @@ -428,7 +428,7 @@ export class BackupController {
const data = parseJson(dataString);

// Type check
if (!isObject(data)) {
if (!isObjectNotArray(data)) {
throw new Error(`Invalid data type: ${typeof data}`);
}

Expand All @@ -451,7 +451,7 @@ export class BackupController {

// Verify options exists
let optionsFull = data.options;
if (!isObject(optionsFull)) {
if (!isObjectNotArray(optionsFull)) {
throw new Error(`Invalid options type: ${typeof optionsFull}`);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/

import {EventListenerCollection} from '../../core/event-listener-collection.js';
import {isObject} from '../../core/utilities.js';
import {isObjectNotArray} from '../../core/object-utilities.js';
import {querySelectorNotNull} from '../../dom/query-selector.js';
import {HotkeyUtil} from '../../input/hotkey-util.js';
import {KeyboardMouseInputField} from './keyboard-mouse-input-field.js';
Expand Down Expand Up @@ -149,7 +149,7 @@ export class ExtensionKeyboardShortcutController {
*/
_getCommands() {
return new Promise((resolve, reject) => {
if (!(isObject(chrome.commands) && typeof chrome.commands.getAll === 'function')) {
if (!(isObjectNotArray(chrome.commands) && typeof chrome.commands.getAll === 'function')) {
resolve([]);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions ext/js/pages/settings/persistent-storage-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import {isObject} from '../../core/utilities.js';
import {isObjectNotArray} from '../../core/object-utilities.js';
import {querySelectorNotNull} from '../../dom/query-selector.js';

export class PersistentStorageController {
Expand Down Expand Up @@ -98,7 +98,7 @@ export class PersistentStorageController {
* @returns {boolean}
*/
_isPersistentStorageSupported() {
return isObject(navigator.storage) && typeof navigator.storage.persist === 'function';
return isObjectNotArray(navigator.storage) && typeof navigator.storage.persist === 'function';
}

/**
Expand Down
5 changes: 3 additions & 2 deletions ext/js/pages/settings/settings-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

import {EventDispatcher} from '../../core/event-dispatcher.js';
import {EventListenerCollection} from '../../core/event-listener-collection.js';
import {generateId, isObject} from '../../core/utilities.js';
import {isObjectNotArray} from '../../core/object-utilities.js';
import {generateId} from '../../core/utilities.js';
import {OptionsUtil} from '../../data/options-util.js';
import {getAllPermissions} from '../../data/permissions-util.js';
import {HtmlTemplateCollection} from '../../dom/html-template-collection.js';
Expand Down Expand Up @@ -353,6 +354,6 @@ export class SettingsController extends EventDispatcher {
* @returns {boolean}
*/
_canObservePermissionsChanges() {
return isObject(chrome.permissions) && isObject(chrome.permissions.onAdded) && isObject(chrome.permissions.onRemoved);
return isObjectNotArray(chrome.permissions) && isObjectNotArray(chrome.permissions.onAdded) && isObjectNotArray(chrome.permissions.onRemoved);
}
}

0 comments on commit 8af46a9

Please sign in to comment.