-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
114 lines (92 loc) · 2.61 KB
/
index.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
#!/usr/bin/env node
import fetch from "node-fetch";
import chalk from "chalk";
import inquirer from "inquirer";
import { createSpinner } from "nanospinner";
import yargs from "yargs";
const y = yargs(process.argv.slice(2)).argv;
const API_URL = "https://tdk-db.herokuapp.com/tdk?word=";
/**
* It takes an input from the user, then it searches the input in the TDK dictionary and returns the
* result
* @returns The result of the getWord function.
*/
async function searchInTDK() {
console.log(`\n${chalk.bgBlue(" 📖 TDK Sözlük ")}\n`);
let input = y._[0];
while (input === undefined || input.trim() === "") {
const answers = await inquirer.prompt([
{
type: "input",
name: "input",
message: "Lütfen bir kelime giriniz:",
},
]);
input = answers.input;
}
const spinner = createSpinner(`"${input}" aranıyor...`).start();
getWord(input).then((res) => {
if (res.error) {
spinner.error({ text: res.error });
process.exit(1);
} else {
spinner.success({ text: "Arama tamamlandı.\n" });
renderResult(res.result.data);
}
});
}
/**
* It fetches a word from the API and returns the JSON data.
* @param word - The word you want to look up.
* @returns A promise.
*/
async function getWord(word) {
try {
const res = await fetch(API_URL + word);
const data = await res.json();
return data;
} catch (err) {
console.log(chalk.red(err.message));
process.exit(1);
}
}
/**
* It takes a list of words and prints out the meaning of each word
* @param data - The data that is returned from the API.
*/
function renderResult(data) {
for (const madde of data) {
let result = "";
result += `${chalk.bold.blue(madde.title)}`;
result += "\n\n";
if (madde.subtitle) {
result += `${chalk.hex("#C792EA")(madde.subtitle)}`;
result += "\n\n";
}
for (const meaning of madde.meanings) {
const replacedMeaning = meaning.meaning.replace(":", ".");
// id
result += `${chalk.bold(meaning.id + ".")} `;
// typeOrSuffix
if (meaning.typeOrSuffix) {
result += `${chalk.yellowBright(meaning.typeOrSuffix)} `;
}
// meaning
result += `${replacedMeaning}\n`;
// Example text
// if (meaning.example.text) {
// result += `\t${chalk.blueBright("Ör.")} ${chalk.green(meaning.example.text)}\n`;
// }
// Example author
// if (meaning.example.author) {
// result += `\t${chalk.yellow(meaning.example.author)}\n\n`;
// }
}
console.log(result);
}
}
function main() {
console.clear();
searchInTDK();
}
main();