node-check-word
node-check-word copied to clipboard
Improvement suggestion
Hi David, interesting library.
I was using a word generator with check-word. It ran a few thousand words and the NPM version couldn't handle it. I noticed that the NPM version isn't up-to-date, and the current one seems to perform much better.
I made some changes that run ~4x faster with the version on Github. It first loads the txt word list file from the old library (ie words/en.txt), then puts them into a map (faster to check if a key exists)
There is a bit of change, including the word list files, so I didn't make a PR. Though I'm happy to if you plan to use it
Here is the code in Typescript
import fs from "fs";
const possibleLanguages = ['de', 'en', 'es', 'fr'];
export default function checkword(language: string) {
language = language && language.toLowerCase() || 'en';
if (possibleLanguages.indexOf(language) == -1) {
throw new Error(language + " is not valid language");
}
const content = fs.readFileSync(__dirname + '/words/' + language + '.txt', { encoding: "utf8" });
const words: { [name: string]: boolean } = {};
content.split(/\n/).forEach(k => {
words[k] = true;
});
return {
check: function (word: string) {
if (word in words) {
return true
}
return false;
}
};
};
I realize this post is old but...
Couldn't you have just:
check: function (word: string) { return (word in words) }
Definitely. What was I thinking!