-
Notifications
You must be signed in to change notification settings - Fork 0
/
fmt.ts
77 lines (62 loc) · 1.94 KB
/
fmt.ts
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
import { colors } from "https://deno.land/x/[email protected]/ansi/colors.ts";
import { Word, Response, History } from "./type.ts";
function etymologySentence(obj: Word) {
let sentence = "";
if (!obj?.etymologies?.length) return sentence;
for (let i = 0; i < obj.etymologies.length; i++) {
const etymology = obj.etymologies[i];
const languageName = etymology.languages[0].name;
const romanizedText = etymology.romanizedText;
const definition = etymology.definition;
const relationText = etymology.relation.text;
const wordClassName = etymology.wordClass.name;
sentence += `${languageName} ${romanizedText}- “${definition}” ${wordClassName}${relationText} `;
if (i == 1) {
const affixName = etymology.affixes.suffix?.name;
if (affixName) sentence += `${languageName} ${affixName} ekiyle `;
}
}
return sentence;
}
interface Item {
description: string;
etymology: string;
histories: History[];
name: string;
}
export const format = (data: Response) => {
const items: Item[] = [];
data.words.forEach((word) => {
const item: Item = {
etymology: etymologySentence(word),
description: word.note,
histories: word.histories,
name: word.name,
};
items.push(item);
});
return items;
};
export const printAll = (items: Item[]) => {
items.forEach((item) => {
const result = `
${colors.bold.brightRed(item.name)}
${colors.green("Köken:")} ${item.etymology}
${colors.green("Ek Açıklama:")} ${item.description}
${colors.green("Tarihçe:")}
${item.histories
?.map(
(h) =>
`${colors.bold.white(h?.language?.name || "")} ${
h.excerpt
} [${colors.gray(h.source.book)}, ${colors.gray(
String(h.dateSortable)
)}] \n${h.quote}`
)
.join("\n")}
`;
console.log(
result.replaceAll("%u", "").replaceAll("%i", "").replaceAll("%b", "")
);
});
};