Skip to content

Commit

Permalink
Merge pull request #49 from ArkhamCookie/random
Browse files Browse the repository at this point in the history
Completed (?) `randomWord` function
  • Loading branch information
ArkhamCookie authored Oct 9, 2023
2 parents 4cafb56 + 068683f commit ee35c2e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
4 changes: 4 additions & 0 deletions docs/examples/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ console.log(await randomWord({}))

const response = await randomWord({})
console.log(response.word) // prints a random word

console.log(await randomWord({
hasDictionaryDef: true
}))
61 changes: 57 additions & 4 deletions lib/words/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,71 @@ import { Wordnik } from '../mod.js'
* ```
*/
export async function randomWord({
hasDictionaryDef
hasDictionaryDef,
includePartOfSpeech,
excludePartOfSpeech,
minCorpusCount,
maxCorpusCount,
minDictionaryCount,
maxDictionaryCount,
minLength,
maxLength
}) {
const wordnik = new Wordnik('words.json/randomWord?')
let endpoint =
wordnik.base + wordnik.version + wordnik.target

if (hasDictionaryDef) {
if (!typeof hasDictionaryDef === Boolean) {
endpoint = endpoint + 'hasDictionaryDef=' + hasDictionaryDef
} else {
if (!(typeof hasDictionaryDef === 'boolean')) {
throw new Error('"hasDictionaryDef" must be a boolean')
}
endpoint = endpoint + 'hasDictionaryDef=' + hasDictionaryDef
}

if (includePartOfSpeech) {
endpoint = endpoint + 'includePartOfSpeech=' + includePartOfSpeech
}
if (excludePartOfSpeech) {
endpoint = endpoint + 'excludePartOfSpeech=' + excludePartOfSpeech
}

if (minCorpusCount) {
if (!(typeof minCorpusCount === 'number')) {
throw new Error('"minCorpusCount" must be a integer')
}
endpoint = endpoint + 'minCorpusCount=' + minCorpusCount
}
if (maxCorpusCount) {
if (!(typeof maxCorpusCount === 'number')) {
throw new Error('"maxCorpusCount" must be a integer')
}
endpoint = endpoint + 'maxCorpusCount=' + maxCorpusCount
}

if (minDictionaryCount) {
if (!(typeof minDictionaryCount === 'number')) {
throw new Error('"minDictionaryCount" must be a integer')
}
endpoint = endpoint + 'minDictionaryCount=' + minDictionaryCount
}
if (maxDictionaryCount) {
if (!(typeof maxDictionaryCount === 'number')) {
throw new Error('"maxDictionaryCount" must be a integer')
}
endpoint = endpoint + 'maxDictionaryCount=' + maxDictionaryCount
}

if (minLength) {
if (!(typeof minLength === 'number')) {
throw new Error('"minLength" must be a integer')
}
endpoint = endpoint + 'minLength=' + minLength
}
if (maxLength) {
if (!(typeof maxLength === 'number')) {
throw new Error('"maxLength" must be a integer')
}
endpoint = endpoint + 'maxLength=' + maxLength
}

endpoint = endpoint + '&' + wordnik.key
Expand Down

0 comments on commit ee35c2e

Please sign in to comment.