Longtime lurker here. I'm making a pun generator in JavaScript, and I was looking for some feedback/guidance. I don't have much experience building applications with Node, and so a lot of my code feels a bit like rambling. This class is meant to be used like so:
let p = new Phrase("The dog likes cheese.")
p.generatePun().then((response) => {
console.log(response)
})
Specifically, I think the web api call could be structured better, but I don't quite know how. I'd appreciate any feedback!
Phrase.js
const util = require("./util.js");
const wordsApi = require("./rhymeApi.js");
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
class Phrase {
constructor(string, badwords = ["the", "on"]) {
this.tokens = string.split(" ");
this.badwords = badwords;
}
generatePun() {
let nonBadWordsIndices = [];
for (let i = 0; i < this.tokens.length; i++) {
if (!this.badwords.includes(this.tokens[i].toLowerCase())) {
nonBadWordsIndices.push(i)
}
}
let replaceIndex = util.getRandomElement(nonBadWordsIndices);
let wordToBePunned = this.tokens[replaceIndex];
const isCapitalized = wordToBePunned[0] === wordToBePunned[0].toUpperCase();
const isAllCaps = wordToBePunned === wordToBePunned.toUpperCase();
const punctuation= wordToBePunned[wordToBePunned.length - 1];
const isPunctuated = !letters.includes(punctuation);
if (isPunctuated) {
wordToBePunned = wordToBePunned.slice(0, wordToBePunned.length - 1)
}
return wordsApi.getRhyme(wordToBePunned).then((rhymes) => {
let rhyme = util.getRandomElement(rhymes).word;
if (isCapitalized) {
rhyme = rhyme.replace(/\b\w/g, l => l.toUpperCase())
}
if (isAllCaps) {
rhyme = rhyme.toUpperCase();
}
if (isPunctuated) {
rhyme = rhyme + punctuation
}
let punnedPhrase = this.tokens.slice();
punnedPhrase.splice(replaceIndex, 1, rhyme);
return punnedPhrase.join(" ")
}).catch((err) => {
return "Error in generating pun: " + err;
});
}
toString() {
return this.tokens.join(" ");
}
}
module.exports = Phrase;
util.js
let getRandomElement = (array) => {
return array[Math.floor(Math.random()*array.length)];
}
//returns random int in [0, stop)
let getRandomIntInRange = (stop) => {
return Math.floor(Math.random()*stop)
}
module.exports = {
getRandomElement,
getRandomIntInRange
}
rhymeApi.js
const rp = require('request-promise');
const options = {
method: 'GET',
uri: 'https://api.datamuse.com',
json: true
}
let getRhyme = (word) => {
let rhymeOptions = Object.assign({}, options);
rhymeOptions.uri += '/words?rel_rhy=' + word;
return rp(rhymeOptions)
}
module.exports = {
getRhyme
}
leteverywhere instead ofconst\$\endgroup\$