|
1 |
| -export declare namespace Quotes { |
2 |
| - interface Item { |
3 |
| - author: string |
4 |
| - content: string |
5 |
| - } |
6 |
| - |
7 |
| - type List = Item[] |
8 |
| - |
9 |
| - type Langs = 'en' | 'fr' | 'de' | 'it' | 'nl' | 'pl' | 'ru' | 'sv' |
10 |
| - |
11 |
| - type Classic = { type: 'classic'; lang: Langs } |
12 |
| - type Kaamelott = { type: 'kaamelott' } |
13 |
| - type Inspirobot = { type: 'inspirobot' } |
14 |
| - |
15 |
| - type Type = Classic | Kaamelott | Inspirobot |
16 |
| -} |
| 1 | +export type Langs = 'en' | 'fr' | 'de' | 'it' | 'nl' | 'pl' | 'ru' | 'sv' |
| 2 | +export type Quote = { author: string; content: string } |
17 | 3 |
|
18 |
| -interface Inspirobot { |
19 |
| - data: { |
20 |
| - text?: string |
21 |
| - type?: string |
22 |
| - }[] |
23 |
| -} |
24 |
| - |
25 |
| -const QUOTES_VERSION: string = '13122023' |
26 |
| -const CDN_BASE_URL: string = 'https://cdn.jsdelivr.net/gh/victrme/i18n-quotes@main/quotes/' |
27 |
| - |
28 |
| -// |
29 |
| -// |
30 |
| -// |
31 |
| - |
32 |
| -export async function classic(lang: string, amount?: number): Promise<Quotes.List> { |
33 |
| - return await jsonFileQuotes(validLang(lang), amount) |
34 |
| -} |
| 4 | +const QUOTES_VERSION = '15102024' |
| 5 | +const LANGS: Langs[] = ['en', 'fr', 'de', 'it', 'nl', 'pl', 'ru', 'sv'] |
35 | 6 |
|
36 |
| -export async function kaamelott(amount?: number): Promise<Quotes.List> { |
37 |
| - return await jsonFileQuotes('kaamelott', amount) |
38 |
| -} |
39 |
| - |
40 |
| -export async function inspirobot(): Promise<Quotes.List> { |
41 |
| - const promises = [] |
42 |
| - let list: Quotes.List = [] |
43 |
| - |
44 |
| - for (let i = 0; i < 6; i++) { |
45 |
| - promises.push(fetch('https://inspirobot.me/api?generateFlow=1')) |
46 |
| - } |
| 7 | +export async function classic(lang: string, amount?: number): Promise<Quote[
F438
span>]> { |
| 8 | + const base = 'https://cdn.jsdelivr.net/gh/victrme/i18n-quotes@main/quotes/' |
| 9 | + const filename = validLang(lang) + '.json?v=' |
| 10 | + const filepath = base + filename + QUOTES_VERSION |
| 11 | + const resp = await fetch(filepath) |
| 12 | + const json = await resp.json() |
47 | 13 |
|
48 |
| - for (const response of await Promise.all(promises)) { |
49 |
| - if (response.status === 200) { |
50 |
| - const json = await response.json() |
51 |
| - list = list.concat(sanitizeInspirobotData(json.data)) |
52 |
| - } |
53 |
| - } |
| 14 | + if (amount && amount > 0) return getRandomSample(json, amount) |
| 15 | + if (amount === 0) return [] |
54 | 16 |
|
55 |
| - return list |
| 17 | + return json |
56 | 18 | }
|
57 | 19 |
|
58 |
| -// |
59 |
| -// Helpers |
60 |
| -// |
61 |
| - |
62 |
| -async function jsonFileQuotes(filename: Quotes.Langs | 'kaamelott', amount?: number): Promise<Quotes.List> { |
63 |
| - const path = `${CDN_BASE_URL}${filename}.json?v=${QUOTES_VERSION}` |
64 |
| - const resp = await fetch(path) |
| 20 | +export async function kaamelott(amount?: number): Promise<Quote[]> { |
| 21 | + const base = 'https://cdn.jsdelivr.net/gh/victrme/i18n-quotes@main/quotes/' |
| 22 | + const filepath = base + 'kaamelott.json?v=' + QUOTES_VERSION |
| 23 | + const resp = await fetch(filepath) |
65 | 24 | const json = await resp.json()
|
66 | 25 |
|
67 |
| - return amount === undefined ? json : getRandomSample(json, amount) |
| 26 | + if (amount && amount > 0) return getRandomSample(json, amount) |
| 27 | + if (amount === 0) return [] |
| 28 | + |
| 29 | + return json |
68 | 30 | }
|
69 | 31 |
|
70 |
| -function sanitizeInspirobotData(data: Inspirobot['data']): Quotes.List { |
71 |
| - const result = [] |
| 32 | +export async function inspirobot(amount?: number): Promise<Quote[]> { |
| 33 | + const base = 'https://raw.githubusercontent.com/victrme/i18n-quotes/refs/heads/main/quotes/' |
| 34 | + const filepath = base + 'inspirobot.txt?v=' + QUOTES_VERSION |
| 35 | + const resp = await fetch(filepath) |
| 36 | + const text = await resp.text() |
72 | 37 |
|
73 |
| - for (const { type, text } of data) { |
74 |
| - const isQuote = type === 'quote' |
75 |
| - const isValid = text && !text.includes('[') && text.length < 100 |
| 38 | + const arr = text.split('\n') |
| 39 | + const json: Quote[] = arr.map((line) => ({ author: 'Inspirobot', content: line })) |
76 | 40 |
|
77 |
| - if (isQuote && isValid) { |
78 |
| - result.push({ author: 'Inspirobot', content: text }) |
79 |
| - } |
80 |
| - } |
| 41 | + if (amount && amount > 0) return getRandomSample(json, amount) |
| 42 | + if (amount === 0) return [] |
81 | 43 |
|
82 |
| - return result |
| 44 | + return json |
83 | 45 | }
|
84 | 46 |
|
85 |
| -function getRandomSample(list: Quotes.List, amount: number): Quotes.List { |
86 |
| - let result: typeof list = [] |
| 47 | +function getRandomSample(list: Quote[], amount: number): Quote[] { |
| 48 | + let result: Quote[] = [] |
| 49 | + let random = 0 |
87 | 50 |
|
88 | 51 | for (let i = 0; i < amount; i++) {
|
89 |
| - result.push(list[Math.floor(Math.random() * list.length)]) |
| 52 | + random = Math.floor(Math.random() * list.length) |
| 53 | + result.push(list[random]) |
90 | 54 | }
|
91 | 55 |
|
92 | 56 | return result
|
93 | 57 | }
|
94 | 58 |
|
95 |
| -function validLang(lang: string): Quotes.Langs { |
96 |
| - const langs: Quotes.Langs[] = ['en', 'fr', 'de', 'it', 'nl', 'pl', 'ru', 'sv'] |
97 |
| - const isLang = (l: string): l is Quotes.Langs => langs.includes(l as any) |
| 59 | +function validLang(lang: string): Langs { |
| 60 | + const isLang = (l: string): l is Langs => LANGS.includes(l as any) |
98 | 61 | return isLang(lang) ? lang : 'en'
|
99 | 62 | }
|
0 commit comments