Skip to content

Commit

Permalink
fix issue for zlargon#33
Browse files Browse the repository at this point in the history
  • Loading branch information
freddiefujiwara committed Dec 2, 2020
1 parent 118dff2 commit d8ddfcf
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 41 deletions.
8 changes: 2 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var key = require('./lib/key');
var tts = require('./lib/api');

/**
Expand All @@ -7,11 +6,8 @@ var tts = require('./lib/api');
* @param {String} text
* @param {String!} lang default is 'en'
* @param {Number!} speed default is 1, show = 0.24
* @param {Number!} timeout default is 10000ms
* @return Promise(url: String)
*/
module.exports = function (text, lang, speed, timeout) {
return key(timeout).then(function (key) {
return tts(text, key, lang, speed);
});
module.exports = function (text, lang, speed) {
return tts(text, lang, speed);
};
10 changes: 2 additions & 8 deletions lib/api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var url = require('url');
var token = require('./token');
var host = 'https://translate.google.com';

/**
Expand All @@ -11,7 +10,7 @@ var host = 'https://translate.google.com';
* @param {Number!} speed show = 0.24, default is 1
* @return {String} url
*/
module.exports = function (text, key, lang, speed) {
module.exports = function (text, lang, speed) {
if (typeof text !== 'string' || text.length === 0) {
throw new TypeError('text should be a string');
}
Expand All @@ -20,10 +19,6 @@ module.exports = function (text, key, lang, speed) {
throw new RangeError('text length (' + text.length + ') should be less than 200 characters');
}

if (typeof key !== 'string' || key.length === 0) {
throw new TypeError('key should be a string');
}

if (typeof lang !== 'undefined' && (typeof lang !== 'string' || lang.length === 0)) {
throw new TypeError('lang should be a string');
}
Expand All @@ -40,8 +35,7 @@ module.exports = function (text, key, lang, speed) {
total: 1,
idx: 0,
textlen: text.length,
tk: token(text, key),
client: 't',
client: 'tw-ob',
prev: 'input',
ttsspeed: speed || 1
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions test/long-characters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Long Characters', () => {
'The Industrial Revolution had several roots, one of which was a commercial revolution that, beginnin' +
'g as far back as the sixteenth century, accompanied Europe’s expansion overseas.';

const url = await tts(text, 'en');
const url = tts(text, 'en');
const res = await fetch(url);
expect(res.status).toBe(200);
});
Expand All @@ -18,7 +18,7 @@ describe('Long Characters', () => {
'The Industrial Revolution had several roots, one of which was a commercial revolution that, beginnin' +
'g as far back as the sixteenth century, accompanied Europe’s expansion overseas. exports and imports';

const url = await tts(text, 'en');
const url = tts(text, 'en');
const res = await fetch(url);
expect(res.status).toBe(200);
});
Expand All @@ -29,7 +29,9 @@ describe('Long Characters', () => {
'g as far back as the sixteenth century, accompanied Europe’s expansion overseas. Both exports and im' +
'ports showed spectacular growth, particularly in England and France.';

await expect(tts(text, 'en')).rejects.toThrow('should be less than 200 characters');
expect(()=>{
tts(text, 'en');
}).toThrow('should be less than 200 characters');
});

it('Chinese: 193 characters', async () => {
Expand All @@ -39,7 +41,7 @@ describe('Long Characters', () => {
'可以迅速被沉积物掩埋的地方,摆脱被完全摧毁的几率便会大大增加。海底通常就具有上述的两方面条件,这里生' +
'活着很多带壳的无脊椎动物(没有脊椎的动物),不断累积的似雨的沉积颗粒会把它们掩埋起来。';

const url = await tts(text, 'zh');
const url = tts(text, 'zh');
const res = await fetch(url);
expect(res.status).toBe(200);
});
Expand All @@ -51,7 +53,7 @@ describe('Long Characters', () => {
'可以迅速被沉积物掩埋的地方,摆脱被完全摧毁的几率便会大大增加。海底通常就具有上述的两方面条件,这里生' +
'活着很多带壳的无脊椎动物(没有脊椎的动物),不断累积的似雨的沉积颗粒会把它们掩埋起来。虽然多数的化石';

const url = await tts(text, 'zh');
const url = tts(text, 'zh');
const res = await fetch(url);
expect(res.status).toBe(200);
});
Expand All @@ -64,6 +66,8 @@ describe('Long Characters', () => {
'活着很多带壳的无脊椎动物(没有脊椎的动物),不断累积的似雨的沉积颗粒会把它们掩埋起来。虽然多数的化石' +
'是在海洋沉积岩中发现的';

await expect(tts(text, 'zh')).rejects.toThrow('should be less than 200 characters');
expect(()=>{
tts(text, 'zh');
}).toThrow('should be less than 200 characters');
});
});
36 changes: 16 additions & 20 deletions test/param.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,35 @@ const tts = require('..');
jest.setTimeout(60000);

describe('parameters', () => {
it('text = null', async () => {
await expect(tts(null)).rejects.toThrow('text should be a string');
it('text = null', () => {
expect(()=>{tts(null);}).toThrow('text should be a string');
});

it("text = ''", async () => {
await expect(tts('')).rejects.toThrow('text should be a string');
it("text = ''", () => {
expect(()=>{tts('');}).toThrow('text should be a string');
});

it('text = 123', async () => {
await expect(tts(123)).rejects.toThrow('text should be a string');
it('text = 123', () => {
expect(()=>{tts(123);}).toThrow('text should be a string');
});

it('lang = null', async () => {
await expect(tts('test', null)).rejects.toThrow('lang should be a string');
it('lang = null', () => {
expect(()=>{tts('test', null);}).toThrow('lang should be a string');
});

it("lang = ''", async () => {
await expect(tts('test', '')).rejects.toThrow('lang should be a string');
it("lang = ''", () => {
expect(()=>{tts('test', '');}).toThrow('lang should be a string');
});

it('lang = 123 (number)', async () => {
await expect(tts('test', 123)).rejects.toThrow('lang should be a string');
it('lang = 123 (number)', () => {
expect(()=>{tts('test', 123);}).toThrow('lang should be a string');
});

it('speed = null', async () => {
await expect(tts('test', 'en', null)).rejects.toThrow('speed should be a number');
it('speed = null', () => {
expect(()=>{tts('test', 'en', null);}).toThrow('speed should be a number');
});

it("speed = '123'", async () => {
await expect(tts('test', 'en', '123')).rejects.toThrow('speed should be a number');
});

it('timeout = 10 ms (too short to success)', async () => {
await expect(tts('test', 'en', 1, 10)).rejects.toThrow('network timeout');
it("speed = '123'", () => {
expect(()=>{tts('test', 'en', '123');}).toThrow('speed should be a number');
});
});

0 comments on commit d8ddfcf

Please sign in to comment.