@sefinek/google-tts-api 2.1.3 → 2.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -4
- package/coverage/assertInputTypes.ts.html +142 -0
- package/coverage/base.css +224 -0
- package/coverage/block-navigation.js +87 -0
- package/coverage/favicon.png +0 -0
- package/coverage/getAudioBase64.ts.html +481 -0
- package/coverage/getAudioUrl.ts.html +361 -0
- package/coverage/index.html +176 -0
- package/coverage/index.ts.html +97 -0
- package/coverage/lcov-report/assertInputTypes.ts.html +142 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/getAudioBase64.ts.html +481 -0
- package/coverage/lcov-report/getAudioUrl.ts.html +361 -0
- package/coverage/lcov-report/index.html +176 -0
- package/coverage/lcov-report/index.ts.html +97 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +196 -0
- package/coverage/lcov-report/splitLongText.ts.html +319 -0
- package/coverage/lcov.info +257 -0
- package/coverage/prettify.css +1 -0
- package/coverage/prettify.js +2 -0
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +196 -0
- package/coverage/splitLongText.ts.html +319 -0
- package/dist/getAudioBase64.js +23 -6
- package/dist/getAudioBase64.js.map +1 -1
- package/package.json +3 -13
- package/test/basic.test.js +29 -0
- package/test/long-text.test.js +187 -0
- package/test/param.test.js +65 -0
- package/test/parse-error.test.js +50 -0
- package/test/split-long-text.test.js +84 -0
- package/tsconfig.json +73 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
const googleTTS = require('../dist/index');
|
|
2
|
+
|
|
3
|
+
const TestCases = [
|
|
4
|
+
[null, {}, 'text should be a string'],
|
|
5
|
+
['', {}, 'text should be a string'],
|
|
6
|
+
[123, {}, 'text should be a string'],
|
|
7
|
+
['test', { lang: null }, 'lang should be a string'],
|
|
8
|
+
['test', { lang: '' }, 'lang should be a string'],
|
|
9
|
+
['test', { lang: 123 }, 'lang should be a string'],
|
|
10
|
+
['test', { slow: null }, 'slow should be a boolean'],
|
|
11
|
+
['test', { slow: 123 }, 'slow should be a boolean'],
|
|
12
|
+
['test', { host: null }, 'host should be a string'],
|
|
13
|
+
['test', { host: '' }, 'host should be a string'],
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
test('test paramater for TTS URL', async () => {
|
|
17
|
+
for (const [text, option, errorMessage] of TestCases) {
|
|
18
|
+
// 1. audio url
|
|
19
|
+
expect(() => {
|
|
20
|
+
googleTTS.getAudioUrl(text, option);
|
|
21
|
+
}).toThrow(errorMessage);
|
|
22
|
+
|
|
23
|
+
// 2. all audio url
|
|
24
|
+
expect(() => {
|
|
25
|
+
googleTTS.getAllAudioUrls(text, option);
|
|
26
|
+
}).toThrow(errorMessage);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test('test paramater for TTS base64', async () => {
|
|
31
|
+
const Base64TestCases = [
|
|
32
|
+
...TestCases,
|
|
33
|
+
['test', { timeout: null }, 'timeout should be a positive number'],
|
|
34
|
+
['test', { timeout: -10 }, 'timeout should be a positive number'],
|
|
35
|
+
['test', { timeout: 10 }, 'timeout of 10ms exceeded'],
|
|
36
|
+
['test', { lang: 'DOG-LANG' }, 'lang "DOG-LANG" might not exist'],
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
for (const [text, option, errorMessage] of Base64TestCases) {
|
|
40
|
+
// 1. audio base64
|
|
41
|
+
await expect(() => {
|
|
42
|
+
return googleTTS.getAudioBase64(text, option);
|
|
43
|
+
}).rejects.toThrow(errorMessage);
|
|
44
|
+
|
|
45
|
+
// 2. all audio base64
|
|
46
|
+
await expect(() => {
|
|
47
|
+
return googleTTS.getAllAudioBase64(text, option);
|
|
48
|
+
}).rejects.toThrow(errorMessage);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('test splitPunct option for all URL and all base64', async () => {
|
|
53
|
+
const option = { splitPunct: null };
|
|
54
|
+
const errorMessage = 'splitPunct should be a string';
|
|
55
|
+
|
|
56
|
+
// 1. all audio url
|
|
57
|
+
expect(() => {
|
|
58
|
+
googleTTS.getAllAudioUrls('test', option);
|
|
59
|
+
}).toThrow(errorMessage);
|
|
60
|
+
|
|
61
|
+
// 2. all audio base64
|
|
62
|
+
await expect(() => {
|
|
63
|
+
return googleTTS.getAllAudioBase64('test', option);
|
|
64
|
+
}).rejects.toThrow(errorMessage);
|
|
65
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
const axios = require('axios');
|
|
2
|
+
const googleTTS = require('../dist/index');
|
|
3
|
+
|
|
4
|
+
jest.mock('axios');
|
|
5
|
+
|
|
6
|
+
describe('parse base64 text', () => {
|
|
7
|
+
it('HTTP request failed', async () => {
|
|
8
|
+
axios.mockRejectedValue(new Error('Network Error'));
|
|
9
|
+
|
|
10
|
+
await expect(() => {
|
|
11
|
+
return googleTTS.getAudioBase64('test');
|
|
12
|
+
}).rejects.toThrow('Network Error');
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('parse failed (Part 1)', async () => {
|
|
16
|
+
axios.mockResolvedValue({ data: 'UNEXPECT RETURN VALUE' });
|
|
17
|
+
|
|
18
|
+
await expect(() => {
|
|
19
|
+
return googleTTS.getAudioBase64('test');
|
|
20
|
+
}).rejects.toThrow('parse response failed');
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('parse failed (Part 2)', async () => {
|
|
24
|
+
const data = `)]}'
|
|
25
|
+
|
|
26
|
+
[["wrb.fr","jQ1olc","UNEXPECT VALUE",null,null,null,"generic"]
|
|
27
|
+
,["di",27]
|
|
28
|
+
,["af.httprm",27,"3172555329265361983",8]
|
|
29
|
+
]`;
|
|
30
|
+
axios.mockResolvedValue({ data });
|
|
31
|
+
|
|
32
|
+
await expect(() => {
|
|
33
|
+
return googleTTS.getAudioBase64('test');
|
|
34
|
+
}).rejects.toThrow('parse response failed');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('language does not exist', async () => {
|
|
38
|
+
const data = `)]}'
|
|
39
|
+
|
|
40
|
+
[["wrb.fr","jQ1olc",null,null,null,null,"generic"]
|
|
41
|
+
,["di",27]
|
|
42
|
+
,["af.httprm",27,"3172555329265361983",8]
|
|
43
|
+
]`;
|
|
44
|
+
axios.mockResolvedValue({ data });
|
|
45
|
+
|
|
46
|
+
await expect(() => {
|
|
47
|
+
return googleTTS.getAudioBase64('test', { lang: 'CAT-LANG' });
|
|
48
|
+
}).rejects.toThrow('lang "CAT-LANG" might not exist');
|
|
49
|
+
});
|
|
50
|
+
});
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
const splitLongText = require('../dist/splitLongText').default;
|
|
2
|
+
|
|
3
|
+
const longArticle = `The Industrial Revolution had several roots, one of which was a commercial revolution that, beginning as far back as the sixteenth century, accompanied Europe’s expansion overseas.
|
|
4
|
+
Both exports and imports showed spectacular growth, particularly in England and France.
|
|
5
|
+
An increasingly larger portion of the stepped-up commercial activity was the result of trade with overseas colonies.
|
|
6
|
+
Imports included a variety of new beverages, spices, and ship’s goods around the world and brought money flowing back.
|
|
7
|
+
Europe’s economic institutions, particularly those in England, were strong, had wealth available for new investment, and seemed almost to be waiting for some technological breakthrough that would expand their profit-making potential even more.
|
|
8
|
+
|
|
9
|
+
The breakthrough came in Great Britain, where several economic advantages created a climate especially favorable to the encouragement of new technology.
|
|
10
|
+
One was its geographic location at the crossroads of international trade.
|
|
11
|
+
Internally, Britain was endowed with easily navigable natural waterway, which helped its trade and communication with the world.
|
|
12
|
+
Beginning in the 1770’s, it enjoyed a boom in canal building, which helped make its domestic market more accessible.
|
|
13
|
+
Because water transportation was the cheapest means of carrying goods to market, canals reduced prices and thus increased consumer demand.
|
|
14
|
+
Great Britain also had rich deposits of coal that fed the factories springing up in industrial and consumer goods.
|
|
15
|
+
|
|
16
|
+
Another advantage was Britain’s large population of rural, agricultural wage earners,as well as cottage workers, who had the potential of being more mobile than peasants of some other countries.
|
|
17
|
+
Eventually they found their way to the cities or mining communities and provided the human power upon which the Industrial Revolution was built.
|
|
18
|
+
The British people were also consumers; the absence of internal tariffs, such as those that existed in France or Italy or between the German states, made Britain the largest free-trade area in Europe.
|
|
19
|
+
Britain’s relatively stable government also helped create an atmosphere conducive to industrial progress.
|
|
20
|
+
|
|
21
|
+
Great Britain’s better-developed banking and credit system also helped speed the industrial progress, as did the fact that it was the home of an impressive array of entrepreneurs and inventors.
|
|
22
|
+
Among them were a large number of nonconformists whose religious principles encouraged thrift and industry rather than luxurious living and who tended to pour their profits back into their business, thus providing the basis for continued expansion.
|
|
23
|
+
|
|
24
|
+
A precursor to the Industrial Revolution was a revolution in agricultural techniques.
|
|
25
|
+
Ideas about agricultural reform developed first in Holland, where as early as the mid-seventeenth century, such modern methods as crop rotation, heavy fertilization, and diversification were all in use.
|
|
26
|
+
Dutch peasant farmers were known throughout Europe for their agricultural innovations, but as British markets and opportunities grew, the English quickly learned from them.
|
|
27
|
+
As early as the seventeenth century the Dutch were helping them drain marshes and fens where, with the help of advanced techniques, they grew new crops.
|
|
28
|
+
By the mid-eighteenth century new agricultural methods as well as selective breeding of livestock had caught on throughout the country.
|
|
29
|
+
|
|
30
|
+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
|
31
|
+
|
|
32
|
+
Much of the increased production was consumed by Great Britain’s burgeoning population.
|
|
33
|
+
At the same time, people were moving to the city, partly because of the enclosure movement; that is, the fencing of common fields and pastures in order to provide more compact, efficient privately held agricultural parcels that would produce more goods and greater profits.
|
|
34
|
+
In the sixteenth century enclosures were usually used for creating sheep pastures, but by the eighteenth century new farming techniques made it advantageous for large landowners to seek enclosures in order to improve agricultural production.
|
|
35
|
+
Between 1714 and 1820 over 6 million acres of English land were enclosed.
|
|
36
|
+
As a result, many small, independent farmers were forced to sell out simply because they could not compete.
|
|
37
|
+
Non-landholding peasants and cottage workers, who worked for wages and grazed cows or pigs on the village common, were also hurt when the common was no longer available.
|
|
38
|
+
It was such people who began to flock to the cities seeking employment and who found work in the factories that would transform the nation and, the world.
|
|
39
|
+
|
|
40
|
+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`;
|
|
41
|
+
|
|
42
|
+
const text200 =
|
|
43
|
+
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
|
|
44
|
+
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
|
|
45
|
+
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
|
|
46
|
+
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
|
|
47
|
+
|
|
48
|
+
describe('Split long text', () => {
|
|
49
|
+
it('each the length of short text should shorter than 200', () => {
|
|
50
|
+
const results = splitLongText(longArticle);
|
|
51
|
+
for (const str of results) {
|
|
52
|
+
expect(str.length <= 200).toBe(true);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('the single word with 200 length', () => {
|
|
57
|
+
expect(splitLongText(text200)).toStrictEqual([text200]);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('the single word is too long to split into short text', () => {
|
|
61
|
+
expect(() => splitLongText(text200, { maxLength: 100 })).toThrow(
|
|
62
|
+
'The word is too long to split into a short text:',
|
|
63
|
+
);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('split long chinese text into short text', () => {
|
|
67
|
+
const longChineseText =
|
|
68
|
+
'如果想想生物在死之后被完全摧毁的种种方式,能够这样频繁出现化石是一件很令人惊讶的事。食腐动物和细菌的' +
|
|
69
|
+
'破坏、化学性腐烂、腐蚀以及其它地质因素都会非常不利于保存。不过,如果生物体碰巧具有矿化的骨骼并且死于' +
|
|
70
|
+
'可以迅速被沉积物掩埋的地方,摆脱被完全摧毁的几率便会大大增加。海底通常就具有上述的两方面条件,这里生' +
|
|
71
|
+
'活着很多带壳的无脊椎动物(没有脊椎的动物),不断累积的似雨的沉积颗粒会把它们掩埋起来。虽然多数的化石' +
|
|
72
|
+
'是在海洋沉积岩中发现的';
|
|
73
|
+
|
|
74
|
+
// no additional split punctuation
|
|
75
|
+
expect(() => splitLongText(longChineseText)).toThrow(
|
|
76
|
+
'The word is too long to split into a short text:',
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
// add additional split punctuation
|
|
80
|
+
const results = splitLongText(longChineseText, { splitPunct: ',、。()' });
|
|
81
|
+
expect(results.length).toBe(2);
|
|
82
|
+
expect(results[1]).toBe('虽然多数的化石是在海洋沉积岩中发现的');
|
|
83
|
+
});
|
|
84
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
/* Visit https://aka.ms/tsconfig.json to read more about this file */
|
|
4
|
+
|
|
5
|
+
/* Basic Options */
|
|
6
|
+
// "incremental": true, /* Enable incremental compilation */
|
|
7
|
+
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
|
|
8
|
+
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
|
|
9
|
+
// "lib": [], /* Specify library files to be included in the compilation. */
|
|
10
|
+
// "allowJs": true, /* Allow javascript files to be compiled. */
|
|
11
|
+
// "checkJs": true, /* Report errors in .js files. */
|
|
12
|
+
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
|
|
13
|
+
"declaration": true, /* Generates corresponding '.d.ts' file. */
|
|
14
|
+
"declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
|
|
15
|
+
"sourceMap": true, /* Generates corresponding '.map' file. */
|
|
16
|
+
// "outFile": "./", /* Concatenate and emit output to single file. */
|
|
17
|
+
"outDir": "./dist", /* Redirect output structure to the directory. */
|
|
18
|
+
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
|
|
19
|
+
// "composite": true, /* Enable project compilation */
|
|
20
|
+
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
|
|
21
|
+
// "removeComments": true, /* Do not emit comments to output. */
|
|
22
|
+
// "noEmit": true, /* Do not emit outputs. */
|
|
23
|
+
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
|
|
24
|
+
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
|
|
25
|
+
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
|
|
26
|
+
|
|
27
|
+
/* Strict Type-Checking Options */
|
|
28
|
+
"strict": true, /* Enable all strict type-checking options. */
|
|
29
|
+
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
|
|
30
|
+
// "strictNullChecks": true, /* Enable strict null checks. */
|
|
31
|
+
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
|
|
32
|
+
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
|
|
33
|
+
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
|
|
34
|
+
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
|
|
35
|
+
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
|
|
36
|
+
|
|
37
|
+
/* Additional Checks */
|
|
38
|
+
// "noUnusedLocals": true, /* Report errors on unused locals. */
|
|
39
|
+
// "noUnusedParameters": true, /* Report errors on unused parameters. */
|
|
40
|
+
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
|
|
41
|
+
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
|
|
42
|
+
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
|
|
43
|
+
|
|
44
|
+
/* Module Resolution Options */
|
|
45
|
+
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
|
|
46
|
+
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
|
|
47
|
+
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
|
|
48
|
+
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
|
|
49
|
+
// "typeRoots": [], /* List of folders to include type definitions from. */
|
|
50
|
+
// "types": [], /* Type declaration files to be included in compilation. */
|
|
51
|
+
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
|
|
52
|
+
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
|
|
53
|
+
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
|
|
54
|
+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
|
55
|
+
|
|
56
|
+
/* Source Map Options */
|
|
57
|
+
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
|
|
58
|
+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
59
|
+
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
|
|
60
|
+
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
|
|
61
|
+
|
|
62
|
+
/* Experimental Options */
|
|
63
|
+
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
|
|
64
|
+
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
|
|
65
|
+
|
|
66
|
+
/* Advanced Options */
|
|
67
|
+
"skipLibCheck": true, /* Skip type checking of declaration files. */
|
|
68
|
+
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
|
|
69
|
+
},
|
|
70
|
+
"include": [
|
|
71
|
+
"src"
|
|
72
|
+
]
|
|
73
|
+
}
|