scrabble-solver
Version:
Scrabble Solver 2 - Free, open-source, cross-platform, multi-language analysis tool for Scrabble, Scrabble Duel, Super Scrabble, Letter League, Crossplay, Literaki, and Kelimelik. Quickly find the top-scoring words using the given board and tiles.
25 lines (17 loc) • 682 B
text/typescript
import { Trie } from '@kamilmielnik/trie';
import { type Locale } from '@scrabble-solver/types';
import { getDictionary } from './dictionaries';
const cache: Partial<Record<Locale, { trie: Trie; dictionary: string } | undefined>> = {};
export const getTrie = async (locale: Locale): Promise<Trie | undefined> => {
const dictionary = await getDictionary(locale);
if (typeof dictionary === 'undefined') {
return undefined;
}
const cached = cache[locale];
if (typeof cached === 'undefined' || cached.dictionary !== dictionary) {
const trie = Trie.deserialize(dictionary);
cache[locale] = { dictionary, trie };
return trie;
}
return cached.trie;
};