mirror of
https://github.com/zhangxiangliang/stock-api.git
synced 2025-02-02 14:18:44 +08:00
feat: 增加 腾讯类型股票代码与统一代码相关转化
This commit is contained in:
parent
b9608f35c0
commit
00e87534d2
@ -1,7 +1,7 @@
|
||||
// Stocks
|
||||
import Base from "@stocks/base";
|
||||
import TencentStockTransform from "@stocks/tencent/transforms/stock";
|
||||
import TencentApiCodeTransform from "@stocks/tencent/transforms/api-code";
|
||||
import TencentCommonCodeTransform from "@stocks/tencent/transforms/common-code";
|
||||
|
||||
// Utils
|
||||
import fetch from "@utils/fetch";
|
||||
@ -26,7 +26,7 @@ class Tencent extends Base {
|
||||
* @param code 需要获取的股票代码
|
||||
*/
|
||||
async getStock(code: string): Promise<Stock> {
|
||||
const transform = (new TencentApiCodeTransform).transform(code);
|
||||
const transform = (new TencentCommonCodeTransform).transform(code);
|
||||
|
||||
// 数据获取
|
||||
const url = `https://qt.gtimg.cn/q=${transform}`;
|
||||
@ -46,10 +46,10 @@ class Tencent extends Base {
|
||||
|
||||
/**
|
||||
* 获取股票组数据
|
||||
* @param codes 需要获取的股票组代码
|
||||
* @param codes 需要获取的股票代码组
|
||||
*/
|
||||
async getStocks(codes: string[]): Promise<Stock[]> {
|
||||
const transforms = (new TencentApiCodeTransform).transforms(codes);
|
||||
const transforms = (new TencentCommonCodeTransform).transforms(codes);
|
||||
|
||||
// 数据获取
|
||||
const url = `https://qt.gtimg.cn/q=${transforms.join(',')}`;
|
||||
|
@ -2,10 +2,11 @@
|
||||
import BaseApiCodeTransform from "@stocks/base/transforms/api-code";
|
||||
|
||||
// Utils
|
||||
import { SZ, HK, US, SH } from "@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";
|
||||
|
||||
/**
|
||||
* 腾讯股票代码转换
|
||||
* 【腾讯】股票代码转换统一代码
|
||||
*/
|
||||
class TencentApiCodeTransform extends BaseApiCodeTransform {
|
||||
/**
|
||||
@ -16,67 +17,83 @@ class TencentApiCodeTransform extends BaseApiCodeTransform {
|
||||
}
|
||||
|
||||
/**
|
||||
* 交易所股票代码转换
|
||||
* 交易所股票代码转换统一代码
|
||||
* @param code 股票代码
|
||||
*/
|
||||
public transform(code: string): string {
|
||||
return super.transform(code);
|
||||
if (code.indexOf(TENCENT_SZ) === 0) {
|
||||
return this.SZTransform(code);
|
||||
}
|
||||
|
||||
if (code.indexOf(TENCENT_SH) === 0) {
|
||||
return this.SHTransform(code);
|
||||
}
|
||||
|
||||
if (code.indexOf(TENCENT_HK) === 0) {
|
||||
return this.HKTransform(code);
|
||||
}
|
||||
|
||||
if (code.indexOf(TENCENT_US) === 0) {
|
||||
return this.USTransform(code);
|
||||
}
|
||||
|
||||
throw new Error("请检查股票代码是否正确");
|
||||
}
|
||||
|
||||
/**
|
||||
* 交易所股票组代码转换
|
||||
* @param codes 股票代码
|
||||
* 交易所股票代码组转换统一代码组
|
||||
* @param codes 股票代码组
|
||||
*/
|
||||
public transforms(codes: string[]): string[] {
|
||||
return super.transforms(codes);
|
||||
}
|
||||
|
||||
/**
|
||||
* 深交所股票代码转换
|
||||
* 深交所股票代码转换统一代码
|
||||
* @param code 股票代码
|
||||
*/
|
||||
public SZTransform(code: string): string {
|
||||
if (!code.includes(SZ)) {
|
||||
if (!code.includes(TENCENT_SZ)) {
|
||||
throw new Error("请检查股票代码是否正确");
|
||||
}
|
||||
|
||||
return "sz" + code.replace(SZ, "");
|
||||
return COMMON_SZ + code.replace(TENCENT_SZ, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 上交所股票代码转换
|
||||
* 上交所股票代码转换统一代码
|
||||
* @param code 股票代码
|
||||
*/
|
||||
public SHTransform(code: string): string {
|
||||
if (!code.includes(SH)) {
|
||||
if (!code.includes(TENCENT_SH)) {
|
||||
throw new Error("请检查股票代码是否正确");
|
||||
}
|
||||
|
||||
return "sh" + code.replace(SH, "");
|
||||
return COMMON_SH + code.replace(TENCENT_SH, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 港交所股票代码转换
|
||||
* 港交所股票代码转换统一代码
|
||||
* @param code 股票代码
|
||||
*/
|
||||
public HKTransform(code: string): string {
|
||||
if (!code.includes(HK)) {
|
||||
if (!code.includes(TENCENT_HK)) {
|
||||
throw new Error("请检查股票代码是否正确");
|
||||
}
|
||||
|
||||
return "hk" + code.replace(HK, "");
|
||||
return COMMON_HK + code.replace(TENCENT_HK, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 美交所股票代码转换
|
||||
* 美交所股票代码转换统一代码
|
||||
* @param code 股票代码
|
||||
*/
|
||||
public USTransform(code: string): string {
|
||||
if (!code.includes(US)) {
|
||||
if (!code.includes(TENCENT_US)) {
|
||||
throw new Error("请检查股票代码是否正确");
|
||||
}
|
||||
|
||||
return "us" + code.replace(US, "");
|
||||
return COMMON_US + code.replace(TENCENT_US, "");
|
||||
}
|
||||
}
|
||||
|
||||
|
84
src/stocks/tencent/transforms/common-code.ts
Normal file
84
src/stocks/tencent/transforms/common-code.ts
Normal file
@ -0,0 +1,84 @@
|
||||
// Stocks
|
||||
import BaseCommonCodeTransform from "@stocks/base/transforms/common-code";
|
||||
|
||||
// Utils
|
||||
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";
|
||||
|
||||
/**
|
||||
* 【腾讯】统一代码转换股票代码
|
||||
*/
|
||||
class TencentCommonCodeTransform extends BaseCommonCodeTransform {
|
||||
/**
|
||||
* 构造函数
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* 交易所统一代码转换股票代码
|
||||
* @param code 统一代码
|
||||
*/
|
||||
public transform(code: string): string {
|
||||
return super.transform(code);
|
||||
}
|
||||
|
||||
/**
|
||||
* 交易所统一代码组转换股票代码组
|
||||
* @param codes 统一代码组
|
||||
*/
|
||||
public transforms(codes: string[]): string[] {
|
||||
return super.transforms(codes);
|
||||
}
|
||||
|
||||
/**
|
||||
* 深交所统一代码转换股票代码
|
||||
* @param code 统一代码
|
||||
*/
|
||||
public SZTransform(code: string): string {
|
||||
if (!code.includes(COMMON_SZ)) {
|
||||
throw new Error("请检查统一代码是否正确");
|
||||
}
|
||||
|
||||
return TENCENT_SZ + code.replace(COMMON_SZ, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 上交所统一代码转换股票代码
|
||||
* @param code 统一代码
|
||||
*/
|
||||
public SHTransform(code: string): string {
|
||||
if (!code.includes(COMMON_SH)) {
|
||||
throw new Error("请检查统一代码是否正确");
|
||||
}
|
||||
|
||||
return TENCENT_SH + code.replace(COMMON_SH, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 港交所统一代码转换股票代码
|
||||
* @param code 统一代码
|
||||
*/
|
||||
public HKTransform(code: string): string {
|
||||
if (!code.includes(COMMON_HK)) {
|
||||
throw new Error("请检查统一代码是否正确");
|
||||
}
|
||||
|
||||
return TENCENT_HK + code.replace(COMMON_HK, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 美交所统一代码转换股票代码
|
||||
* @param code 统一代码
|
||||
*/
|
||||
public USTransform(code: string): string {
|
||||
if (!code.includes(COMMON_US)) {
|
||||
throw new Error("请检查统一代码是否正确");
|
||||
}
|
||||
|
||||
return TENCENT_US + code.replace(COMMON_US, "");
|
||||
}
|
||||
}
|
||||
|
||||
export default TencentCommonCodeTransform;
|
4
src/stocks/tencent/utils/constant.ts
Normal file
4
src/stocks/tencent/utils/constant.ts
Normal file
@ -0,0 +1,4 @@
|
||||
export const TENCENT_SZ = "sz"; // 深交所相关常量
|
||||
export const TENCENT_SH = "sh"; // 上交所相关常量
|
||||
export const TENCENT_HK = "hk"; // 港交所相关常量
|
||||
export const TENCENT_US = "us"; // 美交所相关常量
|
@ -8,7 +8,7 @@ describe("【腾讯】股票代码接口", () => {
|
||||
.toMatchObject({ code: "SH510500", name: "500ETF" });
|
||||
});
|
||||
|
||||
it("需要获取的股票组代码", async () => {
|
||||
it("需要获取的股票代码组", async () => {
|
||||
await expect((new Tencent()).getStocks(["SH510500"]))
|
||||
.resolves
|
||||
.toMatchObject([{ code: "SH510500", name: "500ETF" }]);
|
||||
|
@ -1,70 +1,70 @@
|
||||
// Stock
|
||||
const TencentApiCodeTransform = require("stocks/tencent/transforms/api-code").default;
|
||||
|
||||
describe("【腾讯】股票代码转换测试", () => {
|
||||
it("深交所股票代码转换", async () => {
|
||||
expect(() => (new TencentApiCodeTransform()).SZTransform("000000"))
|
||||
describe("【腾讯】股票代码转换统一代码", () => {
|
||||
it("深交所股票代码转换统一代码", async () => {
|
||||
expect(() => (new TencentApiCodeTransform()).SZTransform("STOCKAPI"))
|
||||
.toThrow(new Error("请检查股票代码是否正确"));
|
||||
|
||||
expect((new TencentApiCodeTransform()).SZTransform("SZ000000"))
|
||||
.toBe("sz000000");
|
||||
expect((new TencentApiCodeTransform()).SZTransform("sz000000"))
|
||||
.toBe("SZ000000");
|
||||
});
|
||||
|
||||
it("上交所股票代码转换", async () => {
|
||||
expect(() => (new TencentApiCodeTransform()).SHTransform("000000"))
|
||||
it("上交所股票代码转换统一代码", async () => {
|
||||
expect(() => (new TencentApiCodeTransform()).SHTransform("STOCKAPI"))
|
||||
.toThrow(new Error("请检查股票代码是否正确"));
|
||||
|
||||
expect((new TencentApiCodeTransform()).SHTransform("SH000000"))
|
||||
.toBe("sh000000");
|
||||
expect((new TencentApiCodeTransform()).SHTransform("sh000000"))
|
||||
.toBe("SH000000");
|
||||
});
|
||||
|
||||
it("港交所股票代码转换", async () => {
|
||||
expect(() => (new TencentApiCodeTransform()).HKTransform("000000"))
|
||||
it("港交所股票代码转换统一代码", async () => {
|
||||
expect(() => (new TencentApiCodeTransform()).HKTransform("STOCKAPI"))
|
||||
.toThrow(new Error("请检查股票代码是否正确"));
|
||||
|
||||
expect((new TencentApiCodeTransform()).HKTransform("HK000000"))
|
||||
.toBe("hk000000");
|
||||
expect((new TencentApiCodeTransform()).HKTransform("hk000000"))
|
||||
.toBe("HK000000");
|
||||
});
|
||||
|
||||
it("美交所股票代码转换", async () => {
|
||||
expect(() => (new TencentApiCodeTransform()).USTransform("000000"))
|
||||
it("美交所股票代码转换统一代码", async () => {
|
||||
expect(() => (new TencentApiCodeTransform()).USTransform("STOCKAPI"))
|
||||
.toThrow(new Error("请检查股票代码是否正确"));
|
||||
|
||||
expect((new TencentApiCodeTransform()).USTransform("US000000"))
|
||||
.toBe("us000000");
|
||||
expect((new TencentApiCodeTransform()).USTransform("us000000"))
|
||||
.toBe("US000000");
|
||||
});
|
||||
|
||||
it("交易所股票代码转换", async () => {
|
||||
expect((new TencentApiCodeTransform()).transform("SZ000000"))
|
||||
.toBe("sz000000");
|
||||
it("交易所股票代码转换统一代码", async () => {
|
||||
expect((new TencentApiCodeTransform()).transform("sz000000"))
|
||||
.toBe("SZ000000");
|
||||
|
||||
expect((new TencentApiCodeTransform()).transform("SH000000"))
|
||||
.toBe("sh000000");
|
||||
expect((new TencentApiCodeTransform()).transform("sh000000"))
|
||||
.toBe("SH000000");
|
||||
|
||||
expect((new TencentApiCodeTransform()).transform("HK000000"))
|
||||
.toBe("hk000000");
|
||||
expect((new TencentApiCodeTransform()).transform("hk000000"))
|
||||
.toBe("HK000000");
|
||||
|
||||
expect((new TencentApiCodeTransform()).transform("US000000"))
|
||||
.toBe("us000000");
|
||||
expect((new TencentApiCodeTransform()).transform("us000000"))
|
||||
.toBe("US000000");
|
||||
|
||||
expect(() => (new TencentApiCodeTransform()).transform("000000"))
|
||||
expect(() => (new TencentApiCodeTransform()).transform("STOCKAPI"))
|
||||
.toThrow(new Error("请检查股票代码是否正确"));
|
||||
});
|
||||
|
||||
it("交易所股票组代码转换", async () => {
|
||||
expect((new TencentApiCodeTransform()).transforms(["SZ000000"]))
|
||||
.toStrictEqual(["sz000000"]);
|
||||
it("交易所股票代码组转换统一代码组", async () => {
|
||||
expect((new TencentApiCodeTransform()).transforms(["sz000000"]))
|
||||
.toStrictEqual(["SZ000000"]);
|
||||
|
||||
expect((new TencentApiCodeTransform()).transforms(["SH000000"]))
|
||||
.toStrictEqual(["sh000000"]);
|
||||
expect((new TencentApiCodeTransform()).transforms(["sh000000"]))
|
||||
.toStrictEqual(["SH000000"]);
|
||||
|
||||
expect((new TencentApiCodeTransform()).transforms(["HK000000"]))
|
||||
.toStrictEqual(["hk000000"]);
|
||||
expect((new TencentApiCodeTransform()).transforms(["hk000000"]))
|
||||
.toStrictEqual(["HK000000"]);
|
||||
|
||||
expect((new TencentApiCodeTransform()).transforms(["US000000"]))
|
||||
.toStrictEqual(["us000000"]);
|
||||
expect((new TencentApiCodeTransform()).transforms(["us000000"]))
|
||||
.toStrictEqual(["US000000"]);
|
||||
|
||||
expect(() => (new TencentApiCodeTransform()).transforms(["000000"]))
|
||||
expect(() => (new TencentApiCodeTransform()).transforms(["STOCKAPI"]))
|
||||
.toThrow(new Error("请检查股票代码是否正确"));
|
||||
});
|
||||
});
|
||||
|
70
test/stocks/tencent/transforms/common-code.test.ts
Normal file
70
test/stocks/tencent/transforms/common-code.test.ts
Normal file
@ -0,0 +1,70 @@
|
||||
// Stock
|
||||
const TencentCommonCodeTransform = require("stocks/tencent/transforms/common-code").default;
|
||||
|
||||
describe("【腾讯】统一代码转换股票代码", () => {
|
||||
it("深交所统一代码转换股票代码", async () => {
|
||||
expect(() => (new TencentCommonCodeTransform()).SZTransform("000000"))
|
||||
.toThrow(new Error("请检查统一代码是否正确"));
|
||||
|
||||
expect((new TencentCommonCodeTransform()).SZTransform("SZ000000"))
|
||||
.toBe("sz000000");
|
||||
});
|
||||
|
||||
it("上交所统一代码转换股票代码", async () => {
|
||||
expect(() => (new TencentCommonCodeTransform()).SHTransform("000000"))
|
||||
.toThrow(new Error("请检查统一代码是否正确"));
|
||||
|
||||
expect((new TencentCommonCodeTransform()).SHTransform("SH000000"))
|
||||
.toBe("sh000000");
|
||||
});
|
||||
|
||||
it("港交所统一代码转换股票代码", async () => {
|
||||
expect(() => (new TencentCommonCodeTransform()).HKTransform("000000"))
|
||||
.toThrow(new Error("请检查统一代码是否正确"));
|
||||
|
||||
expect((new TencentCommonCodeTransform()).HKTransform("HK000000"))
|
||||
.toBe("hk000000");
|
||||
});
|
||||
|
||||
it("美交所统一代码转换股票代码", async () => {
|
||||
expect(() => (new TencentCommonCodeTransform()).USTransform("000000"))
|
||||
.toThrow(new Error("请检查统一代码是否正确"));
|
||||
|
||||
expect((new TencentCommonCodeTransform()).USTransform("US000000"))
|
||||
.toBe("us000000");
|
||||
});
|
||||
|
||||
it("交易所统一代码转换股票代码", async () => {
|
||||
expect((new TencentCommonCodeTransform()).transform("SZ000000"))
|
||||
.toBe("sz000000");
|
||||
|
||||
expect((new TencentCommonCodeTransform()).transform("SH000000"))
|
||||
.toBe("sh000000");
|
||||
|
||||
expect((new TencentCommonCodeTransform()).transform("HK000000"))
|
||||
.toBe("hk000000");
|
||||
|
||||
expect((new TencentCommonCodeTransform()).transform("US000000"))
|
||||
.toBe("us000000");
|
||||
|
||||
expect(() => (new TencentCommonCodeTransform()).transform("000000"))
|
||||
.toThrow(new Error("请检查统一代码是否正确"));
|
||||
});
|
||||
|
||||
it("交易所统一代码组转换股票代码组", async () => {
|
||||
expect((new TencentCommonCodeTransform()).transforms(["SZ000000"]))
|
||||
.toStrictEqual(["sz000000"]);
|
||||
|
||||
expect((new TencentCommonCodeTransform()).transforms(["SH000000"]))
|
||||
.toStrictEqual(["sh000000"]);
|
||||
|
||||
expect((new TencentCommonCodeTransform()).transforms(["HK000000"]))
|
||||
.toStrictEqual(["hk000000"]);
|
||||
|
||||
expect((new TencentCommonCodeTransform()).transforms(["US000000"]))
|
||||
.toStrictEqual(["us000000"]);
|
||||
|
||||
expect(() => (new TencentCommonCodeTransform()).transforms(["000000"]))
|
||||
.toThrow(new Error("请检查统一代码是否正确"));
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user