Skip to content

Commit 1aa9978

Browse files
committed
impr(polyglot): Remove slices.
Add IWordset. Replace Wordset on IWordset in PolyglotWordset. Change signature of PolyglotWordset constructor. (@IliyaZinoviev)
1 parent 30445a3 commit 1aa9978

4 files changed

Lines changed: 60 additions & 54 deletions

File tree

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

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { FunboxWordsFrequency, WordsetPick, Wordset } from "../wordset";
1+
import {
2+
FunboxWordsFrequency,
3+
WordsetPick,
4+
Wordset,
5+
IWordset,
6+
} from "../wordset";
27
import * as GetText from "../../utils/generate";
38
import { Config } from "../../config/store";
49
import { setConfig, toggleFunbox } from "../../config/setters";
@@ -30,9 +35,9 @@ import { Language, LanguageObject } from "@monkeytype/schemas/languages";
3035
import { qs } from "../../utils/dom";
3136

3237
export type FunboxFunctions = {
33-
getWord?: (wordset?: Wordset, wordIndex?: number) => string;
38+
getWord?: (wordset?: IWordset, wordIndex?: number) => string;
3439
punctuateWord?: (word: string) => string;
35-
withWords?: (words?: string[]) => Promise<Wordset | PolyglotWordset>;
40+
withWords?: (words?: string[]) => Promise<IWordset>;
3641
alterText?: (word: string, wordIndex: number, wordsBound: number) => string;
3742
applyConfig?: () => void;
3843
applyGlobalCSS?: () => void;
@@ -149,25 +154,36 @@ class PseudolangWordGenerator extends Wordset {
149154
}
150155
}
151156

