use for-of statements to replace for and for-in

This commit is contained in:
MaysWind
2025-09-14 17:18:01 +08:00
parent 4700446ca0
commit 1a8ce7d58d
29 changed files with 455 additions and 579 deletions
+38 -46
View File
@@ -3,6 +3,7 @@ import { defineStore } from 'pinia';
import type { BeforeResolveFunction } from '@/core/base.ts';
import { itemAndIndex, values } from '@/core/base.ts';
import { CategoryType } from '@/core/category.ts';
import {
@@ -53,23 +54,15 @@ export const useTransactionCategoriesStore = defineStore('transactionCategories'
allTransactionCategories.value = allCategories;
allTransactionCategoriesMap.value = {};
for (const categoryType in allCategories) {
if (!Object.prototype.hasOwnProperty.call(allCategories, categoryType)) {
continue;
}
const categories = allCategories[categoryType];
for (let i = 0; i < categories.length; i++) {
const category = categories[i];
for (const categories of values(allCategories)) {
for (const category of categories) {
allTransactionCategoriesMap.value[category.id] = category;
if (!category.subCategories) {
continue;
}
for (let j = 0; j < category.subCategories.length; j++) {
const subCategory = category.subCategories[j];
for (const subCategory of category.subCategories) {
allTransactionCategoriesMap.value[subCategory.id] = subCategory;
}
}
@@ -82,7 +75,7 @@ export const useTransactionCategoriesStore = defineStore('transactionCategories'
if (!category.parentId || category.parentId === '0') {
categoryList = allTransactionCategories.value[category.type];
} else if (allTransactionCategoriesMap.value[category.parentId]) {
categoryList = allTransactionCategoriesMap.value[category.parentId].subCategories;
categoryList = allTransactionCategoriesMap.value[category.parentId]!.subCategories;
}
if (categoryList) {
@@ -92,33 +85,33 @@ export const useTransactionCategoriesStore = defineStore('transactionCategories'
allTransactionCategoriesMap.value[category.id] = category;
}
function updateCategoryInTransactionCategoryList(category: TransactionCategory, oldCategory: TransactionCategory): boolean {
if (oldCategory && category.parentId !== oldCategory.parentId) {
function updateCategoryInTransactionCategoryList(currentCategory: TransactionCategory, oldCategory?: TransactionCategory): boolean {
if (oldCategory && currentCategory.parentId !== oldCategory.parentId) {
return false;
}
let categoryList: TransactionCategory[] | undefined = undefined;
if (!category.parentId || category.parentId === '0') {
categoryList = allTransactionCategories.value[category.type];
} else if (allTransactionCategoriesMap.value[category.parentId]) {
categoryList = allTransactionCategoriesMap.value[category.parentId].subCategories;
if (!currentCategory.parentId || currentCategory.parentId === '0') {
categoryList = allTransactionCategories.value[currentCategory.type];
} else if (allTransactionCategoriesMap.value[currentCategory.parentId]) {
categoryList = allTransactionCategoriesMap.value[currentCategory.parentId]!.subCategories;
}
if (categoryList) {
for (let i = 0; i < categoryList.length; i++) {
if (categoryList[i].id === category.id) {
if (!category.parentId || category.parentId === '0') {
category.subCategories = categoryList[i].subCategories;
for (const [category, index] of itemAndIndex(categoryList)) {
if (category.id === currentCategory.id) {
if (!currentCategory.parentId || currentCategory.parentId === '0') {
currentCategory.subCategories = category.subCategories;
}
categoryList.splice(i, 1, category);
categoryList.splice(index, 1, currentCategory);
break;
}
}
}
allTransactionCategoriesMap.value[category.id] = category;
allTransactionCategoriesMap.value[currentCategory.id] = currentCategory;
return true;
}
@@ -128,44 +121,43 @@ export const useTransactionCategoriesStore = defineStore('transactionCategories'
if (!category.parentId || category.parentId === '0') {
categoryList = allTransactionCategories.value[category.type];
} else if (allTransactionCategoriesMap.value[category.parentId]) {
categoryList = allTransactionCategoriesMap.value[category.parentId].subCategories;
categoryList = allTransactionCategoriesMap.value[category.parentId]!.subCategories;
}
if (categoryList) {
categoryList.splice(to, 0, categoryList.splice(from, 1)[0]);
categoryList.splice(to, 0, categoryList.splice(from, 1)[0] as TransactionCategory);
}
}
function updateCategoryVisibilityInTransactionCategoryList({ category, hidden }: { category: TransactionCategory, hidden: boolean }): void {
if (allTransactionCategoriesMap.value[category.id]) {
allTransactionCategoriesMap.value[category.id].visible = !hidden;
allTransactionCategoriesMap.value[category.id]!.visible = !hidden;
}
}
function removeCategoryFromTransactionCategoryList(category: TransactionCategory): void {
function removeCategoryFromTransactionCategoryList(currentCategory: TransactionCategory): void {
let categoryList: TransactionCategory[] | undefined = undefined;
if (!category.parentId || category.parentId === '0') {
categoryList = allTransactionCategories.value[category.type];
} else if (allTransactionCategoriesMap.value[category.parentId]) {
categoryList = allTransactionCategoriesMap.value[category.parentId].subCategories;
if (!currentCategory.parentId || currentCategory.parentId === '0') {
categoryList = allTransactionCategories.value[currentCategory.type];
} else if (allTransactionCategoriesMap.value[currentCategory.parentId]) {
categoryList = allTransactionCategoriesMap.value[currentCategory.parentId]!.subCategories;
}
if (categoryList) {
for (let i = 0; i < categoryList.length; i++) {
if (categoryList[i].id === category.id) {
categoryList.splice(i, 1);
for (const [category, index] of itemAndIndex(categoryList)) {
if (category.id === currentCategory.id) {
categoryList.splice(index, 1);
break;
}
}
}
if (allTransactionCategoriesMap.value[category.id] && allTransactionCategoriesMap.value[category.id].subCategories) {
const subCategoryList = allTransactionCategoriesMap.value[category.id].subCategories;
if (allTransactionCategoriesMap.value[currentCategory.id] && allTransactionCategoriesMap.value[currentCategory.id]!.subCategories) {
const subCategoryList = allTransactionCategoriesMap.value[currentCategory.id]!.subCategories;
if (subCategoryList) {
for (let i = 0; i < subCategoryList.length; i++) {
const subCategory = subCategoryList[i];
for (const subCategory of subCategoryList) {
if (allTransactionCategoriesMap.value[subCategory.id]) {
delete allTransactionCategoriesMap.value[subCategory.id];
}
@@ -173,8 +165,8 @@ export const useTransactionCategoriesStore = defineStore('transactionCategories'
}
}
if (allTransactionCategoriesMap.value[category.id]) {
delete allTransactionCategoriesMap.value[category.id];
if (allTransactionCategoriesMap.value[currentCategory.id]) {
delete allTransactionCategoriesMap.value[currentCategory.id];
}
}
@@ -403,12 +395,12 @@ export const useTransactionCategoriesStore = defineStore('transactionCategories'
if (!category.parentId || category.parentId === '0') {
if (!allTransactionCategories.value[category.type] ||
!allTransactionCategories.value[category.type][to]) {
!allTransactionCategories.value[category.type]![to]) {
reject({ message: 'Unable to move category' });
return;
}
} else {
const subCategoryList = allTransactionCategoriesMap.value[category.parentId].subCategories;
const subCategoryList = allTransactionCategoriesMap.value[category.parentId]?.subCategories;
if (!subCategoryList || !subCategoryList[to]) {
reject({ message: 'Unable to move category' });
@@ -438,10 +430,10 @@ export const useTransactionCategoriesStore = defineStore('transactionCategories'
}
if (categoryList) {
for (let i = 0; i < categoryList.length; i++) {
for (const [category, index] of itemAndIndex(categoryList)) {
newDisplayOrders.push({
id: categoryList[i].id,
displayOrder: i + 1
id: category.id,
displayOrder: index + 1
});
}
}