-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.js
124 lines (112 loc) · 4.51 KB
/
commands.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const rp = require("request-promise");
const apiHost = "https://fourtytwowords.herokuapp.com";
const capitalizeWord = word => {
const firstLetter = word.charAt(0);
return word.replace(firstLetter, firstLetter.toUpperCase());
};
const getAntonymOrSynonym = ({ data = [], required }) => {
// Returns Antonyms or Synonyms
return data[0] && data[0].relationshipType === required ? data[0].words : data[1] && data[1].words;
};
module.exports = ({ apiKey, playGame }) => {
const getDataFromApi = async route => {
// Gets the data from the API using the route provided.
const options = {
uri: `${apiHost}/${route}`,
qs: { api_key: apiKey },
json: true
};
// console.log("Options", options);
try {
return await rp(options);
} catch (error) {
// console.log("Error while getting the required data. Please try again", error);
}
};
const getDefinition = async (word, { dataRequired } = {}) => {
// Gets and prints/returns all the definitions of the given word. It returns the data only when required by the calling functions else prints it.
const definitions = await getDataFromApi(`word/${word}/definitions`);
if (dataRequired) {
return definitions ? definitions.map(element => element.text) : [];
}
if (!definitions) {
console.log("\nNo definition found");
return;
}
console.log(`\n${capitalizeWord(word)} can be defined in the following ways.`);
definitions.forEach(element => console.log(">>", element.text));
};
const getExamples = async (word, { dataRequired } = {}) => {
// Gets and prints/returns all the examples of the given word. It returns the data only when required by the calling functions else prints it.
const { examples } = (await getDataFromApi(`word/${word}/examples`)) || {};
if (dataRequired) {
return examples ? examples.map(element => element.text) : [];
}
if (!examples) {
console.log("\nNo examples found");
return;
}
console.log(`\nSome examples of ${capitalizeWord(word)} are.`);
examples.forEach(element => console.log(">>", element.text));
};
const getRelatedWords = async (word, { dataRequired, ant, syn } = {}) => {
// Gets the synonyms and antonyms the word. It returns the data only when required by the calling functions else prints it.
const relatedWords = await getDataFromApi(`word/${word}/relatedWords`);
const returnData = {};
if (ant) {
let words = getAntonymOrSynonym({ data: relatedWords, required: "antonym" });
if (words && words.length) {
if (dataRequired) {
returnData["ant"] = words;
} else {
console.log(`\nSome antonyms of ${capitalizeWord(word)} are: `);
words.forEach(element => console.log(">>", element));
}
} else {
!dataRequired && console.log(`\nNo antonyms found for: ${capitalizeWord(word)}.`);
}
}
if (syn) {
let words = getAntonymOrSynonym({ data: relatedWords, required: "synonym" });
if (words && words.length) {
if (dataRequired) {
returnData["syn"] = words;
} else {
console.log(`\nSome synonyms of ${capitalizeWord(word)} are: `);
words.forEach(element => console.log(">>", element));
}
} else {
!dataRequired && console.log(`\nNo synonyms found for: ${capitalizeWord(word)}.`);
}
}
return returnData;
};
const getAllData = async (word, { dataRequired } = {}) => {
// Fetches all required data.
const definitions = await getDefinition(word, { dataRequired });
const relatedWords = await getRelatedWords(word, { ant: true, syn: true, dataRequired });
const examples = await getExamples(word, { dataRequired });
return { definitions, ...relatedWords, examples };
};
const getRandomWord = async ({ dataRequired } = {}) => {
// Get a random word and all of its data.
const { word } = (await getDataFromApi("words/randomWord")) || {};
if (!word) {
!dataRequired && console.log("Unable to get random word");
return;
}
!dataRequired && console.log("\nWord of the day:", capitalizeWord(word));
const data = await getAllData(word, { dataRequired });
return { ...data, word };
};
const play = async () => {
// Play the game
let data = await getRandomWord({ dataRequired: true });
if (!data) {
console.log("Unable to start game.");
return;
}
return await playGame(data);
};
return { getRandomWord, getDefinition, getRelatedWords, getExamples, getAllData, play };
};