Skip to content
This repository was archived by the owner on Oct 17, 2023. It is now read-only.

fix: Improve typescript types #124

Merged
merged 1 commit into from
Sep 21, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -61,7 +61,7 @@
"check": "gts check",
"clean": "gts clean",
"compile": "tsc -p .",
"fix": "gts fix",
"fix": "gts fix && eslint --fix 'samples/**/*.js' && npm run prettier",
"prepare": "npm run compile",
"pretest": "npm run compile",
"presystem-test": "npm run compile"
25 changes: 19 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -178,7 +178,7 @@ export interface TranslateConfig extends GoogleAuthOptions {
* region_tag:translate_quickstart
* Full quickstart example:
*/
export class Translate extends Service {
class Translate extends Service {
options: TranslateConfig;
key?: string;
constructor(options?: TranslateConfig) {
@@ -267,12 +267,15 @@ export class Translate extends Service {
* region_tag:translate_detect_language
* Here's a full example:
*/
detect(input: string): Promise<[DetectResult, r.Response]>;
detect(input: string[]): Promise<[DetectResult[], r.Response]>;
detect(input: string, callback: DetectCallback<DetectResult>): void;
detect(input: string[], callback: DetectCallback<DetectResult[]>): void;
detect(
input: string|string[],
callback: DetectCallback<DetectResult>|
DetectCallback<DetectResult[]>): void {
callback?: DetectCallback<DetectResult>|
DetectCallback<DetectResult[]>): void|Promise<[DetectResult, r.Response]>|
Promise<[DetectResult[], r.Response]> {
const inputIsArray = Array.isArray(input);
input = arrify(input);
this.request(
@@ -328,11 +331,13 @@ export class Translate extends Service {
* region_tag:translate_list_language_names
* Gets the language names in a language other than English:
*/
getLanguages(callback: GetLanguagesCallback): void;
getLanguages(target?: string): Promise<[LanguageResult[], r.Response]>;
getLanguages(target: string, callback: GetLanguagesCallback): void;
getLanguages(callback: GetLanguagesCallback): void;
getLanguages(
targetOrCallback?: string|GetLanguagesCallback,
callback?: GetLanguagesCallback) {
callback?: GetLanguagesCallback):
void|Promise<[LanguageResult[], r.Response]> {
let target: string;
if (is.fn(targetOrCallback)) {
callback = targetOrCallback as GetLanguagesCallback;
@@ -445,6 +450,12 @@ export class Translate extends Service {
* region_tag:translate_text_with_model
* Translation using the premium model:
*/
translate(input: string, options: TranslateRequest):
Promise<[string, r.Response]>;
translate(input: string[], options: TranslateRequest):
Promise<[string[], r.Response]>;
translate(input: string, to: string): Promise<[string, r.Response]>;
translate(input: string[], to: string): Promise<[string[], r.Response]>;
translate(
input: string, options: TranslateRequest,
callback: TranslateCallback<string>): void;
@@ -457,7 +468,8 @@ export class Translate extends Service {
void;
translate(
inputs: string|string[], optionsOrTo: string|TranslateRequest,
callback: TranslateCallback<string>|TranslateCallback<string[]>) {
callback?: TranslateCallback<string>|TranslateCallback<string[]>):
void|Promise<[string, r.Response]>|Promise<[string[], r.Response]> {
const inputIsArray = Array.isArray(inputs);
const input = arrify(inputs);
let options: TranslateRequest = {};
@@ -590,3 +602,4 @@ promisifyAll(Translate, {exclude: ['request']});
* region_tag:translate_quickstart
* Full quickstart example:
*/
export {Translate};
101 changes: 33 additions & 68 deletions system-test/translate.ts
Original file line number Diff line number Diff line change
@@ -36,15 +36,11 @@ describe('translate', () => {
},
];

it('should detect a langauge', (done) => {
it('should detect a langauge', async () => {
const input = INPUT.map(x => x.input);

translate.detect(input, (err, results) => {
assert.ifError(err);
assert.strictEqual(results![0].language, INPUT[0].expectedLanguage);
assert.strictEqual(results![1].language, INPUT[1].expectedLanguage);
done();
});
const [results] = await translate.detect(input);
assert.strictEqual(results[0].language, INPUT[0].expectedLanguage);
assert.strictEqual(results[1].language, INPUT[1].expectedLanguage);
});
});

@@ -67,88 +63,57 @@ describe('translate', () => {
return input.replace(/^\W|\W*$/g, '');
}

it('should translate input', (done) => {
it('should translate input', async () => {
const input = INPUT.map(x => x.input);

translate.translate(input, 'es', (err, results) => {
assert.ifError(err);
results = results!.map(removeSymbols);
assert.strictEqual(results![0], INPUT[0].expectedTranslation);
assert.strictEqual(results![1], INPUT[1].expectedTranslation);
done();
});
let [results] = await translate.translate(input, 'es');
results = results.map(removeSymbols);
assert.strictEqual(results[0], INPUT[0].expectedTranslation);
assert.strictEqual(results[1], INPUT[1].expectedTranslation);
});

it('should translate input with from and to options', (done) => {
it('should translate input with from and to options', async () => {
const input = INPUT.map(x => x.input);

const opts = {
from: 'en',
to: 'es',
};

translate.translate(input, opts, (err, results) => {
assert.ifError(err);
results = results!.map(removeSymbols);
assert.strictEqual(results![0], INPUT[0].expectedTranslation);
assert.strictEqual(results![1], INPUT[1].expectedTranslation);
done();
});
let [results] = await translate.translate(input, opts);
results = results.map(removeSymbols);
assert.strictEqual(results[0], INPUT[0].expectedTranslation);
assert.strictEqual(results[1], INPUT[1].expectedTranslation);
});

it('should autodetect HTML', (done) => {
it('should autodetect HTML', async () => {
const input = '<body>' + INPUT[0].input + '</body>';

const opts = {
from: 'en',
to: 'es',
};

translate.translate(input, opts, (err, results) => {
assert.ifError(err);

const translation = results!.split(/<\/*body>/g)[1].trim();

assert.strictEqual(
removeSymbols(translation), INPUT[0].expectedTranslation);

done();
});
const [results] = await translate.translate(input, opts);
const translation = results.split(/<\/*body>/g)[1].trim();
assert.strictEqual(
removeSymbols(translation), INPUT[0].expectedTranslation);
});
});

describe('supported languages', () => {
it('should get a list of supported languages', (done) => {
translate.getLanguages((err, languages) => {
assert.ifError(err);

const englishResult = languages!.filter(language => {
return language.code === 'en';
})[0];

assert.deepStrictEqual(englishResult, {
code: 'en',
name: 'English',
});

done();
it('should get a list of supported languages', async () => {
const [languages] = await translate.getLanguages();
const englishResult = languages.filter(l => l.code === 'en')[0];
assert.deepStrictEqual(englishResult, {
code: 'en',
name: 'English',
});
});

it('should accept a target language', (done) => {
translate.getLanguages('es', (err, languages) => {
assert.ifError(err);

const englishResult = languages!.filter((language) => {
return language.code === 'en';
})[0];

assert.deepStrictEqual(englishResult, {
code: 'en',
name: 'inglés',
});

done();
it('should accept a target language', async () => {
const [languages] = await translate.getLanguages('es');
const englishResult = languages.filter((language) => {
return language.code === 'en';
})[0];
assert.deepStrictEqual(englishResult, {
code: 'en',
name: 'inglés',
});
});
});