feat: 增加 腾讯类型使用错误常量

This commit is contained in:
zhangxiangliang 2020-08-22 17:03:29 +08:00
parent d050a0748f
commit d3a68341f2
7 changed files with 42 additions and 28 deletions

View File

@ -5,7 +5,7 @@ import TencentCommonCodeTransform from "@stocks/tencent/transforms/common-code";
// Utils // Utils
import fetch from "@utils/fetch"; import fetch from "@utils/fetch";
import iconv from "@utils/iconv"; import iconv from "@utils/iconv";
import { DEFAULT_STOCK } from "@stocks/base/utils/constant"; import { DEFAULT_STOCK, ERROR_UNDEFINED_SEARCH_STOCK } from "@utils/constant";
// Types // Types
import Stock from "types/utils/stock"; import Stock from "types/utils/stock";
@ -81,7 +81,7 @@ const Tencent: StockApi = {
* @param key * @param key
*/ */
async searchStocks(key: string): Promise<Stock[]> { async searchStocks(key: string): Promise<Stock[]> {
throw new Error("未实现搜索股票代码"); throw new Error(ERROR_UNDEFINED_SEARCH_STOCK);
} }
} }

View File

@ -2,6 +2,7 @@
import BaseApiCodeTransform from "@stocks/base/transforms/api-code"; import BaseApiCodeTransform from "@stocks/base/transforms/api-code";
// Utils // Utils
import { ERROR_API_CODE } from "@utils/constant";
import { COMMON_SH, COMMON_SZ, COMMON_HK, COMMON_US } from "@stocks/base/utils/constant"; import { COMMON_SH, COMMON_SZ, COMMON_HK, COMMON_US } from "@stocks/base/utils/constant";
import { TENCENT_SZ, TENCENT_SH, TENCENT_HK, TENCENT_US } from "@stocks/tencent/utils/constant"; import { TENCENT_SZ, TENCENT_SH, TENCENT_HK, TENCENT_US } from "@stocks/tencent/utils/constant";
@ -30,7 +31,7 @@ class TencentApiCodeTransform extends BaseApiCodeTransform {
return this.USTransform(code); return this.USTransform(code);
} }
throw new Error("请检查股票代码是否正确"); throw new Error(ERROR_API_CODE);
} }
/** /**
@ -47,7 +48,7 @@ class TencentApiCodeTransform extends BaseApiCodeTransform {
*/ */
public SZTransform(code: string): string { public SZTransform(code: string): string {
if (!code.includes(TENCENT_SZ)) { if (!code.includes(TENCENT_SZ)) {
throw new Error("请检查股票代码是否正确"); throw new Error(ERROR_API_CODE);
} }
return COMMON_SZ + code.replace(TENCENT_SZ, ""); return COMMON_SZ + code.replace(TENCENT_SZ, "");
@ -59,7 +60,7 @@ class TencentApiCodeTransform extends BaseApiCodeTransform {
*/ */
public SHTransform(code: string): string { public SHTransform(code: string): string {
if (!code.includes(TENCENT_SH)) { if (!code.includes(TENCENT_SH)) {
throw new Error("请检查股票代码是否正确"); throw new Error(ERROR_API_CODE);
} }
return COMMON_SH + code.replace(TENCENT_SH, ""); return COMMON_SH + code.replace(TENCENT_SH, "");
@ -71,7 +72,7 @@ class TencentApiCodeTransform extends BaseApiCodeTransform {
*/ */
public HKTransform(code: string): string { public HKTransform(code: string): string {
if (!code.includes(TENCENT_HK)) { if (!code.includes(TENCENT_HK)) {
throw new Error("请检查股票代码是否正确"); throw new Error(ERROR_API_CODE);
} }
return COMMON_HK + code.replace(TENCENT_HK, ""); return COMMON_HK + code.replace(TENCENT_HK, "");
@ -83,7 +84,7 @@ class TencentApiCodeTransform extends BaseApiCodeTransform {
*/ */
public USTransform(code: string): string { public USTransform(code: string): string {
if (!code.includes(TENCENT_US)) { if (!code.includes(TENCENT_US)) {
throw new Error("请检查股票代码是否正确"); throw new Error(ERROR_API_CODE);
} }
return COMMON_US + code.replace(TENCENT_US, ""); return COMMON_US + code.replace(TENCENT_US, "");

View File

@ -2,6 +2,7 @@
import BaseCommonCodeTransform from "@stocks/base/transforms/common-code"; import BaseCommonCodeTransform from "@stocks/base/transforms/common-code";
// Utils // Utils
import { ERROR_COMMON_CODE } from "@utils/constant";
import { COMMON_SH, COMMON_SZ, COMMON_HK, COMMON_US } from "@stocks/base/utils/constant"; import { COMMON_SH, COMMON_SZ, COMMON_HK, COMMON_US } from "@stocks/base/utils/constant";
import { TENCENT_SZ, TENCENT_SH, TENCENT_HK, TENCENT_US } from "@stocks/tencent/utils/constant"; import { TENCENT_SZ, TENCENT_SH, TENCENT_HK, TENCENT_US } from "@stocks/tencent/utils/constant";
@ -38,7 +39,7 @@ class TencentCommonCodeTransform extends BaseCommonCodeTransform {
*/ */
public SZTransform(code: string): string { public SZTransform(code: string): string {
if (!code.includes(COMMON_SZ)) { if (!code.includes(COMMON_SZ)) {
throw new Error("请检查统一代码是否正确"); throw new Error(ERROR_COMMON_CODE);
} }
return TENCENT_SZ + code.replace(COMMON_SZ, ""); return TENCENT_SZ + code.replace(COMMON_SZ, "");
@ -50,7 +51,7 @@ class TencentCommonCodeTransform extends BaseCommonCodeTransform {
*/ */
public SHTransform(code: string): string { public SHTransform(code: string): string {
if (!code.includes(COMMON_SH)) { if (!code.includes(COMMON_SH)) {
throw new Error("请检查统一代码是否正确"); throw new Error(ERROR_COMMON_CODE);
} }
return TENCENT_SH + code.replace(COMMON_SH, ""); return TENCENT_SH + code.replace(COMMON_SH, "");
@ -62,7 +63,7 @@ class TencentCommonCodeTransform extends BaseCommonCodeTransform {
*/ */
public HKTransform(code: string): string { public HKTransform(code: string): string {
if (!code.includes(COMMON_HK)) { if (!code.includes(COMMON_HK)) {
throw new Error("请检查统一代码是否正确"); throw new Error(ERROR_COMMON_CODE);
} }
return TENCENT_HK + code.replace(COMMON_HK, ""); return TENCENT_HK + code.replace(COMMON_HK, "");
@ -74,7 +75,7 @@ class TencentCommonCodeTransform extends BaseCommonCodeTransform {
*/ */
public USTransform(code: string): string { public USTransform(code: string): string {
if (!code.includes(COMMON_US)) { if (!code.includes(COMMON_US)) {
throw new Error("请检查统一代码是否正确"); throw new Error(ERROR_COMMON_CODE);
} }
return TENCENT_US + code.replace(COMMON_US, ""); return TENCENT_US + code.replace(COMMON_US, "");

View File

@ -1,6 +1,9 @@
// Stocks // Stocks
import BaseStockTransform from "@stocks/base/transforms/stock"; import BaseStockTransform from "@stocks/base/transforms/stock";
// Utils
import { DEFAULT_STRING, DEFAULT_NUMBER } from "@utils/constant";
// Types // Types
import Stock from "types/utils/stock"; import Stock from "types/utils/stock";
@ -26,35 +29,35 @@ class TencentStockTransform extends BaseStockTransform {
* *
*/ */
getName(): string { getName(): string {
return String(this.params[1]); return String(this.params[1] || DEFAULT_STRING);
} }
/** /**
* *
*/ */
getNow(): number { getNow(): number {
return Number(this.params[3]); return Number(this.params[3] || DEFAULT_NUMBER);
} }
/** /**
* *
*/ */
getLow(): number { getLow(): number {
return Number(this.params[34]); return Number(this.params[34] || DEFAULT_NUMBER);
} }
/** /**
* *
*/ */
getHigh(): number { getHigh(): number {
return Number(this.params[33]); return Number(this.params[33] || DEFAULT_NUMBER);
} }
/** /**
* *
*/ */
getYesterday(): number { getYesterday(): number {
return Number(this.params[4]); return Number(this.params[4] || DEFAULT_NUMBER);
} }
/** /**

View File

@ -1,3 +1,4 @@
// 默认交易所常量
export const TENCENT_SZ = "sz"; // 深交所相关常量 export const TENCENT_SZ = "sz"; // 深交所相关常量
export const TENCENT_SH = "sh"; // 上交所相关常量 export const TENCENT_SH = "sh"; // 上交所相关常量
export const TENCENT_HK = "hk"; // 港交所相关常量 export const TENCENT_HK = "hk"; // 港交所相关常量

View File

@ -2,9 +2,13 @@
const TencentApiCodeTransform = require("stocks/tencent/transforms/api-code").default; const TencentApiCodeTransform = require("stocks/tencent/transforms/api-code").default;
describe("【腾讯】股票代码转换统一代码", () => { describe("【腾讯】股票代码转换统一代码", () => {
const {
ERROR_API_CODE,
} = require("utils/constant");
it("深交所股票代码转换统一代码", async () => { it("深交所股票代码转换统一代码", async () => {
expect(() => (new TencentApiCodeTransform()).SZTransform("STOCKAPI")) expect(() => (new TencentApiCodeTransform()).SZTransform("STOCKAPI"))
.toThrow(new Error("请检查股票代码是否正确")); .toThrow(new Error(ERROR_API_CODE));
expect((new TencentApiCodeTransform()).SZTransform("sz000000")) expect((new TencentApiCodeTransform()).SZTransform("sz000000"))
.toBe("SZ000000"); .toBe("SZ000000");
@ -12,7 +16,7 @@ describe("【腾讯】股票代码转换统一代码", () => {
it("上交所股票代码转换统一代码", async () => { it("上交所股票代码转换统一代码", async () => {
expect(() => (new TencentApiCodeTransform()).SHTransform("STOCKAPI")) expect(() => (new TencentApiCodeTransform()).SHTransform("STOCKAPI"))
.toThrow(new Error("请检查股票代码是否正确")); .toThrow(new Error(ERROR_API_CODE));
expect((new TencentApiCodeTransform()).SHTransform("sh000000")) expect((new TencentApiCodeTransform()).SHTransform("sh000000"))
.toBe("SH000000"); .toBe("SH000000");
@ -20,7 +24,7 @@ describe("【腾讯】股票代码转换统一代码", () => {
it("港交所股票代码转换统一代码", async () => { it("港交所股票代码转换统一代码", async () => {
expect(() => (new TencentApiCodeTransform()).HKTransform("STOCKAPI")) expect(() => (new TencentApiCodeTransform()).HKTransform("STOCKAPI"))
.toThrow(new Error("请检查股票代码是否正确")); .toThrow(new Error(ERROR_API_CODE));
expect((new TencentApiCodeTransform()).HKTransform("hk000000")) expect((new TencentApiCodeTransform()).HKTransform("hk000000"))
.toBe("HK000000"); .toBe("HK000000");
@ -28,7 +32,7 @@ describe("【腾讯】股票代码转换统一代码", () => {
it("美交所股票代码转换统一代码", async () => { it("美交所股票代码转换统一代码", async () => {
expect(() => (new TencentApiCodeTransform()).USTransform("STOCKAPI")) expect(() => (new TencentApiCodeTransform()).USTransform("STOCKAPI"))
.toThrow(new Error("请检查股票代码是否正确")); .toThrow(new Error(ERROR_API_CODE));
expect((new TencentApiCodeTransform()).USTransform("us000000")) expect((new TencentApiCodeTransform()).USTransform("us000000"))
.toBe("US000000"); .toBe("US000000");
@ -48,7 +52,7 @@ describe("【腾讯】股票代码转换统一代码", () => {
.toBe("US000000"); .toBe("US000000");
expect(() => (new TencentApiCodeTransform()).transform("STOCKAPI")) expect(() => (new TencentApiCodeTransform()).transform("STOCKAPI"))
.toThrow(new Error("请检查股票代码是否正确")); .toThrow(new Error(ERROR_API_CODE));
}); });
it("交易所股票代码组转换统一代码组", async () => { it("交易所股票代码组转换统一代码组", async () => {
@ -65,6 +69,6 @@ describe("【腾讯】股票代码转换统一代码", () => {
.toStrictEqual(["US000000"]); .toStrictEqual(["US000000"]);
expect(() => (new TencentApiCodeTransform()).transforms(["STOCKAPI"])) expect(() => (new TencentApiCodeTransform()).transforms(["STOCKAPI"]))
.toThrow(new Error("请检查股票代码是否正确")); .toThrow(new Error(ERROR_API_CODE));
}); });
}); });

View File

@ -2,9 +2,13 @@
const TencentCommonCodeTransform = require("stocks/tencent/transforms/common-code").default; const TencentCommonCodeTransform = require("stocks/tencent/transforms/common-code").default;
describe("【腾讯】统一代码转换股票代码", () => { describe("【腾讯】统一代码转换股票代码", () => {
const {
ERROR_COMMON_CODE,
} = require("utils/constant");
it("深交所统一代码转换股票代码", async () => { it("深交所统一代码转换股票代码", async () => {
expect(() => (new TencentCommonCodeTransform()).SZTransform("000000")) expect(() => (new TencentCommonCodeTransform()).SZTransform("000000"))
.toThrow(new Error("请检查统一代码是否正确")); .toThrow(new Error(ERROR_COMMON_CODE));
expect((new TencentCommonCodeTransform()).SZTransform("SZ000000")) expect((new TencentCommonCodeTransform()).SZTransform("SZ000000"))
.toBe("sz000000"); .toBe("sz000000");
@ -12,7 +16,7 @@ describe("【腾讯】统一代码转换股票代码", () => {
it("上交所统一代码转换股票代码", async () => { it("上交所统一代码转换股票代码", async () => {
expect(() => (new TencentCommonCodeTransform()).SHTransform("000000")) expect(() => (new TencentCommonCodeTransform()).SHTransform("000000"))
.toThrow(new Error("请检查统一代码是否正确")); .toThrow(new Error(ERROR_COMMON_CODE));
expect((new TencentCommonCodeTransform()).SHTransform("SH000000")) expect((new TencentCommonCodeTransform()).SHTransform("SH000000"))
.toBe("sh000000"); .toBe("sh000000");
@ -20,7 +24,7 @@ describe("【腾讯】统一代码转换股票代码", () => {
it("港交所统一代码转换股票代码", async () => { it("港交所统一代码转换股票代码", async () => {
expect(() => (new TencentCommonCodeTransform()).HKTransform("000000")) expect(() => (new TencentCommonCodeTransform()).HKTransform("000000"))
.toThrow(new Error("请检查统一代码是否正确")); .toThrow(new Error(ERROR_COMMON_CODE));
expect((new TencentCommonCodeTransform()).HKTransform("HK000000")) expect((new TencentCommonCodeTransform()).HKTransform("HK000000"))
.toBe("hk000000"); .toBe("hk000000");
@ -28,7 +32,7 @@ describe("【腾讯】统一代码转换股票代码", () => {
it("美交所统一代码转换股票代码", async () => { it("美交所统一代码转换股票代码", async () => {
expect(() => (new TencentCommonCodeTransform()).USTransform("000000")) expect(() => (new TencentCommonCodeTransform()).USTransform("000000"))
.toThrow(new Error("请检查统一代码是否正确")); .toThrow(new Error(ERROR_COMMON_CODE));
expect((new TencentCommonCodeTransform()).USTransform("US000000")) expect((new TencentCommonCodeTransform()).USTransform("US000000"))
.toBe("us000000"); .toBe("us000000");
@ -48,7 +52,7 @@ describe("【腾讯】统一代码转换股票代码", () => {
.toBe("us000000"); .toBe("us000000");
expect(() => (new TencentCommonCodeTransform()).transform("000000")) expect(() => (new TencentCommonCodeTransform()).transform("000000"))
.toThrow(new Error("请检查统一代码是否正确")); .toThrow(new Error(ERROR_COMMON_CODE));
}); });
it("交易所统一代码组转换股票代码组", async () => { it("交易所统一代码组转换股票代码组", async () => {
@ -65,6 +69,6 @@ describe("【腾讯】统一代码转换股票代码", () => {
.toStrictEqual(["us000000"]); .toStrictEqual(["us000000"]);
expect(() => (new TencentCommonCodeTransform()).transforms(["000000"])) expect(() => (new TencentCommonCodeTransform()).transforms(["000000"]))
.toThrow(new Error("请检查统一代码是否正确")); .toThrow(new Error(ERROR_COMMON_CODE));
}); });
}); });