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
@@ -3,6 +3,8 @@ import path from 'path';
import { describe, expect, test } from '@jest/globals';
import { DEFAULT_CONTENT } from '@/locales/calendar/chinese/index.ts';
import { itemAndIndex, entries } from '@/core/base.ts';
import type { ChineseCalendarLocaleData } from '@/core/calendar.ts';
import {
type ChineseYearMonthDayInfo,
@@ -90,12 +92,12 @@ describe('getChineseYearMonthAllDayInfos', () => {
allMonthChineseDays[`${currentYear}-${currentMonth}`] = currentMonthChineseDays;
allMonthSolarTermNames[`${currentYear}-${currentMonth}`] = currentMonthSolarTermNames;
for (const yearMonth in allMonthChineseDays) {
for (const [yearMonth, monthChineseDays] of entries(allMonthChineseDays)) {
test(`returns correct chinese all dates in month for ${yearMonth}`, () => {
const [yearStr, monthStr] = yearMonth.split('-');
const year = parseInt(yearStr as string);
const month = parseInt(monthStr as string);
const expectedChineseMonthOrDays = allMonthChineseDays[yearMonth] as string[];
const expectedChineseMonthOrDays = monthChineseDays;
const expectedSolarTermNames = allMonthSolarTermNames[yearMonth] as string[];
const actualChineseDates: ChineseYearMonthDayInfo[] | undefined = getChineseYearMonthAllDayInfos({
@@ -106,13 +108,12 @@ describe('getChineseYearMonthAllDayInfos', () => {
expect(actualChineseDates).toBeDefined();
if (actualChineseDates) {
for (let i = 0; i < actualChineseDates.length; i++) {
const actualChineseDate = actualChineseDates[i];
for (const [actualChineseDate, index] of itemAndIndex(actualChineseDates)) {
const chineseMonthOrDay: string | undefined = actualChineseDate?.day === 1 ? `${actualChineseDate?.month}${ordinalSuffix[actualChineseDate?.month - 1] ?? 'th'} Lunar Month`.toLowerCase() : actualChineseDate?.day.toString();
expect(actualChineseDate).toBeDefined();
expect(chineseMonthOrDay).toBe(expectedChineseMonthOrDays[i]);
expect(actualChineseDate?.solarTermName).toBe(expectedSolarTermNames[i]);
expect(chineseMonthOrDay).toBe(expectedChineseMonthOrDays[index]);
expect(actualChineseDate?.solarTermName).toBe(expectedSolarTermNames[index]);
}
}
});
+2 -2
View File
@@ -71,7 +71,7 @@ export function localizedPresetCategoriesToTransactionCategoryCreateWithSubCateg
return categories;
}
export function getSecondaryTransactionMapByName(allCategories: TransactionCategory[]): Record<string, TransactionCategory> {
export function getSecondaryTransactionMapByName(allCategories?: TransactionCategory[]): Record<string, TransactionCategory> {
const ret: Record<string, TransactionCategory> = {};
if (!allCategories) {
@@ -274,7 +274,7 @@ export function isSubCategoryIdAvailable(categories: TransactionCategory[], cate
return false;
}
export function getFirstAvailableCategoryId(categories: TransactionCategory[]): string {
export function getFirstAvailableCategoryId(categories?: TransactionCategory[]): string {
if (!categories || !categories.length) {
return '';
}
+11 -15
View File
@@ -431,8 +431,8 @@ export function countSplitItems(str: string | undefined | null, separator: strin
const items = str.split(separator);
let count = 0;
for (let i = 0; i < items.length; i++) {
if (items[i]) {
for (const item of items) {
if (item) {
count++;
}
}
@@ -483,9 +483,13 @@ export function selectInvert(filterItemIds: Record<string, boolean>, allItemsMap
}
export function isPrimaryItemHasSecondaryValue(primaryItem: Record<string, Record<string, unknown>[]>, primarySubItemsField: string, secondaryValueField: string | undefined, secondaryHiddenField: string | undefined, secondaryValue: unknown): boolean {
for (let i = 0; i < primaryItem[primarySubItemsField].length; i++) {
const secondaryItem = primaryItem[primarySubItemsField][i];
const secondaryItems = primaryItem[primarySubItemsField];
if (!secondaryItems || secondaryItems.length < 1) {
return false;
}
for (const secondaryItem of secondaryItems) {
if (secondaryHiddenField && secondaryItem[secondaryHiddenField]) {
continue;
}
@@ -500,14 +504,12 @@ export function isPrimaryItemHasSecondaryValue(primaryItem: Record<string, Recor
return false;
}
export function getPrimaryValueBySecondaryValue<T>(items: Record<string, Record<string, T>[]>[] | Record<string, Record<string, Record<string, T>[]>>, primarySubItemsField: string | undefined, primaryValueField: string | undefined, primaryHiddenField: string | undefined, secondaryValueField: string | undefined, secondaryHiddenField: string | undefined, secondaryValue: T): Record<string, T>[] | Record<string, Record<string, T>[]> | null {
export function getPrimaryValueBySecondaryValue<T>(items: Record<string, Record<string, T>[]>[] | Record<string, Record<string, Record<string, T>[]>>, primarySubItemsField: string | undefined, primaryValueField: string | undefined, primaryHiddenField: string | undefined, secondaryValueField: string | undefined, secondaryHiddenField: string | undefined, secondaryValue: T): Record<string, T>[] | Record<string, Record<string, T>[]> | null | undefined {
if (primarySubItemsField) {
if (isArray(items)) {
const arr = items as Record<string, Record<string, T>[]>[];
for (let i = 0; i < arr.length; i++) {
const primaryItem = arr[i];
for (const primaryItem of arr) {
if (primaryHiddenField && primaryItem[primaryHiddenField]) {
continue;
}
@@ -523,13 +525,7 @@ export function getPrimaryValueBySecondaryValue<T>(items: Record<string, Record<
} else {
const obj = items as Record<string, Record<string, Record<string, T>[]>>;
for (const field in obj) {
if (!Object.prototype.hasOwnProperty.call(obj, field)) {
continue;
}
const primaryItem = obj[field];
for (const primaryItem of values(obj)) {
if (primaryHiddenField && primaryItem[primaryHiddenField]) {
continue;
}
+1 -3
View File
@@ -224,9 +224,7 @@ export class LeafletMapInstance implements MapInstance {
private getFinalUrlFormat(urlFormat: string, urlExtraParams: LeafletTileSourceExtraParam[], options: MapInstanceInitOptions) {
const params: string[] = [];
for (let i = 0; i < urlExtraParams.length; i++) {
const param = urlExtraParams[i];
for (const param of urlExtraParams) {
if (param.paramValueType === 'tomtom_key') {
params.push(param.paramName + '=' + getTomTomMapAPIKey());
} else if (param.paramValueType === 'tianditu_key') {