refactor: Delete isPlainObject type guard and replace its usages with direct typeof checks.

This commit is contained in:
2025-11-24 21:22:38 +01:00
parent 63be4663ab
commit f5ca74b63c
8 changed files with 86 additions and 95 deletions
+1
View File
@@ -7,6 +7,7 @@
"dev": "webpack serve --mode development --open",
"build": "webpack --mode production",
"lint": "eslint src --ext .js,.jsx,.ts,.tsx",
"check": "tsc --noEmit && npm run lint",
"test:engine": "node --test tests/workspaceEngine.test.js"
},
"dependencies": {
@@ -12,7 +12,7 @@ import {
toIssuedTimestamp,
} from '../utils/date';
import { describeDocumentSummary, type DocumentSummaryRow } from './documentSummary';
import { isPlainObject } from '../utils/typeGuards';
import { useFolderManager } from '../folders/FolderManagerContext';
type Identifier = string | number;
@@ -128,11 +128,11 @@ const resolveOptionName = (source?: QuickAddOption | string | null): string => {
if (!source) {
return '';
}
if (isPlainObject(source)) {
if (typeof source === 'string') {
return source.trim();
}
const raw = source.name ?? source.label ?? '';
return `${raw}`.trim();
}
return `${source}`.trim();
};
const normalizeQuickAddOption = (option?: QuickAddOption | string | null): QuickAddEntry | null => {
@@ -140,17 +140,17 @@ const normalizeQuickAddOption = (option?: QuickAddOption | string | null): Quick
return null;
}
const label = (() => {
if (isPlainObject(option)) {
if (typeof option === 'string') {
return option.trim();
}
const sourceLabel = option.label ?? option.name ?? '';
return `${sourceLabel}`.trim();
}
return `${option}`.trim();
})();
if (!label) {
return null;
}
return {
id: isPlainObject(option) && option.id ? option.id : label,
id: typeof option !== 'string' && option.id ? option.id : label,
label,
original: option,
};
@@ -245,7 +245,7 @@ export const TagSection: React.FC<TagSectionProps> = ({
return;
}
if (item.state === 'all' && onRemove) {
const payload = isPlainObject(item.payload)
const payload = item.payload && typeof item.payload === 'object' && 'id' in item.payload
? (item.payload as TagEntry)
: tags.find((tag) => (tag.id ?? tag.label) === item.id) ?? { id: item.id, label: item.label };
onRemove(payload);
@@ -379,7 +379,7 @@ export const CorrespondentSection: React.FC<CorrespondentSectionProps> = ({
return;
}
if (item.state === 'all' && onRemove) {
const payload = isPlainObject(item.payload)
const payload = item.payload && typeof item.payload === 'object' && 'id' in item.payload
? (item.payload as CorrespondentEntry)
: entries.find((entry) => (entry.id ?? entry.name) === item.id) ?? { id: item.id, name: item.label };
onRemove(payload);
@@ -393,7 +393,7 @@ export const CorrespondentSection: React.FC<CorrespondentSectionProps> = ({
if (!resolvedName) {
return;
}
const payload = isPlainObject(source)
const payload = typeof source !== 'string'
? { ...source, name: resolvedName }
: { id: null, name: resolvedName };
onAdd({ name: resolvedName, option: payload, input: null });
@@ -1,5 +1,5 @@
import { useCallback, useMemo } from 'react';
import { isPlainObject } from '../../utils/typeGuards';
type ApiClient = {
post: (path: string, body?: unknown) => Promise<{ data: unknown }>;
@@ -124,16 +124,14 @@ const useDocumentCorrespondentActions = ({
if (!option) {
return null;
}
if (isPlainObject(option) && 'id' in option) {
return option as CorrespondentOption;
}
if (typeof option === 'string') {
const trimmed = option.trim();
if (trimmed) {
return { id: null, name: trimmed };
}
}
return null;
}
return option;
};
const handleCorrespondentAdd = useCallback(
@@ -1,5 +1,5 @@
import { useCallback } from 'react';
import { isPlainObject } from '../../utils/typeGuards';
import type { Dispatch, MutableRefObject, SetStateAction } from 'react';
import { DEFAULT_FOLDER_NAME, getRowId, isDocumentRowKey } from '../../app/appLayoutUtils';
import {
@@ -175,7 +175,7 @@ interface UseDocumentMutationsResult {
const normalizeDocumentId = (value: unknown): DocumentId | null => {
if (!value) return null;
if (isPlainObject(value) && 'id' in value && value.id != null) {
if (value && typeof value === 'object' && 'id' in value && value.id != null) {
return value.id as DocumentId;
}
return value as DocumentId;
@@ -446,7 +446,7 @@ const useDocumentMutations = ({
},
[
token,
documentLookup,
removeDocumentsFromCaches,
previewDocumentId,
closeDocumentPreview,
@@ -495,7 +495,8 @@ const useDocumentMutations = ({
);
const handleDocumentIssuedUpdate = useCallback(
async (documentId: DocumentId, nextIssuedDate: number | null) => {const payload = { issued_at: nextIssuedDate || null };
async (documentId: DocumentId, nextIssuedDate: number | null) => {
const payload = { issued_at: nextIssuedDate || null };
try {
const data = await updateDocument(documentId, payload);
const updatedDocument = extractDocumentFromResponse?.(data);
@@ -7,7 +7,7 @@ import {
} from 'react';
import type { JSX } from 'react';
import { CheckIcon, ChevronDownIcon } from '../../ui/icons';
import { isPlainObject } from '../../utils/typeGuards';
type CapabilityValue = string | number;
@@ -29,9 +29,7 @@ interface CapabilityDropdownProps {
summaryLabel?: string;
}
const isCapabilityOption = (
option: CapabilityDropdownOption | CapabilityValue | null,
): option is CapabilityDropdownOption => isPlainObject(option);
const resolveCapabilityValue = (
option: CapabilityDropdownOption | CapabilityValue | null,
@@ -39,8 +37,8 @@ const resolveCapabilityValue = (
if (option == null) {
return null;
}
if (!isCapabilityOption(option)) {
return option as CapabilityValue;
if (typeof option !== 'object') {
return option;
}
if (option.value != null) {
return option.value;
@@ -170,7 +168,7 @@ const CapabilityDropdown = ({
}
const label = formatLabel
? formatLabel(value)
: (isCapabilityOption(option) && option?.label) || String(value);
: (typeof option === 'object' && option?.label) || String(value);
const selected = selectedValues.includes(value);
return (
<button
@@ -8,7 +8,7 @@ import React, {
import type { SettingsSectionConfig } from '../SettingsModal';
import { IconX } from '../../ui/icons';
import CapabilityDropdown, { CapabilityDropdownOption } from '../components/CapabilityDropdown';
import { isPlainObject } from '../../utils/typeGuards';
type CapabilityValue = string | number;
type CapabilitySetId = string | number;
@@ -55,15 +55,14 @@ interface CapabilitySetsSectionProps {
type CapabilityOptionInput = CapabilityDropdownOption | CapabilityValue | null;
const isCapabilityOption = (option: CapabilityOptionInput): option is CapabilityDropdownOption =>
isPlainObject(option);
const resolveCapabilityValue = (option: CapabilityOptionInput): CapabilityValue | null => {
if (option == null) {
return null;
}
if (!isCapabilityOption(option)) {
return option as CapabilityValue;
if (typeof option === 'string' || typeof option === 'number') {
return option;
}
if (option.value != null) {
return option.value as CapabilityValue;
+2 -2
View File
@@ -1,7 +1,7 @@
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import type { CSSProperties, ReactNode, FormEvent, MutableRefObject } from 'react';
import { PlusIcon } from './icons';
import { isPlainObject } from '../utils/typeGuards';
import useFloatingMenu from './useFloatingMenu';
type QuickAddOption = string | number | { id?: string | number; label?: string; name?: string;[key: string]: unknown };
@@ -17,7 +17,7 @@ const normalizeOption = (option: QuickAddOption | null, index: number): Normaliz
if (option == null) {
return null;
}
if (isPlainObject(option)) {
if (typeof option === 'object') {
const label = option.label ?? option.name;
if (label == null) {
return null;
-6
View File
@@ -1,6 +0,0 @@
const objectToString = Object.prototype.toString;
export const isPlainObject = (value: unknown): value is Record<string, unknown> =>
value !== null && !Array.isArray(value) && Object(value) === value;