From be91004c3a1237153b958f48bd0e400f8c9df1d6 Mon Sep 17 00:00:00 2001 From: JOLIMAITRE Matthieu Date: Tue, 30 Apr 2024 17:14:23 +0200 Subject: [PATCH] add dict from imported json --- src/lib/dict.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/lib/dict.ts b/src/lib/dict.ts index 539e0c4..50603a6 100644 --- a/src/lib/dict.ts +++ b/src/lib/dict.ts @@ -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(); - 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 }`; } }