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

This commit is contained in:
MaysWind
2025-09-14 01:40:53 +08:00
parent 67bc81d3e2
commit 4700446ca0
38 changed files with 389 additions and 597 deletions
+37 -67
View File
@@ -1,3 +1,4 @@
import { itemAndIndex, keys, keysIfValueEquals, values } from '@/core/base.ts';
import { AccountType, AccountCategory } from '@/core/account.ts';
import { PARENT_ACCOUNT_CURRENCY_PLACEHOLDER } from '@/consts/currency.ts';
import { type AccountBalance, type CategorizedAccount, type AccountCategoriesWithVisibleCount, Account } from '@/models/account.ts';
@@ -5,9 +6,7 @@ import { type AccountBalance, type CategorizedAccount, type AccountCategoriesWit
export function getCategorizedAccountsMap(allAccounts: Account[]): Record<number, CategorizedAccount> {
const ret: Record<number, CategorizedAccount> = {};
for (let i = 0; i < allAccounts.length; i++) {
const account = allAccounts[i];
for (const account of allAccounts) {
if (!ret[account.category]) {
const categoryInfo = AccountCategory.valueOf(account.category);
@@ -21,8 +20,10 @@ export function getCategorizedAccountsMap(allAccounts: Account[]): Record<number
}
}
if (ret[account.category]) {
const accountList = ret[account.category].accounts;
const categorizedAccount = ret[account.category];
if (categorizedAccount) {
const accountList = categorizedAccount.accounts;
accountList.push(account);
}
}
@@ -35,15 +36,16 @@ export function getCategorizedAccounts(allAccounts: Account[]): CategorizedAccou
const allCategories = AccountCategory.values();
const categorizedAccounts = getCategorizedAccountsMap(allAccounts);
for (let i = 0; i < allCategories.length; i++) {
const category = allCategories[i];
for (const category of allCategories) {
if (!categorizedAccounts[category.type]) {
continue;
}
const accountCategory = categorizedAccounts[category.type];
ret.push(accountCategory);
if (accountCategory) {
ret.push(accountCategory);
}
}
return ret;
@@ -56,14 +58,11 @@ export function getAccountMapByName(allAccounts: Account[]): Record<string, Acco
return ret;
}
for (let i = 0; i < allAccounts.length; i++) {
const account = allAccounts[i];
for (const account of allAccounts) {
if (account.type === AccountType.SingleAccount.type) {
ret[account.name] = account;
} else if (account.type === AccountType.MultiSubAccounts.type && account.subAccounts) {
for (let j = 0; j < account.subAccounts.length; j++) {
const subAccount = account.subAccounts[j];
for (const subAccount of account.subAccounts) {
ret[subAccount.name] = subAccount;
}
}
@@ -76,28 +75,26 @@ export function getCategorizedAccountsWithVisibleCount(categorizedAccountsMap: R
const ret: AccountCategoriesWithVisibleCount[] = [];
const allCategories = AccountCategory.values();
for (let i = 0; i < allCategories.length; i++) {
const accountCategory = allCategories[i];
for (const accountCategory of allCategories) {
const categorizedAccount = categorizedAccountsMap[accountCategory.type];
if (!categorizedAccountsMap[accountCategory.type] || !categorizedAccountsMap[accountCategory.type].accounts) {
if (!categorizedAccount || !categorizedAccount.accounts) {
continue;
}
const allAccounts = categorizedAccountsMap[accountCategory.type].accounts;
const allAccounts = categorizedAccount.accounts;
const allSubAccounts: Record<string, Account[]> = {};
const allVisibleSubAccountCounts: Record<string, number> = {};
const allFirstVisibleSubAccountIndexes: Record<string, number> = {};
let allVisibleAccountCount = 0;
let firstVisibleAccountIndex = -1;
for (let j = 0; j < allAccounts.length; j++) {
const account = allAccounts[j];
for (const [account, accountIndex] of itemAndIndex(allAccounts)) {
if (!account.hidden) {
allVisibleAccountCount++;
if (firstVisibleAccountIndex === -1) {
firstVisibleAccountIndex = j;
firstVisibleAccountIndex = accountIndex;
}
}
@@ -105,14 +102,12 @@ export function getCategorizedAccountsWithVisibleCount(categorizedAccountsMap: R
let visibleSubAccountCount = 0;
let firstVisibleSubAccountIndex = -1;
for (let k = 0; k < account.subAccounts.length; k++) {
const subAccount = account.subAccounts[k];
for (const [subAccount, subAccountIndex] of itemAndIndex(account.subAccounts)) {
if (!subAccount.hidden) {
visibleSubAccountCount++;
if (firstVisibleSubAccountIndex === -1) {
firstVisibleSubAccountIndex = k;
firstVisibleSubAccountIndex = subAccountIndex;
}
}
}
@@ -147,16 +142,14 @@ export function getAllFilteredAccountsBalance(categorizedAccounts: Record<number
const allAccountCategories = AccountCategory.values();
const ret: AccountBalance[] = [];
for (let categoryIdx = 0; categoryIdx < allAccountCategories.length; categoryIdx++) {
const accountCategory = allAccountCategories[categoryIdx];
for (const accountCategory of allAccountCategories) {
const categorizedAccount = categorizedAccounts[accountCategory.type];
if (!categorizedAccounts[accountCategory.type] || !categorizedAccounts[accountCategory.type].accounts) {
if (!categorizedAccount || !categorizedAccount.accounts) {
continue;
}
for (let accountIdx = 0; accountIdx < categorizedAccounts[accountCategory.type].accounts.length; accountIdx++) {
const account = categorizedAccounts[accountCategory.type].accounts[accountIdx];
for (const account of categorizedAccount.accounts) {
if (account.hidden || !accountFilter(account)) {
continue;
}
@@ -169,9 +162,7 @@ export function getAllFilteredAccountsBalance(categorizedAccounts: Record<number
currency: account.currency
});
} else if (account.type === AccountType.MultiSubAccounts.type && account.subAccounts) {
for (let subAccountIdx = 0; subAccountIdx < account.subAccounts.length; subAccountIdx++) {
const subAccount = account.subAccounts[subAccountIdx];
for (const subAccount of account.subAccounts) {
if (subAccount.hidden || !accountFilter(subAccount)) {
continue;
}
@@ -197,13 +188,7 @@ export function getFinalAccountIdsByFilteredAccountIds(allAccountsMap: Record<st
return finalAccountIds;
}
for (const accountId in allAccountsMap) {
if (!Object.prototype.hasOwnProperty.call(allAccountsMap, accountId)) {
continue;
}
const account = allAccountsMap[accountId];
for (const account of values(allAccountsMap)) {
if (filteredAccountIds && !isAccountOrSubAccountsAllChecked(account, filteredAccountIds)) {
continue;
}
@@ -225,13 +210,13 @@ export function getUnifiedSelectedAccountsCurrencyOrDefaultCurrency(allAccountsM
let accountCurrency = '';
for (const accountId in selectedAccountIds) {
if (!Object.prototype.hasOwnProperty.call(selectedAccountIds, accountId)) {
for (const accountId of keysIfValueEquals(selectedAccountIds, true)) {
const account = allAccountsMap[accountId];
if (!account) {
continue;
}
const account = allAccountsMap[accountId];
if (account.currency === PARENT_ACCOUNT_CURRENCY_PLACEHOLDER) {
continue;
}
@@ -258,19 +243,14 @@ export function selectAccountOrSubAccounts(filterAccountIds: Record<string, bool
return;
}
for (let i = 0; i < account.subAccounts.length; i++) {
const subAccount = account.subAccounts[i];
for (const subAccount of account.subAccounts) {
filterAccountIds[subAccount.id] = value;
}
}
}
export function selectAll(filterAccountIds: Record<string, boolean>, allAccountsMap: Record<string, Account>, skipHiddenAccount: boolean): void {
for (const accountId in filterAccountIds) {
if (!Object.prototype.hasOwnProperty.call(filterAccountIds, accountId)) {
continue;
}
for (const accountId of keys(filterAccountIds)) {
const account = allAccountsMap[accountId];
if (skipHiddenAccount && account && account.hidden) {
@@ -284,11 +264,7 @@ export function selectAll(filterAccountIds: Record<string, boolean>, allAccounts
}
export function selectNone(filterAccountIds: Record<string, boolean>, allAccountsMap: Record<string, Account>, skipHiddenAccount: boolean): void {
for (const accountId in filterAccountIds) {
if (!Object.prototype.hasOwnProperty.call(filterAccountIds, accountId)) {
continue;
}
for (const accountId of keys(filterAccountIds)) {
const account = allAccountsMap[accountId];
if (skipHiddenAccount && account && account.hidden) {
@@ -302,11 +278,7 @@ export function selectNone(filterAccountIds: Record<string, boolean>, allAccount
}
export function selectInvert(filterAccountIds: Record<string, boolean>, allAccountsMap: Record<string, Account>, skipHiddenAccount: boolean): void {
for (const accountId in filterAccountIds) {
if (!Object.prototype.hasOwnProperty.call(filterAccountIds, accountId)) {
continue;
}
for (const accountId of keys(filterAccountIds)) {
const account = allAccountsMap[accountId];
if (skipHiddenAccount && account && account.hidden) {
@@ -324,8 +296,7 @@ export function isAccountOrSubAccountsAllChecked(account: Account, filterAccount
return !filterAccountIds[account.id];
}
for (let i = 0; i < account.subAccounts.length; i++) {
const subAccount = account.subAccounts[i];
for (const subAccount of account.subAccounts) {
if (filterAccountIds[subAccount.id]) {
return false;
}
@@ -341,8 +312,7 @@ export function isAccountOrSubAccountsHasButNotAllChecked(account: Account, filt
let checkedCount = 0;
for (let i = 0; i < account.subAccounts.length; i++) {
const subAccount = account.subAccounts[i];
for (const subAccount of account.subAccounts) {
if (!filterAccountIds[subAccount.id]) {
checkedCount++;
}