UNPKG

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.

39 lines (38 loc) 1.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parse = exports.crawl = void 0; const cheerio_1 = require("cheerio"); const lib_1 = require("../lib"); const crawl = (word) => { return (0, lib_1.request)({ protocol: 'https', hostname: 'en.wiktionary.org', path: `/api/rest_v1/page/definition/${encodeURIComponent(word)}`, headers: { 'User-Agent': 'scrabble-solver (https://github.com/kamilmielnik/scrabble-solver)', }, }); }; exports.crawl = crawl; const parse = (json) => { let data; try { data = JSON.parse(json); } catch { return { definitions: [], exists: false }; } const usages = data?.en ?? []; if (!Array.isArray(usages)) { return { definitions: [], exists: false }; } const definitions = usages .flatMap((usage) => (usage.definitions ?? []).map(({ definition }) => definition)) .map((definition) => (0, cheerio_1.load)(definition).text().trim()) .filter(Boolean); return { definitions, exists: definitions.length > 0, }; }; exports.parse = parse;