add dict from imported json

This commit is contained in:
JOLIMAITRE Matthieu 2024-04-30 17:14:23 +02:00
parent c5e439e567
commit be91004c3a

View file

@ -9,10 +9,9 @@ export class Dict {
this.letters = new Set([...words.values()].map((w) => w.split("")).flat());
}
static async from_file(path: string, length: number) {
static from_lines(lines: string[], length: number) {
const words = new Set<string>();
const content = await Deno.readTextFile(path);
for (const word of content.split("\n")) {
for (const word of lines) {
const word_ = word.trim().toLowerCase();
if (word_.length !== length) continue;
if (contains_any(word_, [" ", "-", "."])) continue;
@ -21,8 +20,13 @@ export class Dict {
return new Dict(words, length);
}
static async from_text_file(path: string, length: number) {
const content = await Deno.readTextFile(path);
return Dict.from_lines(content.split("\n"), length);
}
[Symbol.for("Deno.customInspect")]() {
return `Dict { ${this.words.size} words }`;
return `Dict { ${this.words.size} words, ${this.letters.size} letters }`;
}
}