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

This commit is contained in:
MaysWind
2025-09-09 23:48:42 +08:00
parent c75a902d84
commit 34c5a1750e
50 changed files with 368 additions and 460 deletions
+9 -5
View File
@@ -40,11 +40,13 @@ export function useNumberInputBase(props: NumberInputProps, emit: NumberInputEmi
let finalValue = '';
for (let i = 0; i < value.length; i++) {
if (!NumeralSystem.WesternArabicNumerals.isDigit(value[i]) && !numeralSystem.value.isDigit(value[i]) && value[i] !== '-' && value[i] !== decimalSeparator) {
const ch = value.charAt(i);
if (!NumeralSystem.WesternArabicNumerals.isDigit(ch) && !numeralSystem.value.isDigit(ch) && ch !== '-' && ch !== decimalSeparator) {
break;
}
finalValue += value[i];
finalValue += ch;
}
finalValue = numeralSystem.value.replaceLocalizedDigitsToWesternArabicDigits(finalValue);
@@ -104,14 +106,16 @@ export function useNumberInputBase(props: NumberInputProps, emit: NumberInputEmi
const decimalSeparator = getCurrentDecimalSeparator();
if (newValue[0] === '-' || newValue[0] === decimalSeparator) {
actualNumeralSystem = NumeralSystem.detect(newValue[1]);
actualNumeralSystem = NumeralSystem.detect(newValue.charAt(1));
} else {
actualNumeralSystem = NumeralSystem.detect(newValue[0]);
actualNumeralSystem = NumeralSystem.detect(newValue.charAt(0));
}
if (actualNumeralSystem && (actualNumeralSystem.type === NumeralSystem.WesternArabicNumerals.type || actualNumeralSystem.type === numeralSystem.value.type)) {
for (let i = 0; i < newValue.length; i++) {
if (!NumeralSystem.WesternArabicNumerals.isDigit(newValue[i]) && !numeralSystem.value.isDigit(newValue[i]) && newValue[i] !== '-' && newValue[i] !== decimalSeparator) {
const ch = newValue.charAt(i);
if (!NumeralSystem.WesternArabicNumerals.isDigit(ch) && !numeralSystem.value.isDigit(ch) && ch !== '-' && ch !== decimalSeparator) {
break;
}
+2 -4
View File
@@ -44,8 +44,7 @@ export function usePieChartBase(props: CommonPieChartProps) {
const validItems = computed<CommonPieChartDataItem[]>(() => {
let totalValidValue = 0;
for (let i = 0; i < props.items.length; i++) {
const item = props.items[i];
for (const item of props.items) {
const value = item[props.valueField];
if (isNumber(value) && value > 0 && (!props.hiddenField || !item[props.hiddenField])) {
@@ -55,8 +54,7 @@ export function usePieChartBase(props: CommonPieChartProps) {
const validItems: CommonPieChartDataItem[] = [];
for (let i = 0; i < props.items.length; i++) {
const item = props.items[i];
for (const item of props.items) {
const value = item[props.valueField];
const percent = props.percentField ? item[props.percentField] : -1;
@@ -44,9 +44,9 @@ export function useScheduleFrequencySelectionBase() {
const values = value.split(',');
const ret: number[] = [];
for (let i = 0; i < values.length; i++) {
if (values[i]) {
ret.push(parseInt(values[i]));
for (const value of values) {
if (value) {
ret.push(parseInt(value));
}
}
+30 -22
View File
@@ -1,15 +1,15 @@
<template>
<div class="pin-codes-input" :style="`grid-template-columns: repeat(${length}, minmax(0, 1fr))`">
<div class="pin-code-input pin-code-input-outline"
:class="{ 'pin-code-input-focued': codes[index].focused }" :key="index"
:class="{ 'pin-code-input-focued': codes[index]!.focused }" :key="index"
v-for="(_, index) in codes">
<input ref="pin-code-input" min="0" maxlength="1" pattern="[0-9]*"
:value="codes[index].value"
:type="codes[index].inputType"
:value="codes[index]!.value"
:type="codes[index]!.inputType"
:disabled="disabled ? true : undefined"
:autofocus="autofocus && index === 0 ? true : undefined"
@focus="codes[index].focused = true"
@blur="codes[index].focused = false"
@focus="codes[index]!.focused = true"
@blur="codes[index]!.focused = false"
@keydown="onKeydown(index, $event)"
@paste="onPaste(index, $event)"
@change="onInput(index, $event)"
@@ -57,9 +57,9 @@ const numeralSystem = computed<NumeralSystem>(() => getCurrentNumeralSystemType(
const finalPinCode = computed<string>(() => {
let ret = '';
for (let i = 0; i < codes.value.length; i++) {
if (codes.value[i].value) {
ret += codes.value[i].value;
for (const code of codes.value) {
if (code.value) {
ret += code.value;
} else {
break;
}
@@ -80,7 +80,7 @@ function init(length: number, value: string): void {
};
if (value && value[i]) {
code.value = value[i];
code.value = value.charAt(i);
if (props.secure) {
code.inputType = 'password';
@@ -95,12 +95,14 @@ function autoFillText(index: number, text: string): void {
let lastIndex = index;
for (let i = index, j = 0; i < codes.value.length && j < text.length; i++, j++) {
if (text[j] < '0' || text[j] > '9') {
codes.value[i].value = '';
const ch = text.charAt(j);
if (ch < '0' || ch > '9') {
codes.value[i]!.value = '';
break;
}
codes.value[i].value = text[j];
codes.value[i]!.value = ch;
setInputType(i);
lastIndex = i;
}
@@ -117,23 +119,29 @@ function setInputType(index: number): void {
return;
}
if (!codes.value[index].value) {
codes.value[index].inputType = 'tel';
const code = codes.value[index];
if (!code) {
return;
}
if (codes.value[index].inputTimer) {
if (!code.value) {
code.inputType = 'tel';
return;
}
codes.value[index].inputTimer = setTimeout(() => {
if (codes.value[index].value) {
codes.value[index].inputType = 'password';
if (code.inputTimer) {
return;
}
code.inputTimer = setTimeout(() => {
if (code.value) {
code.inputType = 'password';
} else {
codes.value[index].inputType = 'tel';
code.inputType = 'tel';
}
codes.value[index].inputTimer = null;
code.inputTimer = null;
}, 300);
}
@@ -205,7 +213,7 @@ function onKeydown(index: number, event: KeyboardEvent): void {
if (event.key === 'Backspace' || event.key === 'Delete' || event.key === 'Del') {
for (let i = index; i < codes.value.length; i++) {
codes.value[i].value = '';
codes.value[i]!.value = '';
setInputType(i);
}
@@ -227,7 +235,7 @@ function onKeydown(index: number, event: KeyboardEvent): void {
}
if (digit) {
codes.value[index].value = digit;
codes.value[index]!.value = digit;
setInputType(index);
setNextFocus(index);
@@ -91,7 +91,7 @@ const alternateDates = computed<Record<TextualYearMonthDay, string> | undefined>
return undefined;
}
const allDates: CalendarAlternateDate[] | undefined = getCalendarAlternateDates({ year: parseInt(yearMonthDay[0]), month1base: parseInt(yearMonthDay[1]) })
const allDates: CalendarAlternateDate[] | undefined = getCalendarAlternateDates({ year: parseInt(yearMonthDay[0] as string), month1base: parseInt(yearMonthDay[1] as string) })
if (!allDates) {
return undefined;
+6 -4
View File
@@ -338,18 +338,20 @@ watch(currentValue, (newValue) => {
const decimalSeparator = getCurrentDecimalSeparator();
if (newValue[0] === '-' || newValue[0] === decimalSeparator) {
actualNumeralSystem = NumeralSystem.detect(newValue[1]);
actualNumeralSystem = NumeralSystem.detect(newValue.charAt(1));
} else {
actualNumeralSystem = NumeralSystem.detect(newValue[0]);
actualNumeralSystem = NumeralSystem.detect(newValue.charAt(0));
}
if (actualNumeralSystem && (actualNumeralSystem.type === NumeralSystem.WesternArabicNumerals.type || actualNumeralSystem.type === numeralSystem.value.type)) {
for (let i = 0; i < newValue.length; i++) {
if (!NumeralSystem.WesternArabicNumerals.isDigit(newValue[i]) && !numeralSystem.value.isDigit(newValue[i]) && newValue[i] !== '-' && newValue[i] !== decimalSeparator) {
const ch = newValue.charAt(i);
if (!NumeralSystem.WesternArabicNumerals.isDigit(ch) && !numeralSystem.value.isDigit(ch) && ch !== '-' && ch !== decimalSeparator) {
break;
}
finalValue += newValue[i];
finalValue += ch;
}
finalValue = numeralSystem.value.replaceWesternArabicDigitsToLocalizedDigits(finalValue);
+2 -6
View File
@@ -52,7 +52,7 @@ import { ref, computed } from 'vue';
import { useI18n } from '@/locales/helpers.ts';
import type { NameNumeralValue } from '@/core/base.ts';
import { type NameNumeralValue, keys } from '@/core/base.ts';
import type { NumeralSystem } from '@/core/numeral.ts';
import type { ComponentDensity } from '@/lib/ui/desktop.ts';
@@ -94,11 +94,7 @@ const currentPage = computed<number>({
if (value && value >= 1 && value <= props.totalPageCount) {
emit('update:modelValue', value);
for (const key in showMenus.value) {
if (!Object.prototype.hasOwnProperty.call(showMenus.value, key)) {
continue;
}
for (const key of keys(showMenus.value)) {
showMenus.value[key] = false;
}
}
+9 -16
View File
@@ -13,6 +13,7 @@ import type { CallbackDataParams } from 'echarts/types/dist/shared';
import { useI18n } from '@/locales/helpers.ts';
import { type CommonPieChartDataItem, type CommonPieChartProps, usePieChartBase } from '@/components/base/PieChartBase.ts'
import { itemAndIndex } from '@/core/base.ts';
import type { ColorStyleValue } from '@/core/color.ts';
import { ThemeType } from '@/core/theme.ts';
@@ -41,8 +42,7 @@ const isDarkMode = computed<boolean>(() => theme.global.name.value === ThemeType
const itemsMap = computed<Record<string, Record<string, unknown>>>(() => {
const map: Record<string, Record<string, unknown>> = {};
for (let i = 0; i < props.items.length; i++) {
const item = props.items[i];
for (const item of props.items) {
let id = '';
if (props.idField && item[props.idField]) {
@@ -60,8 +60,7 @@ const itemsMap = computed<Record<string, Record<string, unknown>>>(() => {
const seriesData = computed<DesktopPieChartDataItem[]>(() => {
const ret: DesktopPieChartDataItem[] = [];
for (let i = 0; i < validItems.value.length; i++) {
const item = validItems.value[i];
for (const item of validItems.value) {
ret.push({
...item,
itemStyle: {
@@ -75,9 +74,7 @@ const seriesData = computed<DesktopPieChartDataItem[]>(() => {
});
const hasUnselectedItem = computed<boolean>(() => {
for (let i = 0; i < validItems.value.length; i++) {
const item = validItems.value[i];
for (const item of validItems.value) {
if (selectedLegends.value && !selectedLegends.value[item.id]) {
return true;
}
@@ -91,9 +88,7 @@ const firstItemAndHalfCurrentItemTotalPercent = computed<number>(() => {
let firstValue = null;
let firstToCurrentTotalValue = 0;
for (let i = 0; i < validItems.value.length; i++) {
const item = validItems.value[i];
for (const [item, index] of itemAndIndex(validItems.value)) {
if (selectedLegends.value && !selectedLegends.value[item.id]) {
continue;
}
@@ -103,9 +98,9 @@ const firstItemAndHalfCurrentItemTotalPercent = computed<number>(() => {
}
if (firstValue !== null) {
if (i < selectedIndex.value) {
if (index < selectedIndex.value) {
firstToCurrentTotalValue += item.value;
} else if (i === selectedIndex.value) {
} else if (index === selectedIndex.value) {
firstToCurrentTotalValue += item.value / 2;
}
}
@@ -241,11 +236,9 @@ function onLegendSelectChanged(e: { selected: Record<string, boolean> }): void {
if (!selectedItem || !selectedLegends.value[selectedItem.id]) {
let newSelectedIndex = 0;
for (let i = 0; i < validItems.value.length; i++) {
const item = validItems.value[i];
for (const [item, index] of itemAndIndex(validItems.value)) {
if (selectedLegends.value[item.id]) {
newSelectedIndex = i;
newSelectedIndex = index;
break;
}
}
@@ -138,9 +138,9 @@ function updateFrequencyValue(value: number, selected: boolean | null): void {
const currentFrequencyValues = frequencyValue.value;
const newFrequencyValues: number[] = [];
for (let i = 0; i < currentFrequencyValues.length; i++) {
if (currentFrequencyValues[i] !== value || selected) {
newFrequencyValues.push(currentFrequencyValues[i]);
for (const currentFrequencyValue of currentFrequencyValues) {
if (currentFrequencyValue !== value || selected) {
newFrequencyValues.push(currentFrequencyValue);
}
}
+4 -3
View File
@@ -48,6 +48,7 @@ import { computed } from 'vue';
import { useI18n } from '@/locales/helpers.ts';
import { itemAndIndex } from '@/core/base.ts';
import { NumeralSystem } from '@/core/numeral.ts';
export interface StepBarItem {
@@ -87,9 +88,9 @@ function isStepActive(step: StepBarItem): boolean {
}
function isStepCompleted(stepIndex: number): boolean {
for (let i = 0; i < props.steps.length; i++) {
if (props.steps[i].name === props.currentStep) {
return stepIndex < i;
for (const [step, index] of itemAndIndex(props.steps)) {
if (step.name === props.currentStep) {
return stepIndex < index;
}
}
+3 -3
View File
@@ -50,7 +50,7 @@
<span class="numpad-button-text numpad-button-text-normal">{{ decimalSeparator }}</span>
</f7-button>
<f7-button class="numpad-button numpad-button-num" v-if="!supportDecimalSeparator" @click="inputDoubleNum(0)">
<span class="numpad-button-text numpad-button-text-normal">{{ digits[0] + digits[0] }}</span>
<span class="numpad-button-text numpad-button-text-normal">{{ `${digits[0]}${digits[0]}` }}</span>
</f7-button>
<f7-button class="numpad-button numpad-button-num" @click="inputNum(0)">
<span class="numpad-button-text numpad-button-text-normal">{{ digits[0] }}</span>
@@ -114,11 +114,11 @@ const digits = computed<string[]>(() => getAllLocalizedDigits());
const decimalSeparator = computed<string>(() => getCurrentDecimalSeparator());
const supportDecimalSeparator = computed<boolean>(() => {
if (!props.currency || !ALL_CURRENCIES[props.currency] || !isNumber(ALL_CURRENCIES[props.currency].fraction)) {
if (!props.currency || !ALL_CURRENCIES[props.currency] || !isNumber(ALL_CURRENCIES[props.currency]!.fraction)) {
return true;
}
return (ALL_CURRENCIES[props.currency].fraction as number) > 0;
return (ALL_CURRENCIES[props.currency]!.fraction as number) > 0;
});
const currentDisplay = computed<string>(() => {
+5 -7
View File
@@ -105,8 +105,8 @@ const circumference: number = diameter * Math.PI;
const totalValidValue = computed<number>(() => {
let totalValidValue = 0;
for (let i = 0; i < validItems.value.length; i++) {
totalValidValue += validItems.value[i].value;
for (const item of validItems.value) {
totalValidValue += item.value;
}
return totalValidValue;
@@ -120,7 +120,7 @@ const itemCommonDashOffset = computed<number>(() => {
let offset = 0;
for (let i = 0; i < Math.min(selectedIndex.value + 1, validItems.value.length); i++) {
const item = validItems.value[i];
const item = validItems.value[i] as CommonPieChartDataItem;
if (item.actualPercent > 0) {
if (i === selectedIndex.value) {
@@ -154,9 +154,7 @@ function getItemStrokeDash(item: CommonPieChartDataItem): string {
function getItemDashOffset(item: CommonPieChartDataItem, items: CommonPieChartDataItem[], offset?: number): number {
let allPreviousPercent = 0;
for (let i = 0; i < items.length; i++) {
const curItem = items[i];
for (const curItem of items) {
if (curItem === item) {
break;
}
@@ -189,7 +187,7 @@ const selectedItem = computed<CommonPieChartDataItem | null>(() => {
index = 0;
}
return validItems.value[index];
return validItems.value[index] ?? null;
});
function switchSelectedIndex(index: number): void {