New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

linguister

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

linguister - npm Package Compare versions

Comparing version 0.3.1 to 0.3.2

15

lib/components/translate.d.ts

@@ -109,3 +109,3 @@ declare const languages: {

declare type langISO = keyof typeof languages;
declare type Translation = {
declare class Translation {
/** The translated text */

@@ -132,3 +132,5 @@ text: string;

raw: Array<any>;
};
constructor(gAPIResponse: any);
toString(): string;
}
/**

@@ -139,3 +141,3 @@ * Returns the ISO 639-1 code of the desiredLang – if it is supported by Google Translate

*/
export declare function LangFrom(query: string): string | undefined;
export declare function LangFrom(query: string): langISO | false;
/**

@@ -163,4 +165,11 @@ * Used to spawn api requests. Will handle a ratelimit

}): Promise<Translation>;
/**
* Translate any string with googles translate
* @param {String} query String to translate
* @param {String} [from='auto'] ISO 639-1 Language code to translate from
* @param {String} [to='en'] ISO 639-1 Language code to translate to
* @return {Promise<Translation>} The translation response
*/
private _translate;
}
export {};

72

lib/components/translate.js

@@ -126,2 +126,26 @@ "use strict";

};
class Translation {
constructor(gAPIResponse) {
this.text = gAPIResponse[0].reduce((str, arr) => (arr[0] ? str + arr[0] : str), '');
this.from = {
language: {
didYouMean: gAPIResponse[2] !== gAPIResponse[8][0][0],
iso: gAPIResponse[8][0][0]
},
text: {
autoCorrected: gAPIResponse[7] ? gAPIResponse[7][5] : false,
value: gAPIResponse[7]
? gAPIResponse[7][0]
.replace(/<b><i>/g, '[')
.replace(/<\/i><\/b>/g, ']')
: '',
didYouMean: gAPIResponse[7] ? !gAPIResponse[7][5] : false
}
};
this.raw = gAPIResponse;
}
toString() {
return this.text;
}
}
/**

@@ -173,3 +197,5 @@ * Practically never does anything. Gonna have to look into how the api actually works

function LangFrom(query) {
return (Object.keys(languages).find(key => languages[key].toLowerCase() === query.toLowerCase()) || (languages[query] ? query : undefined));
return (Object.keys(languages).find(key => languages[key].toLowerCase() === query.toLowerCase()) ||
(languages[query] ? query : false) ||
false);
}

@@ -195,14 +221,16 @@ exports.LangFrom = LangFrom;

translate(text, opts = {}) {
const options = {
from: LangFrom(opts.from || 'auto') || 'auto',
to: LangFrom(opts.to || 'en') || 'en'
};
const startat = (this.lastreq =
Date.now() > this.lastreq + this.ratelimit
? Date.now()
: this.lastreq + this.ratelimit);
return new Promise((resolve, reject) => {
setTimeout(() => resolve(this._translate(text, options.from, options.to)), startat - Date.now());
return new Promise(resolve => {
setTimeout(() => resolve(this._translate(text, LangFrom(opts.from || 'auto') || 'auto', LangFrom(opts.to || 'en') || 'en')), (this.lastreq =
Date.now() > this.lastreq + this.ratelimit
? Date.now()
: this.lastreq + this.ratelimit) - Date.now());
});
}
/**
* Translate any string with googles translate
* @param {String} query String to translate
* @param {String} [from='auto'] ISO 639-1 Language code to translate from
* @param {String} [to='en'] ISO 639-1 Language code to translate to
* @return {Promise<Translation>} The translation response
*/
_translate(query, from, to) {

@@ -230,5 +258,4 @@ return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {

res.on('end', () => {
let parsed;
try {
parsed = JSON.parse(body);
resolve(new Translation(JSON.parse(body)));
}

@@ -238,21 +265,2 @@ catch (error) {

}
resolve({
text: parsed[0].reduce((str, arr) => (arr[0] ? str + arr[0] : str), ''),
from: {
language: {
didYouMean: parsed[2] !== parsed[8][0][0],
iso: parsed[8][0][0]
},
text: {
autoCorrected: parsed[7] ? parsed[7][5] : false,
value: parsed[7]
? parsed[7][0]
.replace(/<b><i>/g, '[')
.replace(/<\/i><\/b>/g, ']')
: '',
didYouMean: parsed[7] ? !parsed[7][5] : false
}
},
raw: parsed
});
});

@@ -259,0 +267,0 @@ });

{
"name": "linguister",
"description": "Translate any text using the google translate api",
"version": "0.3.1",
"version": "0.3.2",
"main": "lib/index.js",

@@ -6,0 +6,0 @@ "scripts": {

@@ -79,9 +79,22 @@ # Linguister [![Build Status](https://travis-ci.org/TomSputz/linguister.svg?branch=master)](https://travis-ci.org/TomSputz/linguister) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo) [![Coverage Status](https://coveralls.io/repos/github/TomSputz/linguister/badge.svg?branch=master)](https://coveralls.io/github/TomSputz/linguister?branch=master) [![Known Vulnerabilities](https://snyk.io/test/github/TomSputz/linguister/badge.svg)](https://snyk.io/test/npm/google-translate-api)

- [Translation](#translation)
- [LangFrom](#langfrom)
- [Parameters](#parameters)
- [TranslateAPI](#translateapi)
- [Parameters](#parameters-1)
- [translate](#translate)
- [Parameters](#parameters-2)
### Translation
The class the API responds with
#### Properties
- `text` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** – The translated text
- `from` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Details about the original text
- `from.language.didYouMean` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** True if the API suggested a correction in the source language
- `from.language.didYouMean` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The ISO 639-I language code of the language that the API has recognized in the text
- `from.text.value` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The untranslated auto corrected text or suggested text
- `from.text.autoCorrected` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** True if the API has auto corrected the text
- `from.text.didYouMean` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** True if the API has suggested corrections to the text
- `raw` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)** The raw response from th egoogle translate server
### LangFrom

@@ -88,0 +101,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc