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.
30 lines (23 loc) • 962 B
text/typescript
import { load } from 'cheerio';
import { request } from '../lib';
import type { ParsingResult } from '../types';
const DOES_NOT_EXIST_MESSAGE =
"The word you've entered isn't in the dictionary. Click on a spelling suggestion below or try again using the search bar above.";
export const crawl = (word: string): Promise<string> => {
return request({
protocol: 'https',
hostname: 'www.merriam-webster.com',
path: `/dictionary/${encodeURIComponent(word)}`,
});
};
export const parse = (html: string): ParsingResult => {
const $ = load(html);
$('strong.mw_t_bc').replaceWith(', ');
$('.text-lowercase').remove();
$('.sub-content-thread').remove();
const $definitions = $('[id^=dictionary-entry]').find('.dtText, .cxl-ref');
return {
definitions: Array.from($definitions).map((definition) => $(definition).text().replace(/\n/g, '')),
exists: $('.spelling-suggestion-text').text().trim() !== DOES_NOT_EXIST_MESSAGE,
};
};