File tree 3 files changed +29
-14
lines changed
3 files changed +29
-14
lines changed Original file line number Diff line number Diff line change @@ -179,9 +179,11 @@ import { getTransliterateSuggestions } from "react-transliterate";
179
179
180
180
const data = await getTransliterateSuggestions (
181
181
word, // word to fetch suggestions for
182
- 5 , // number of suggestions to fetch
183
- false , // add the word as the last suggestion
184
- " hi" , // target language
182
+ {
183
+ numOptions: 5 , // number of suggestions to fetch
184
+ showCurrentWordAsLastSuggestion: true , // add the word as the last suggestion
185
+ lang: " hi" , // target language
186
+ },
185
187
);
186
188
```
187
189
Original file line number Diff line number Diff line change @@ -111,12 +111,11 @@ export const ReactTransliterate = ({
111
111
? maxOptions - 1
112
112
: maxOptions ;
113
113
114
- const data = await getTransliterateSuggestions (
115
- lastWord ,
114
+ const data = await getTransliterateSuggestions ( lastWord , {
116
115
numOptions,
117
116
showCurrentWordAsLastSuggestion,
118
117
lang,
119
- ) ;
118
+ } ) ;
120
119
setOptions ( data ) ;
121
120
} ;
122
121
Original file line number Diff line number Diff line change
1
+ import { Language } from "../types/Language" ;
2
+
3
+ type Config = {
4
+ numOptions ?: number ;
5
+ showCurrentWordAsLastSuggestion ?: boolean ;
6
+ lang ?: Language ;
7
+ } ;
8
+
1
9
export const getTransliterateSuggestions = async (
2
- lastWord : string ,
3
- numOptions = 5 ,
4
- showCurrentWordAsLastSuggestion = false ,
5
- lang = "hi" ,
6
- ) => {
10
+ word : string ,
11
+ config ?: Config ,
12
+ ) : Promise < string [ ] > => {
13
+ const { numOptions, showCurrentWordAsLastSuggestion, lang } = config || {
14
+ numOptions : 5 ,
15
+ showCurrentWordAsLastSuggestion : true ,
16
+ lang : "hi" ,
17
+ } ;
7
18
// fetch suggestion from api
8
- // const url = `https://www.google.com/inputtools/request?ime=transliteration_en_${lang}&num=5&cp=0&cs=0&ie=utf-8&oe=utf-8&app=jsapi&text=${lastWord }`;
19
+ // const url = `https://www.google.com/inputtools/request?ime=transliteration_en_${lang}&num=5&cp=0&cs=0&ie=utf-8&oe=utf-8&app=jsapi&text=${word }`;
9
20
10
- const url = `https://inputtools.google.com/request?text=${ lastWord } &itc=${ lang } -t-i0-und&num=${ numOptions } &cp=0&cs=1&ie=utf-8&oe=utf-8&app=demopage` ;
21
+ const url = `https://inputtools.google.com/request?text=${ word } &itc=${ lang } -t-i0-und&num=${ numOptions } &cp=0&cs=1&ie=utf-8&oe=utf-8&app=demopage` ;
11
22
try {
12
23
const res = await fetch ( url ) ;
13
24
const data = await res . json ( ) ;
14
25
if ( data && data [ 0 ] === "SUCCESS" ) {
15
26
const found = showCurrentWordAsLastSuggestion
16
- ? [ ...data [ 1 ] [ 0 ] [ 1 ] , lastWord ]
27
+ ? [ ...data [ 1 ] [ 0 ] [ 1 ] , word ]
17
28
: data [ 1 ] [ 0 ] [ 1 ] ;
18
29
return found ;
19
30
} else {
31
+ if ( showCurrentWordAsLastSuggestion ) {
32
+ return [ word ] ;
33
+ }
20
34
return [ ] ;
21
35
}
22
36
} catch ( e ) {
You can’t perform that action at this time.
0 commit comments