Skip to content

Commit

Permalink
🎉 Bulid: release 0.2.3-beta8
Browse files Browse the repository at this point in the history
  • Loading branch information
DarinRowe committed May 8, 2020
1 parent ae6f476 commit a34ef48
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "googletrans",
"version": "0.2.3-beta.7",
"version": "0.2.3-beta.8",
"description": "Free and Unlimited Google translate API for node.js",
"files": [
"lib/**/*"
Expand Down
33 changes: 33 additions & 0 deletions src/__tests__/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe("translate Methods Test", () => {
return googletrans("vue").then((res) => {
expect(res.src).toBe("fr");
expect(res.hasCorrectedLang).toBe(false);
expect(res.hasCorrectedText).toBe(false);
});
});

Expand All @@ -29,6 +30,7 @@ describe("translate Methods Test", () => {
.then((res) => {
expect(res.hasCorrectedLang).toBe(true);
expect(res.src).toBe("en");
expect(res.hasCorrectedText).toBe(false);
})
.catch((err) => {
console.log(err);
Expand All @@ -39,6 +41,7 @@ describe("translate Methods Test", () => {
try {
const res = await googletrans("Hero", { to: "zh" });
expect(res.text).toBe("英雄");
expect(res.hasCorrectedText).toBe(false);
} catch (e) {
console.log(e);
}
Expand All @@ -50,6 +53,7 @@ describe("translate Methods Test", () => {
expect(res.text).toBe("Grün");
expect(res.src).toBe("en");
expect(res.hasCorrectedLang).toBe(true);
expect(res.hasCorrectedText).toBe(false);
})
.catch((err) => {
expect(err.message).toMatch(/not/);
Expand All @@ -60,6 +64,7 @@ describe("translate Methods Test", () => {
return googletrans("Green", { from: "en", to: "Green" })
.then((res) => {
expect(res.text).toBe("Green");
expect(res.hasCorrectedText).toBe(false);
})
.catch((err) => {
expect(err.message).toMatch(/not/);
Expand All @@ -72,6 +77,24 @@ describe("translate Methods Test", () => {
expect(res.src).toBe("nl");
});
});
test("zh-hk", async () => {
try {
const res = await googletrans("media", "zh-hk");
expect(res.text).toBe("媒體");
expect(res.hasCorrectedText).toBe(false);
} catch (error) {
console.log(error);
}
});
test("zh-sg", async () => {
try {
const res = await googletrans("Game console", "zh-sg");
expect(res.text).toBe("游戏机");
expect(res.hasCorrectedText).toBe(false);
} catch (error) {
console.log(error);
}
});
test("batch translation through array without empty string.", async () => {
try {
const res = await googletrans(["blue", "green", "yellow"], "nl");
Expand Down Expand Up @@ -198,6 +221,16 @@ describe("getToken method Test", () => {
test("get Token by English", () => {
expect(getToken("Green")).toBe("701361.821189");
});

test("Unicode > 2048", () => {
expect(getToken("⁉")).toBe("631846.1019986");
});

test("2048 > Unicode > 128", () => {
// const a = getToken("ᢈ");
// console.log(a);
expect(getToken("ᢈ")).toBe("951746.569782");
});
});

describe("random number method Test", () => {
Expand Down
13 changes: 7 additions & 6 deletions src/googletrans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,12 @@ function getResult(res: any): Result {
translations: [],
raw: [],
};
if (res === null) return result;
if (res.status === 200) result.raw = res.data;

if (res !== null && res.status === 200) {
result.raw = res.data;
} else {
return result;
}
const body = res.data;
body[0].forEach((obj: string) => {
if (obj[0]) {
Expand All @@ -161,10 +165,7 @@ function getResult(res: any): Result {
str = str.replace(/<\/i><\/b>/g, "]");
result.correctedText = str;

let a = false;
let b = false;
body[7][5] === true ? (a = true) : (b = true);
if (a || b) result.hasCorrectedText = true;
if (body[7][5]) result.hasCorrectedText = true;
}

if (result.text.indexOf("\n") !== -1) {
Expand Down

0 comments on commit a34ef48

Please sign in to comment.