Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate words starting with letter #42

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ declare type CountOptions = {

declare function count(options?: CountOptions): number;

export { generate, count, wordsList };
export { generate, count, wordsList };
31 changes: 25 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1965,11 +1965,13 @@ const longestWordSize = wordList.reduce((longestWord, currentWord) =>
).length;

export function generate(options) {
// initalize random number generator for words if options.seed is provided
// Initialize random number generator for words if options.seed is provided
const random = options?.seed ? new seedrandom(options.seed) : null;

const { minLength, maxLength, ...rest } = options || {};
// Destructure options
const { minLength, maxLength, prefix, ...rest } = options || {};

// Function to generate a random word
function word() {
let min =
typeof minLength !== "number"
Expand All @@ -1986,14 +1988,31 @@ export function generate(options) {
let rightSize = false;
let wordUsed;
while (!rightSize) {
wordUsed = generateRandomWord();
// Generate a random word with the provided prefix (if any)
wordUsed = generateRandomWord(prefix);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it was already possible in this loop, adding prefix increases the likelihood that we'll never find a word and get stuck in an infinite loop, which is problematic for sure. So please refactor generateRandomWord to also take min and max and include them in your filter criteria. That way we will know if it's impossible to satisfy the request due to any combination of min, max and prefix and can throw one clean exception. And this "rightSize" while loop can go away.

Probably generateRandomWord should just be merged with this function.

rightSize = wordUsed.length <= max && wordUsed.length >= min;
}
return wordUsed;
}

function generateRandomWord() {
return wordList[randInt(wordList.length)];
// Function to generate a random word with an optional prefix
function generateRandomWord(prefix = null) {
let result = null;

// Filter the wordList to find words starting with the specified prefix
if (prefix) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make sure it's a string:

if (typeof prefix === 'string')

const filteredList = wordList.filter((word) =>
word.startsWith(prefix)
);
if (filteredList.length > 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throw an exception (throw new Error('Unavailable')) if no words are available meeting the criteria.

const randomIndex = Math.floor(Math.random() * filteredList.length);
result = filteredList[randomIndex];
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

} else { (follow our style for if/else whitespace)

else {
result = wordList[randInt(wordList.length)];
}
return result;
}

// limits the size of words to the minimum and maximum possible
Expand Down Expand Up @@ -2081,4 +2100,4 @@ export function count(options) {
return wordList.filter(
(word) => word.length >= minLength && word.length <= maxLength
).length;
}
}