-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
158 lines (151 loc) · 4.47 KB
/
index.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const fields = [
"word",
"partOfSpeech",
"pronunciation1",
"audio1",
"pronunciation2",
"audio2",
"pronunciation3",
"audio3",
"definition1",
"example1",
"definition2",
"example2",
"definition3",
"example3",
"definition4",
"example4",
"definition5",
"example5",
"definition6",
"example6",
"definition7",
"example7",
"definition8",
"example8",
"definition9",
"example9",
"definition10",
"example10",
"synonyms",
"antonyms",
] as const;
const headers = {
separator: ";",
html: true,
columns: fields.join(";"),
};
Promise.allSettled(
process.argv.splice(2).map((arg) => {
return fetch(`https://api.dictionaryapi.dev/api/v2/entries/en/${arg}`)
.then((res): Promise<string[]> => {
if (!res.ok) throw arg;
return (res.json() as Promise<DictionaryResponse>).then(makeNotes);
})
.catch(() => {
throw arg;
});
}),
).then((fetches) => {
const [succededWords, failedWords] = fetches.reduce<
[succededWords: string[], failedWords: string[]]
>(
([success, fail], promise) => {
if (promise.status === "rejected") {
fail.push(promise.reason);
} else {
success.push(...promise.value);
}
return [success, fail];
},
[[], []],
);
if (succededWords.length) {
console.log(
Object.entries(headers)
.map(([k, v]) => `# ${k}:${v}`)
.join("\n"),
);
console.log(succededWords.join("\n"));
} else {
console.error("Could not fetch anything!");
}
if (failedWords.length) {
console.error(`Failed to fetch: ${failedWords.join(" ")}`);
}
});
function makeNotes(entries: DictionaryResponse): string[] {
return entries.flatMap((dict) => {
// remove non-us audio and phonetics with no audio (they're usually non-us)
dict.phonetics = dict.phonetics?.filter((p) => p.audio?.includes("-us."));
return dict.meanings.map((meaning) => {
// this is to make it typesafe with the headers.
const thisFields: Record<(typeof fields)[number], string | undefined> = {
[fields[0]]: dict.word,
[fields[1]]: meaning.partOfSpeech,
[fields[2]]: dict.phonetics?.[0]?.text || dict.phonetic,
[fields[3]]: dict.phonetics?.[0]?.audio,
[fields[4]]: dict.phonetics?.[1]?.text,
[fields[5]]: dict.phonetics?.[1]?.audio,
[fields[6]]: dict.phonetics?.[2]?.text,
[fields[7]]: dict.phonetics?.[2]?.audio,
[fields[8]]: meaning.definitions[0]?.definition,
[fields[9]]: meaning.definitions[0]?.example,
[fields[10]]: meaning.definitions[1]?.definition,
[fields[11]]: meaning.definitions[1]?.example,
[fields[12]]: meaning.definitions[2]?.definition,
[fields[13]]: meaning.definitions[2]?.example,
[fields[14]]: meaning.definitions[3]?.definition,
[fields[15]]: meaning.definitions[3]?.example,
[fields[16]]: meaning.definitions[4]?.definition,
[fields[17]]: meaning.definitions[4]?.example,
[fields[18]]: meaning.definitions[5]?.definition,
[fields[19]]: meaning.definitions[5]?.example,
[fields[20]]: meaning.definitions[6]?.definition,
[fields[21]]: meaning.definitions[6]?.example,
[fields[22]]: meaning.definitions[7]?.definition,
[fields[23]]: meaning.definitions[7]?.example,
[fields[24]]: meaning.definitions[8]?.definition,
[fields[25]]: meaning.definitions[8]?.example,
[fields[26]]: meaning.definitions[9]?.definition,
[fields[27]]: meaning.definitions[9]?.example,
[fields[28]]: meaning.synonyms.join(", "),
[fields[29]]: meaning.antonyms.join(", "),
};
return Object.values(thisFields).map(escapeField).join(headers.separator);
});
});
}
function escapeField(field: string | undefined): `"${string}"` {
// replace " with "" to escape the double quotes. Then wrap whole thing in double quotes
return `"${(field || "").replaceAll('"', '""')}"`;
}
type DictionaryResponse = {
word: string;
phonetic?: string;
phonetics?: Array<{
audio?: string;
sourceUrl?: string;
license?: {
name: string;
url: string;
};
text?: string;
}>;
meanings: Array<{
partOfSpeech: string;
definitions: Array<{
definition: string;
synonyms: Array<string>;
antonyms: Array<string>;
example?: string;
}>;
synonyms: Array<string>;
antonyms: Array<string>;
}>;
license: {
name: string;
url: string;
};
sourceUrls: Array<string>;
}[];