deno.land / x / at_test@v8.1.0 / src / externalInterface / grammar.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
type MakeUrl = (p: { txt: string; grammarAuthKey: string;}) => string;const makeUrl: MakeUrl = (p) => `https://api.textgears.com/grammar?key=${p.grammarAuthKey}&text=${ encodeURI(p.txt) }`;
type GrammarCheckResponse = null | { status: boolean; response: { result: boolean; errors: Array< { id: string; offset: number; length: number; description: { en: string; }; bad: string; better: Array<string>; type: "spelling"; } >; };};type GrammarCheck = (p: { txt: string; grammarAuthKey: string;}) => Promise< void | { description: { en: string; }; bad: string; better: string[]; }[]>;
/** * @see https://textgears.com/api#grammar */export const grammarCheck: GrammarCheck = (p) => fetch(makeUrl(p)) .then<GrammarCheckResponse>((v) => v.ok ? v.json() : null) .then((v) => { if (v === null) { return []; }
const allowTexts = p.txt.match(/`(.*?)`/g) || []; return v.response.errors .map((vv) => ({ description: { en: vv.description.en, }, bad: vv.bad, better: vv.better, })) .filter((o) => !allowTexts.some((txt) => txt === `\`${o.bad}\``)) || []; }) .catch(console.error);
at_test

Version Info

Tagged at
a year ago