import { getAllWords } from "https://esm.town/v/jdan/getAllWords";
export async function wordsMatching(input: string) {
const inputCount = input
.split("")
.reduce((acc, letter) => {
acc[letter] = (acc[letter] || 0) + 1;
return acc;
}, {} as Record<string, number>);
const allWords = await getAllWords();
return allWords.filter((word) => {
const wordCount = word
.split("")
.reduce((acc, letter) => {
acc[letter] = (acc[letter] || 0) + 1;
return acc;
}, {} as Record<string, number>);
return Object.keys(wordCount).every((letter) => {
return inputCount[letter] >= wordCount[letter];
});
}).sort((a, b) => {
if (a.length > b.length) {
return 1;
} else if (a.length < b.length) {
return -1;
} else {
return a < b ? -1 : 1;
}
});
}