code refactor

This commit is contained in:
MaysWind
2025-02-12 23:26:03 +08:00
parent 30575d15d0
commit 0b5721671d
6 changed files with 11 additions and 85 deletions
+1 -2
View File
@@ -13,7 +13,6 @@ import { ref, computed, useTemplateRef } from 'vue';
import { useI18n } from '@/locales/helpers.ts';
import { copyObjectTo } from '@/lib/common.ts';
import type { MapInstance, MapPosition } from '@/lib/map/base.ts';
import { createMapInstance } from '@/lib/map/index.ts';
@@ -38,7 +37,7 @@ const mapSupported = computed<boolean>(() => !!mapInstance.value);
const mapDependencyLoaded = computed<boolean>(() => mapInstance.value?.dependencyLoaded || false);
const finalMapStyle = computed<Record<string, unknown>>(() => {
const styles: Record<string, unknown> = copyObjectTo(props.mapStyle, {});
const styles: Record<string, unknown> = Object.assign({}, props.mapStyle);
if (props.height) {
styles['height'] = props.height;
@@ -93,7 +93,6 @@ import { useI18nUIComponents, showLoading, hideLoading } from '@/lib/ui/mobile.t
import { TransactionTag } from '@/models/transaction_tag.ts';
import { useTransactionTagsStore } from '@/stores/transactionTag.ts';
import { copyArrayTo } from '@/lib/common.ts';
import { type Framework7Dom, scrollToSelectedItem, scrollSheetToTop } from '@/lib/ui/mobile.ts';
const props = defineProps<{
@@ -117,7 +116,7 @@ const sheet = useTemplateRef<Sheet.Sheet>('sheet');
const searchbar = useTemplateRef<Searchbar.Searchbar>('searchbar');
const filterContent = ref<string>('');
const selectedItemIds = ref<string[]>(copyArrayTo(props.modelValue, []));
const selectedItemIds = ref<string[]>(Array.from(props.modelValue));
const newTag = ref<TransactionTag | null>(null);
const heightClass = ref<string>(getHeightClass());
@@ -227,7 +226,7 @@ function onSearchBarFocus(): void {
}
function onSheetOpen(event: { $el: Framework7Dom }): void {
selectedItemIds.value = copyArrayTo(props.modelValue, []);
selectedItemIds.value = Array.from(props.modelValue);
newTag.value = null;
scrollToSelectedItem(event.$el, '.page-content', 'li.list-item-selected');
}
-69
View File
@@ -402,75 +402,6 @@ export function getNameByKeyValue<V, N>(src: Record<string, N | V>[] | Record<st
return defaultName;
}
export function copyObjectTo(fromObject: Record<string, unknown> | undefined, toObject: Record<string, unknown> | undefined): Record<string, unknown> {
if (!isObject(fromObject)) {
return toObject as Record<string, unknown>;
}
if (!isObject(toObject)) {
toObject = {};
}
for (const key in fromObject) {
if (!Object.prototype.hasOwnProperty.call(fromObject, key)) {
continue;
}
const fromValue = fromObject[key];
const toValue = toObject[key];
if (isArray(fromValue)) {
toObject[key] = copyArrayTo(fromValue, toValue as unknown[]);
} else if (isObject(fromValue)) {
toObject[key] = copyObjectTo(fromValue as Record<string, unknown>, toValue as Record<string, unknown>);
} else {
if (fromValue !== toValue) {
toObject[key] = fromValue;
}
}
}
return toObject;
}
export function copyArrayTo<T>(fromArray: T[], toArray: T[]): T[] {
if (!isArray(fromArray)) {
return toArray;
}
if (!isArray(toArray)) {
toArray = [];
}
for (let i = 0; i < fromArray.length; i++) {
const fromValue = fromArray[i];
if (toArray.length > i) {
const toValue = toArray[i];
if (isArray(fromValue)) {
toArray[i] = copyArrayTo(fromValue as unknown[], toValue as unknown[]) as T;
} else if (isObject(fromValue)) {
toArray[i] = copyObjectTo(fromValue as Record<string, unknown>, toValue as Record<string, unknown>) as T;
} else {
if (fromValue !== toValue) {
toArray[i] = fromValue;
}
}
} else {
if (isArray(fromValue)) {
toArray.push(copyArrayTo(fromValue as unknown[], []) as T);
} else if (isObject(fromValue)) {
toArray.push(copyObjectTo(fromValue as Record<string, unknown>, {}) as T);
} else {
toArray.push(fromValue);
}
}
}
return toArray;
}
export function arrayContainsFieldValue<T>(array: Record<string, T>[], fieldName: string, value: T): boolean {
if (!value || !array || !array.length) {
return false;
+4 -5
View File
@@ -110,8 +110,7 @@ import {
isObject,
isString,
isNumber,
isBoolean,
copyArrayTo
isBoolean
} from '@/lib/common.ts';
import {
@@ -990,11 +989,11 @@ export function useI18n() {
let defaultCategories: PresetCategory[] = [];
if (categoryType === CategoryType.Income) {
defaultCategories = copyArrayTo(DEFAULT_INCOME_CATEGORIES, []);
defaultCategories = DEFAULT_INCOME_CATEGORIES;
} else if (categoryType === CategoryType.Expense) {
defaultCategories = copyArrayTo(DEFAULT_EXPENSE_CATEGORIES, []);
defaultCategories = DEFAULT_EXPENSE_CATEGORIES;
} else if (categoryType === CategoryType.Transfer) {
defaultCategories = copyArrayTo(DEFAULT_TRANSFER_CATEGORIES, []);
defaultCategories = DEFAULT_TRANSFER_CATEGORIES;
}
for (let j = 0; j < defaultCategories.length; j++) {
@@ -7,7 +7,6 @@ import { useStatisticsStore } from '@/stores/statistics.ts';
import type { Account, AccountCategoriesWithVisibleCount } from '@/models/account.ts';
import { copyObjectTo } from '@/lib/common.ts';
import {
getCategorizedAccountsWithVisibleCount,
selectAccountOrSubAccounts,
@@ -73,10 +72,10 @@ export function useAccountFilterSettingPageBase(type?: string) {
}
if (type === 'statisticsDefault') {
filterAccountIds.value = copyObjectTo(settingsStore.appSettings.statistics.defaultAccountFilter, allAccountIds) as Record<string, boolean>;
filterAccountIds.value = Object.assign(allAccountIds, settingsStore.appSettings.statistics.defaultAccountFilter);
return true;
} else if (type === 'statisticsCurrent') {
filterAccountIds.value = copyObjectTo(statisticsStore.transactionStatisticsFilter.filterAccountIds, allAccountIds) as Record<string, boolean>;
filterAccountIds.value = Object.assign(allAccountIds, statisticsStore.transactionStatisticsFilter.filterAccountIds);
return true;
} else if (type === 'transactionListCurrent') {
for (const accountId in transactionsStore.allFilterAccountIds) {
@@ -11,7 +11,6 @@ import { CategoryType } from '@/core/category.ts';
import type { TransactionCategory, TransactionCategoriesWithVisibleCount } from '@/models/transaction_category.ts';
import {
copyObjectTo,
arrayItemToObjectField
} from '@/lib/common.ts';
import {
@@ -96,10 +95,10 @@ export function useCategoryFilterSettingPageBase(type?: string, allowCategoryTyp
}
if (type === 'statisticsDefault') {
filterCategoryIds.value = copyObjectTo(settingsStore.appSettings.statistics.defaultTransactionCategoryFilter, allCategoryIds) as Record<string, boolean>;
filterCategoryIds.value = Object.assign(allCategoryIds, settingsStore.appSettings.statistics.defaultTransactionCategoryFilter);
return true;
} else if (type === 'statisticsCurrent') {
filterCategoryIds.value = copyObjectTo(statisticsStore.transactionStatisticsFilter.filterCategoryIds, allCategoryIds) as Record<string, boolean>;
filterCategoryIds.value = Object.assign(allCategoryIds, statisticsStore.transactionStatisticsFilter.filterCategoryIds);
return true;
} else if (type === 'transactionListCurrent') {
for (const categoryId in transactionsStore.allFilterCategoryIds) {