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
+62
View File
@@ -3,6 +3,68 @@ export type PartialRecord<K extends keyof any, T> = {
[P in K]?: T;
}
export function* itemAndIndex<T>(arr: T[]): Iterable<[T, number]> {
for (let i = 0; i < arr.length; i++) {
yield [arr[i], i] as [T, number];
}
}
export function* reversed<T>(arr: T[]): Iterable<T> {
for (let i = arr.length - 1; i >= 0; i--) {
yield arr[i] as T;
}
}
export function* reversedItemAndIndex<T>(arr: T[]): Iterable<[T, number]> {
for (let i = arr.length - 1; i >= 0; i--) {
yield [arr[i], i] as [T, number];
}
}
export function* entries<K extends string | number | symbol, V>(obj: Record<K, V>): Iterable<[K, V]> {
for (const key in obj) {
if (!Object.prototype.hasOwnProperty.call(obj, key)) {
continue;
}
yield [key, obj[key]] as [K, V];
}
}
export function* keys<K extends string | number | symbol, V>(obj: Record<K, V>): Iterable<K> {
for (const key in obj) {
if (!Object.prototype.hasOwnProperty.call(obj, key)) {
continue;
}
yield key as K;
}
}
export function* keysIfValueEquals<K extends string | number | symbol, V>(obj: Record<K, V>, value: V): Iterable<K> {
for (const key in obj) {
if (!Object.prototype.hasOwnProperty.call(obj, key)) {
continue;
}
if (obj[key] !== value) {
continue;
}
yield key as K;
}
}
export function* values<K extends string | number | symbol, V>(obj: Record<K, V>): Iterable<V> {
for (const key in obj) {
if (!Object.prototype.hasOwnProperty.call(obj, key)) {
continue;
}
yield obj[key] as V;
}
}
export interface NameValue {
readonly name: string;
readonly value: string;
+3 -3
View File
@@ -69,8 +69,8 @@ export class FiscalYearStart {
return undefined;
}
const month = parseInt(parts[0], 10);
const day = parseInt(parts[1], 10);
const month = parseInt(parts[0] as string, 10);
const day = parseInt(parts[1] as string, 10);
return FiscalYearStart.of(month, day);
}
@@ -87,7 +87,7 @@ export class FiscalYearStart {
}
private static isValidFiscalYearMonthDay(month: number, day: number): boolean {
return 1 <= month && month <= 12 && 1 <= day && day <= FiscalYearStart.MONTH_MAX_DAYS[month - 1];
return 1 <= month && month <= 12 && 1 <= day && day <= (FiscalYearStart.MONTH_MAX_DAYS[month - 1] as number);
}
}
+7 -7
View File
@@ -96,7 +96,7 @@ export class NumeralSystem implements TypeAndName {
return '';
}
return this.allDigits[digit];
return this.allDigits[digit] as string;
}
public parseInt(value: string): number {
@@ -136,11 +136,11 @@ export class NumeralSystem implements TypeAndName {
let result = '';
for (let i = 0; i < value.length; i++) {
const ch = value[i];
const ch = value.charAt(i);
if (NumeralSystem.WesternArabicNumerals.isDigit(ch)) {
const digit = NumeralSystem.WesternArabicNumerals.digitsToWesternArabic[ch];
result += this.allDigits[digit];
const digit = NumeralSystem.WesternArabicNumerals.digitsToWesternArabic[ch] as number;
result += this.allDigits[digit] as string;
} else {
result += ch;
}
@@ -157,11 +157,11 @@ export class NumeralSystem implements TypeAndName {
let result = '';
for (let i = 0; i < value.length; i++) {
const ch = value[i];
const ch = value.charAt(i);
if (this.isDigit(ch)) {
const digit = this.digitsToWesternArabic[ch];
result += NumeralSystem.WesternArabicNumerals.allDigits[digit];
const digit = this.digitsToWesternArabic[ch] as number;
result += NumeralSystem.WesternArabicNumerals.allDigits[digit] as string;
} else {
result += ch;
}