Skip to content

Commit cbb6d75

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 cbb6d75

4 files changed

Lines changed: 80 additions & 59 deletions

File tree

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

Lines changed: 53 additions & 46 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,34 @@ 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[];
156-
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;
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+
this.langs = [];
168+
169+
for (const lang of languages) {
170+
const count = lang.words.length;
171+
this.length += count;
172+
this.langs.push(lang.name);
173+
this.languageProperties.set(lang.name, {
174+
noLazyMode: lang.noLazyMode,
175+
ligatures: lang.ligatures,
176+
rightToLeft: lang.rightToLeft,
177+
additionalAccents: lang.additionalAccents,
178+
});
179+
this.wordsetMap.set(lang.name, new Wordset(lang.words));
180+
}
166181
this.resetIndexes();
167-
this.length = words.length;
168182
}
169183

170-
override resetIndexes(): void {
184+
resetIndexes(): void {
171185
this.wordsetMap.forEach((ws) => {
172186
ws.resetIndexes();
173187
});
@@ -183,20 +197,36 @@ export class PolyglotWordset extends Wordset {
183197
return { wordset: this.wordsetMap.get(language) as Wordset, language };
184198
}
185199

186-
override randomWord(mode: FunboxWordsFrequency): WordsetPick {
200+
randomWord(mode: FunboxWordsFrequency): WordsetPick {
187201
const { wordset, language } = this.getWordsetAndLang();
188202
return { word: wordset.randomWord(mode).word, language };
189203
}
190204

191-
override shuffledWord(): WordsetPick {
205+
shuffledWord(): WordsetPick {
192206
const { wordset, language } = this.getWordsetAndLang();
193207
return { word: wordset.shuffledWord().word, language };
194208
}
195209

196-
override nextWord(): WordsetPick {
210+
nextWord(): WordsetPick {
197211
const { wordset, language } = this.getWordsetAndLang();
198212
return { word: wordset.nextWord().word, language };
199213
}
214+
215+
hasLigatures(): boolean {
216+
for (const props of this.languageProperties.values()) {
217+
if (props.ligatures) return true;
218+
}
219+
return false;
220+
}
221+
222+
hasChar(char: string): boolean {
223+
for (const wordset of this.wordsetMap.values()) {
224+
if (wordset.hasChar(char)) {
225+
return true;
226+
}
227+
}
228+
return false;
229+
}
200230
}
201231

202232
const list: Partial<Record<FunboxName, FunboxFunctions>> = {
@@ -580,9 +610,9 @@ const list: Partial<Record<FunboxName, FunboxFunctions>> = {
580610
},
581611
},
582612
weakspot: {
583-
getWord(wordset?: Wordset): string {
613+
getWord(wordset?: IWordset): string {
584614
if (wordset !== undefined) return WeakSpot.getWord(wordset);
585-
else return "";
615+
return "";
586616
},
587617
},
588618
pseudolang: {
@@ -713,7 +743,7 @@ const list: Partial<Record<FunboxName, FunboxFunctions>> = {
713743
},
714744
},
715745
polyglot: {
716-
async withWords(_words) {
746+
async withWords(_words): Promise<PolyglotWordset> {
717747
const promises = Config.customPolyglot.map(async (language) =>
718748
JSONData.getLanguage(language).catch(() => {
719749
showNoticeNotification(
@@ -772,30 +802,7 @@ const list: Partial<Record<FunboxName, FunboxFunctions>> = {
772802
throw new WordGenError("");
773803
}
774804

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);
805+
return new PolyglotWordset(languages);
799806
},
800807
},
801808
};

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: 11 additions & 10 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

@@ -655,9 +657,7 @@ export async function generateWords(
655657
// PolyglotWordset if polyglot otherwise Wordset
656658
if (currentWordset instanceof PolyglotWordset) {
657659
// set allLigatures if any language in languageProperties has ligatures true
658-
ret.allLigatures = Array.from(
659-
currentWordset.languageProperties.values(),
660-
).some((props) => !!props.ligatures);
660+
ret.allLigatures = currentWordset.hasLigatures();
661661
}
662662
} else {
663663
currentWordset = await withWords(wordList);
@@ -704,12 +704,13 @@ export async function generateWords(
704704

705705
ret.hasTab =
706706
ret.words.some((w) => w.includes("\t")) ||
707-
currentWordset.words.some((w) => w.includes("\t")) ||
707+
currentWordset.hasChar("\t") ||
708708
(Config.mode === "quote" &&
709709
(quote as QuoteWithTextSplit).textSplit.some((w) => w.includes("\t")));
710+
710711
ret.hasNewline =
711712
ret.words.some((w) => w.includes("\n")) ||
712-
currentWordset.words.some((w) => w.includes("\n")) ||
713+
currentWordset.hasChar("\n") ||
713714
(Config.mode === "quote" &&
714715
(quote as QuoteWithTextSplit).textSplit.some((w) => w.includes("\n")));
715716

frontend/src/ts/test/wordset.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,16 @@ 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+
hasChar(char: string): boolean;
18+
};
19+
20+
export class Wordset implements IWordset {
1221
words: string[];
1322
length: number;
1423
orderedIndex: number;
@@ -59,6 +68,10 @@ export class Wordset {
5968
}
6069
return { word: this.words[this.orderedIndex++] as string };
6170
}
71+
72+
hasChar(char: string): boolean {
73+
return this.words.some((w) => w.includes(char));
74+
}
6275
}
6376

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

0 commit comments

Comments
 (0)