Skip to content

Commit 6d1d342

Browse files
committed
impr(polyglot): Add hasChar method to Wordset to escape direct refer to Wordset.words.
Small refactoring in Wordset classes. (@IliyaZinoviev)
1 parent 52be7ba commit 6d1d342

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

frontend/src/ts/test/funbox/funbox-functions.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export class PolyglotWordset extends Wordset {
161161
this.wordsMap = wordsMap;
162162
this.resetIndexes();
163163
this.length = Array.from(this.wordsMap.values()).reduce(
164-
(sum, ws) => sum + ws.words.length,
164+
(sum, ws) => sum + ws.length,
165165
0,
166166
);
167167
this.currLang = this.langs[0] as Language;
@@ -172,7 +172,7 @@ export class PolyglotWordset extends Wordset {
172172
}
173173

174174
override resetIndexes(): void {
175-
this.wordsMap.forEach((ws, _) => {
175+
this.wordsMap.forEach((ws) => {
176176
ws.resetIndexes();
177177
});
178178
}
@@ -199,6 +199,12 @@ export class PolyglotWordset extends Wordset {
199199
override nextWord(): string {
200200
return this.getWordset().nextWord();
201201
}
202+
203+
override hasChar(c: string): boolean {
204+
return Array.from(this.wordsMap.values()).some((ws) =>
205+
ws.words.includes(c),
206+
);
207+
}
202208
}
203209

204210
const list: Partial<Record<FunboxName, FunboxFunctions>> = {

frontend/src/ts/test/words-generator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -707,12 +707,12 @@ export async function generateWords(
707707

708708
ret.hasTab =
709709
ret.words.some((w) => w.includes("\t")) ||
710-
currentWordset.words.some((w) => w.includes("\t")) ||
710+
currentWordset.hasChar("\t") ||
711711
(Config.mode === "quote" &&
712712
(quote as QuoteWithTextSplit).textSplit.some((w) => w.includes("\t")));
713713
ret.hasNewline =
714714
ret.words.some((w) => w.includes("\n")) ||
715-
currentWordset.words.some((w) => w.includes("\n")) ||
715+
currentWordset.hasChar("\n") ||
716716
(Config.mode === "quote" &&
717717
(quote as QuoteWithTextSplit).textSplit.some((w) => w.includes("\n")));
718718

frontend/src/ts/test/wordset.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class Wordset {
2525

2626
randomWord(mode: FunboxWordsFrequency): string {
2727
if (mode === "zipf") {
28-
return this.words[zipfyRandomArrayIndex(this.words.length)] as string;
28+
return this.words[zipfyRandomArrayIndex(this.length)] as string;
2929
} else {
3030
return randomElementFromArray(this.words);
3131
}
@@ -52,6 +52,10 @@ export class Wordset {
5252
}
5353
return this.words[this.orderedIndex++] as string;
5454
}
55+
56+
hasChar(c: string): boolean {
57+
return this.words.some((w) => w.includes(c));
58+
}
5559
}
5660

5761
export async function withWords(words: string[]): Promise<Wordset> {

0 commit comments

Comments
 (0)