Skip to content

Commit f92036d

Browse files
committed
improve types for getTransliterateSuggestions
1 parent 7247c9e commit f92036d

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,11 @@ import { getTransliterateSuggestions } from "react-transliterate";
179179

180180
const data = await getTransliterateSuggestions(
181181
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+
},
185187
);
186188
```
187189

src/index.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,11 @@ export const ReactTransliterate = ({
111111
? maxOptions - 1
112112
: maxOptions;
113113

114-
const data = await getTransliterateSuggestions(
115-
lastWord,
114+
const data = await getTransliterateSuggestions(lastWord, {
116115
numOptions,
117116
showCurrentWordAsLastSuggestion,
118117
lang,
119-
);
118+
});
120119
setOptions(data);
121120
};
122121

src/util/suggestions-util.ts

+22-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
1+
import { Language } from "../types/Language";
2+
3+
type Config = {
4+
numOptions?: number;
5+
showCurrentWordAsLastSuggestion?: boolean;
6+
lang?: Language;
7+
};
8+
19
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+
};
718
// 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}`;
920

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`;
1122
try {
1223
const res = await fetch(url);
1324
const data = await res.json();
1425
if (data && data[0] === "SUCCESS") {
1526
const found = showCurrentWordAsLastSuggestion
16-
? [...data[1][0][1], lastWord]
27+
? [...data[1][0][1], word]
1728
: data[1][0][1];
1829
return found;
1930
} else {
31+
if (showCurrentWordAsLastSuggestion) {
32+
return [word];
33+
}
2034
return [];
2135
}
2236
} catch (e) {

0 commit comments

Comments
 (0)