152-
export class PolyglotWordset extends Wordset {
157+
export class PolyglotWordset implements IWordset {
153158
readonly wordsetMap: Map<Language, Wordset>;
154159
readonly languageProperties: Map<Language, JSONData.LanguageProperties>;
155160
readonly langs: Language[];
161+
length: number;
162+
163+
constructor(languages: LanguageObject[]) {
164+
this.languageProperties = new Map<Language, JSONData.LanguageProperties>();
165+
this.wordsetMap = new Map<Language, Wordset>();
166+
this.length = 0;
167+
168+
for (const lang of languages) {
169+
const count = lang.words.length;
170+
this.length += count;
171+
172+
this.languageProperties.set(lang.name, {
173+
noLazyMode: lang.noLazyMode,
174+
ligatures: lang.ligatures,
175+
rightToLeft: lang.rightToLeft,
176+
additionalAccents: lang.additionalAccents,
177+
});
156178

157-
constructor(
158-
words: string[],
159-
wordsetMap: Map<Language, Wordset>,
160-
languageProperties: Map<Language, JSONData.LanguageProperties>,
161-
) {
162-
super(words);
163-
this.languageProperties = languageProperties;
164-
this.langs = Array.from(languageProperties.keys());
165-
this.wordsetMap = wordsetMap;
179+
this.wordsetMap.set(lang.name, new Wordset(lang.words));
180+
}
181+
this.langs = Array.from(this.languageProperties.keys());
182+
this.length = length;
166183
this.resetIndexes();
167-
this.length = words.length;
168184
}
169185

170-
override resetIndexes(): void {
186+
resetIndexes(): void {
171187
this.wordsetMap.forEach((ws) => {
172188
ws.resetIndexes();
173189
});
@@ -183,17 +199,17 @@ export class PolyglotWordset extends Wordset {
183199
return { wordset: this.wordsetMap.get(language) as Wordset, language };
184200
}
185201

186-
override randomWord(mode: FunboxWordsFrequency): WordsetPick {
202+
randomWord(mode: FunboxWordsFrequency): WordsetPick {
187203
const { wordset, language } = this.getWordsetAndLang();
188204
return { word: wordset.randomWord(mode).word, language };
189205
}
190206

191-
override shuffledWord(): WordsetPick {
207+
shuffledWord(): WordsetPick {
192208
const { wordset, language } = this.getWordsetAndLang();
193209
return { word: wordset.shuffledWord().word, language };
194210
}
195211

196-
override nextWord(): WordsetPick {
212+
nextWord(): WordsetPick {
197213
const { wordset, language } = this.getWordsetAndLang();
198214
return { word: wordset.nextWord().word, language };
199215
}
@@ -580,9 +596,9 @@ const list: Partial<Record<FunboxName, FunboxFunctions>> = {
580596
},
581597
},
582598
weakspot: {
583-
getWord(wordset?: Wordset): string {
599+
getWord(wordset?: IWordset): string {
584600
if (wordset !== undefined) return WeakSpot.getWord(wordset);
585-
else return "";
601+
return "";
586602
},
587603
},
588604
pseudolang: {
@@ -772,30 +788,7 @@ const list: Partial<Record<FunboxName, FunboxFunctions>> = {
772788
throw new WordGenError("");
773789
}
774790

775-
// build languageProperties
776-
const languageProperties: Map<Language, JSONData.LanguageProperties> =
777-
new Map(
778-
languages.map((lang) => [
779-
lang.name,
780-
{
781-
noLazyMode: lang.noLazyMode,
782-
ligatures: lang.ligatures,
783-
rightToLeft: lang.rightToLeft,
784-
additionalAccents: lang.additionalAccents,
785-
},
786-
]),
787-
);
788-
// build wordsetMap and words
789-
const wordsetMap = new Map<Language, Wordset>();
790-
let end = 0;
791-
const words: string[] = [];
792-
for (const lang of languages) {
793-
const start = end;
794-
end += lang.words.length;
795-
words.push(...lang.words);
796-
wordsetMap.set(lang.name, new Wordset(words.slice(start, end)));
797-
}
798-
return new PolyglotWordset(words, wordsetMap, languageProperties);
791+
return new PolyglotWordset(languages);
799792
},
800793
},
801794
};

frontend/src/ts/test/weak-spot.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as TestInput from "./test-input";
2-
import { Wordset } from "./wordset";
2+
import { IWordset } from "./wordset";
33

44
// Changes how quickly it 'learns' scores - very roughly the score for a char
55
// is based on last perCharCount occurrences. Make it smaller to adjust faster.
@@ -59,7 +59,7 @@ function score(word: string): number {
5959
return numChars === 0 ? 0.0 : total / numChars;
6060
}
6161

62-
export function getWord(wordset: Wordset): string {
62+
export function getWord(wordset: IWordset): string {
6363
let highScore;
6464
let randomWord = "";
6565
for (let i = 0; i < wordSamples; i++) {

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Config } from "../config/store";
22
import { setConfig, setQuoteLengthAll, toggleFunbox } from "../config/setters";
33
import * as CustomText from "./custom-text";
4-
import { Wordset, FunboxWordsFrequency, withWords } from "./wordset";
4+
import { IWordset, Wordset, FunboxWordsFrequency, withWords } from "./wordset";
55
import QuotesController, {
66
Quote,
77
QuoteWithTextSplit,
@@ -348,7 +348,7 @@ async function getFunboxSection(): Promise<string[]> {
348348
function getFunboxWord(
349349
word: string,
350350
wordIndex: number,
351-
wordset?: Wordset,
351+
wordset?: IWordset,
352352
): string {
353353
const funbox = findSingleActiveFunboxWithFunction("getWord");
354354

@@ -495,8 +495,10 @@ async function getQuoteWordList(
495495
wordOrder?: FunboxWordOrder,
496496
): Promise<string[]> {
497497
if (TestState.isRepeated) {
498-
if (currentWordset === null) {
499-
throw new WordGenError("Current wordset is null");
498+
if (currentWordset === null || !(currentWordset instanceof Wordset)) {
499+
throw new WordGenError(
500+
"Current wordset is null or not instance of Wordset",
501+
);
500502
}
501503

502504
TestWords.setCurrentQuote(previousRandomQuote);
@@ -587,7 +589,7 @@ async function getQuoteWordList(
587589
return TestWords.currentQuote.textSplit;
588590
}
589591

590-
let currentWordset: Wordset | null = null;
592+
let currentWordset: IWordset | null = null;
591593
let currentLanguage: LanguageObject | null = null;
592594
let isCurrentlyUsingFunboxSection = false;
593595

@@ -704,12 +706,15 @@ export async function generateWords(
704706

705707
ret.hasTab =
706708
ret.words.some((w) => w.includes("\t")) ||
707-
currentWordset.words.some((w) => w.includes("\t")) ||
709+
(currentWordset instanceof Wordset &&
710+
currentWordset.words.some((w) => w.includes("\t"))) ||
708711
(Config.mode === "quote" &&
709712
(quote as QuoteWithTextSplit).textSplit.some((w) => w.includes("\t")));
713+
710714
ret.hasNewline =
711715
ret.words.some((w) => w.includes("\n")) ||
712-
currentWordset.words.some((w) => w.includes("\n")) ||
716+
(currentWordset instanceof Wordset &&
717+
currentWordset.words.some((w) => w.includes("\n"))) ||
713718
(Config.mode === "quote" &&
714719
(quote as QuoteWithTextSplit).textSplit.some((w) => w.includes("\n")));
715720

frontend/src/ts/test/wordset.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ export type WordsetPick = { word: string; language?: Language };
88

99
let currentWordset: Wordset | null = null;
1010

11-
export class Wordset {
11+
export type IWordset = {
12+
length: number;
13+
resetIndexes(): void;
14+
randomWord(mode: FunboxWordsFrequency): WordsetPick;
15+
shuffledWord(): WordsetPick;
16+
nextWord(): WordsetPick;
17+
};
18+
19+
export class Wordset implements IWordset {
1220
words: string[];
1321
length: number;
1422
orderedIndex: number;

0 commit comments

Comments
 (0)