Skip to content

Commit

Permalink
use eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
foxriver76 committed Sep 28, 2023
1 parent 13db865 commit 40eed3c
Show file tree
Hide file tree
Showing 8 changed files with 990 additions and 839 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["@foxriver76/eslint-config"]
}
22 changes: 13 additions & 9 deletions lib/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ function isObject(it) {
* @returns {it is any[]}
*/
function isArray(it) {
if (typeof Array.isArray === 'function') return Array.isArray(it);
if (typeof Array.isArray === 'function') {
return Array.isArray(it);
}
return Object.prototype.toString.call(it) === '[object Array]';
}

Expand All @@ -30,7 +32,7 @@ function isArray(it) {
* @param {string} [yandexApiKey] The yandex API key. You can create one for free at https://translate.yandex.com/developers
* @returns {Promise<string>}
*/
async function translateText(text, targetLang, yandexApiKey) {
function translateText(text, targetLang, yandexApiKey) {
if (targetLang === 'en') {
return text;
} else if (!text) {
Expand All @@ -55,8 +57,10 @@ async function translateYandex(text, targetLang, apiKey) {
targetLang = 'zh';
}
try {
const url = `https://translate.yandex.net/api/v1.5/tr.json/translate?key=${apiKey}&text=${encodeURIComponent(text)}&lang=en-${targetLang}`;
const response = await axios({url, timeout: 15000});
const url = `https://translate.yandex.net/api/v1.5/tr.json/translate?key=${apiKey}&text=${encodeURIComponent(
text
)}&lang=en-${targetLang}`;
const response = await axios({ url, timeout: 15000 });
if (response.data && response.data.text && isArray(response.data.text)) {
return response.data.text[0];
}
Expand All @@ -74,18 +78,18 @@ async function translateYandex(text, targetLang, apiKey) {
*/
async function translateGoogle(text, targetLang) {
try {
const url = `http://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=${targetLang}&dt=t&q=${encodeURIComponent(text)}&ie=UTF-8&oe=UTF-8`;
const response = await axios({url, timeout: 15000});
const url = `http://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=${targetLang}&dt=t&q=${encodeURIComponent(
text
)}&ie=UTF-8&oe=UTF-8`;
const response = await axios({ url, timeout: 15000 });
if (isArray(response.data)) {
// we got a valid response
return response.data[0][0][0];
}
throw new Error('Invalid response for translate request');
} catch (e) {
if (e.response && e.response.status === 429) {
throw new Error(
`Could not translate to "${targetLang}": Rate-limited by Google Translate`
);
throw new Error(`Could not translate to "${targetLang}": Rate-limited by Google Translate`);
} else {
throw new Error(`Could not translate to "${targetLang}": ${e}`);
}
Expand Down
Loading

0 comments on commit 40eed3c

Please sign in to comment.