diff --git a/src/stocks/sina/index.ts b/src/stocks/sina/index.ts index 97eebc1..81d2919 100644 --- a/src/stocks/sina/index.ts +++ b/src/stocks/sina/index.ts @@ -1,7 +1,7 @@ // Stocks import Base from "@stocks/base"; import SinaStockTransform from "@stocks/sina/transforms/stock"; -import SinaApiCodeTransform from "@stocks/sina/transforms/api-code"; +import SinaCommonCodeTransform from "@stocks/sina/transforms/common-code"; // Utils import fetch from "@utils/fetch"; @@ -26,7 +26,7 @@ class Sina extends Base { * @param code 需要获取的股票代码 */ async getStock(code: string): Promise { - const transform = (new SinaApiCodeTransform).transform(code); + const transform = (new SinaCommonCodeTransform).transform(code); // 数据获取 const url = `https://hq.sinajs.cn/list=${transform}`; @@ -49,7 +49,7 @@ class Sina extends Base { * @param codes 需要获取的股票组代码 */ async getStocks(codes: string[]): Promise { - const transforms = (new SinaApiCodeTransform).transforms(codes); + const transforms = (new SinaCommonCodeTransform).transforms(codes); // 数据获取 const url = `https://hq.sinajs.cn/list=${transforms.join(',')}`; diff --git a/src/stocks/sina/transforms/api-code.ts b/src/stocks/sina/transforms/api-code.ts index 75cc31c..6de5832 100644 --- a/src/stocks/sina/transforms/api-code.ts +++ b/src/stocks/sina/transforms/api-code.ts @@ -1,13 +1,14 @@ // Stocks -import BaseApiCodeTransform from "@stocks/base/transforms/api-code"; +import BaseCommonCodeTransform from "@stocks/base/transforms/common-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 { SINA_SZ, SINA_SH, SINA_HK, SINA_US } from "@stocks/sina/utils/constant"; /** - * 新浪股票代码转换 + * 【新浪】股票代码转换统一代码 */ -class SinaApiCodeTransform extends BaseApiCodeTransform { +class SinaCommonCodeTransform extends BaseCommonCodeTransform { /** * 构造函数 */ @@ -16,15 +17,31 @@ class SinaApiCodeTransform extends BaseApiCodeTransform { } /** - * 交易所股票代码转换 + * 交易所股票代码转换统一代码 * @param code 股票代码 */ public transform(code: string): string { - return super.transform(code); + if (code.indexOf(SINA_SZ) === 0) { + return this.SZTransform(code); + } + + if (code.indexOf(SINA_SH) === 0) { + return this.SHTransform(code); + } + + if (code.indexOf(SINA_HK) === 0) { + return this.HKTransform(code); + } + + if (code.indexOf(SINA_US) === 0) { + return this.USTransform(code); + } + + throw new Error("请检查股票代码是否正确"); } /** - * 交易所股票组代码转换 + * 交易所股票代码组转换统一代码组 * @param codes 股票代码 */ public transforms(codes: string[]): string[] { @@ -32,52 +49,52 @@ class SinaApiCodeTransform extends BaseApiCodeTransform { } /** - * 深交所股票代码转换 + * 深交所股票代码转换统一代码 * @param code 股票代码 */ public SZTransform(code: string): string { - if (!code.includes(SZ)) { + if (!code.includes(SINA_SZ)) { throw new Error("请检查股票代码是否正确"); } - return "sz" + code.replace(SZ, ""); + return COMMON_SZ + code.replace(SINA_SZ, ""); } /** - * 上交所股票代码转换 + * 上交所股票代码转换统一代码 * @param code 股票代码 */ public SHTransform(code: string): string { - if (!code.includes(SH)) { + if (!code.includes(SINA_SH)) { throw new Error("请检查股票代码是否正确"); } - return "sh" + code.replace(SH, ""); + return COMMON_SH + code.replace(SINA_SH, ""); } /** - * 港交所股票代码转换 + * 港交所股票代码转换统一代码 * @param code 股票代码 */ public HKTransform(code: string): string { - if (!code.includes(HK)) { + if (!code.includes(SINA_HK)) { throw new Error("请检查股票代码是否正确"); } - return "hk" + code.replace(HK, ""); + return COMMON_HK + code.replace(SINA_HK, ""); } /** - * 美交所股票代码转换 + * 美交所股票代码转换统一代码 * @param code 股票代码 */ public USTransform(code: string): string { - if (!code.includes(US)) { + if (!code.includes(SINA_US)) { throw new Error("请检查股票代码是否正确"); } - return "gb_" + code.replace(US, "").toLowerCase(); + return COMMON_US + code.replace(SINA_US, "").toLowerCase(); } } -export default SinaApiCodeTransform; +export default SinaCommonCodeTransform; diff --git a/src/stocks/sina/transforms/common-code.ts b/src/stocks/sina/transforms/common-code.ts new file mode 100644 index 0000000..f1e9696 --- /dev/null +++ b/src/stocks/sina/transforms/common-code.ts @@ -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 { SINA_SZ, SINA_SH, SINA_HK, SINA_US } from "@stocks/sina/utils/constant"; + +/** + * 【新浪】统一代码转换股票代码 + */ +class SinaCommonCodeTransform 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 SINA_SZ + code.replace(COMMON_SZ, ""); + } + + /** + * 上交所统一代码转换股票代码 + * @param code 统一代码 + */ + public SHTransform(code: string): string { + if (!code.includes(COMMON_SH)) { + throw new Error("请检查统一代码是否正确"); + } + + return SINA_SH + code.replace(COMMON_SH, ""); + } + + /** + * 港交所统一代码转换股票代码 + * @param code 统一代码 + */ + public HKTransform(code: string): string { + if (!code.includes(COMMON_HK)) { + throw new Error("请检查统一代码是否正确"); + } + + return SINA_HK + code.replace(COMMON_HK, ""); + } + + /** + * 美交所统一代码转换股票代码 + * @param code 统一代码 + */ + public USTransform(code: string): string { + if (!code.includes(COMMON_US)) { + throw new Error("请检查统一代码是否正确"); + } + + return SINA_US + code.replace(COMMON_US, "").toLowerCase(); + } +} + +export default SinaCommonCodeTransform; diff --git a/src/stocks/sina/transforms/stock.ts b/src/stocks/sina/transforms/stock.ts index ca661f5..a60d85e 100644 --- a/src/stocks/sina/transforms/stock.ts +++ b/src/stocks/sina/transforms/stock.ts @@ -2,7 +2,7 @@ import BaseStockTransform from "@stocks/base/transforms/stock"; // Utils -import { SZ, HK, US, SH } from "@utils/constant"; +import { COMMON_SH, COMMON_SZ, COMMON_HK, COMMON_US } from "@stocks/base/utils/constant"; // Types import Stock from "types/utils/stock"; @@ -30,13 +30,13 @@ class SinaStockTransform extends BaseStockTransform { */ getName(): string { switch (this.code.slice(0, 2)) { - case SH: + case COMMON_SH: return String(this.params[0]); - case SZ: + case COMMON_SZ: return String(this.params[0]); - case HK: + case COMMON_HK: return String(this.params[1]); - case US: + case COMMON_US: return String(this.params[0]); default: throw new Error('不支持当前交易所'); @@ -48,13 +48,13 @@ class SinaStockTransform extends BaseStockTransform { */ getNow(): number { switch (this.code.slice(0, 2)) { - case SH: + case COMMON_SH: return Number(this.params[3]); - case SZ: + case COMMON_SZ: return Number(this.params[3]); - case HK: + case COMMON_HK: return Number(this.params[6]); - case US: + case COMMON_US: return Number(this.params[1]); default: throw new Error('不支持当前交易所'); @@ -66,13 +66,13 @@ class SinaStockTransform extends BaseStockTransform { */ getLow(): number { switch (this.code.slice(0, 2)) { - case SH: + case COMMON_SH: return Number(this.params[5]); - case SZ: + case COMMON_SZ: return Number(this.params[5]); - case HK: + case COMMON_HK: return Number(this.params[5]); - case US: + case COMMON_US: return Number(this.params[7]); default: throw new Error('不支持当前交易所'); @@ -84,13 +84,13 @@ class SinaStockTransform extends BaseStockTransform { */ getHigh(): number { switch (this.code.slice(0, 2)) { - case SH: + case COMMON_SH: return Number(this.params[4]); - case SZ: + case COMMON_SZ: return Number(this.params[4]); - case HK: + case COMMON_HK: return Number(this.params[4]); - case US: + case COMMON_US: return Number(this.params[6]); default: throw new Error('不支持当前交易所'); @@ -102,13 +102,13 @@ class SinaStockTransform extends BaseStockTransform { */ getYesterday(): number { switch (this.code.slice(0, 2)) { - case SH: + case COMMON_SH: return Number(this.params[2]); - case SZ: + case COMMON_SZ: return Number(this.params[2]); - case HK: + case COMMON_HK: return Number(this.params[3]); - case US: + case COMMON_US: return Number(this.params[26]); default: throw new Error('不支持当前交易所'); diff --git a/src/stocks/sina/utils/constant.ts b/src/stocks/sina/utils/constant.ts new file mode 100644 index 0000000..0eb2769 --- /dev/null +++ b/src/stocks/sina/utils/constant.ts @@ -0,0 +1,4 @@ +export const SINA_SZ = "sz"; // 深交所相关常量 +export const SINA_SH = "sh"; // 上交所相关常量 +export const SINA_HK = "hk"; // 港交所相关常量 +export const SINA_US = "gb_"; // 美交所相关常量 diff --git a/test/stocks/sina/transforms/api-code.test.ts b/test/stocks/sina/transforms/api-code.test.ts index 24dc30f..e818d8e 100644 --- a/test/stocks/sina/transforms/api-code.test.ts +++ b/test/stocks/sina/transforms/api-code.test.ts @@ -1,70 +1,70 @@ // Stock const SinaApiCodeTransform = require("stocks/sina/transforms/api-code").default; -describe("【新浪】股票代码转换测试", () => { - it("深交所股票代码转换", async () => { - expect(() => (new SinaApiCodeTransform()).SZTransform("000000")) +describe("【新浪】股票代码转换统一代码", () => { + it("深交所股票代码转换统一代码", async () => { + expect(() => (new SinaApiCodeTransform()).SZTransform("STOCKAPI")) .toThrow(new Error("请检查股票代码是否正确")); - expect((new SinaApiCodeTransform()).SZTransform("SZ000000")) - .toBe("sz000000"); + expect((new SinaApiCodeTransform()).SZTransform("sz000000")) + .toBe("SZ000000"); }); - it("上交所股票代码转换", async () => { - expect(() => (new SinaApiCodeTransform()).SHTransform("000000")) + it("上交所股票代码转换统一代码", async () => { + expect(() => (new SinaApiCodeTransform()).SHTransform("STOCKAPI")) .toThrow(new Error("请检查股票代码是否正确")); - expect((new SinaApiCodeTransform()).SHTransform("SH000000")) - .toBe("sh000000"); + expect((new SinaApiCodeTransform()).SHTransform("sh000000")) + .toBe("SH000000"); }); - it("港交所股票代码转换", async () => { - expect(() => (new SinaApiCodeTransform()).HKTransform("000000")) + it("港交所股票代码转换统一代码", async () => { + expect(() => (new SinaApiCodeTransform()).HKTransform("STOCKAPI")) .toThrow(new Error("请检查股票代码是否正确")); - expect((new SinaApiCodeTransform()).HKTransform("HK000000")) - .toBe("hk000000"); + expect((new SinaApiCodeTransform()).HKTransform("hk000000")) + .toBe("HK000000"); }); - it("美交所股票代码转换", async () => { - expect(() => (new SinaApiCodeTransform()).USTransform("000000")) + it("美交所股票代码转换统一代码", async () => { + expect(() => (new SinaApiCodeTransform()).USTransform("STOCKAPI")) .toThrow(new Error("请检查股票代码是否正确")); - expect((new SinaApiCodeTransform()).USTransform("US000000")) - .toBe("gb_000000"); + expect((new SinaApiCodeTransform()).USTransform("gb_000000")) + .toBe("US000000"); }); - it("交易所股票代码转换", async () => { - expect((new SinaApiCodeTransform()).transform("SZ000000")) - .toBe("sz000000"); + it("交易所股票代码转换统一代码", async () => { + expect((new SinaApiCodeTransform()).transform("sz000000")) + .toBe("SZ000000"); - expect((new SinaApiCodeTransform()).transform("SH000000")) - .toBe("sh000000"); + expect((new SinaApiCodeTransform()).transform("sh000000")) + .toBe("SH000000"); - expect((new SinaApiCodeTransform()).transform("HK000000")) - .toBe("hk000000"); + expect((new SinaApiCodeTransform()).transform("hk000000")) + .toBe("HK000000"); - expect((new SinaApiCodeTransform()).transform("US000000")) - .toBe("gb_000000"); + expect((new SinaApiCodeTransform()).transform("gb_000000")) + .toBe("US000000"); - expect(() => (new SinaApiCodeTransform()).transform("000000")) + expect(() => (new SinaApiCodeTransform()).transform("STOCKAPI")) .toThrow(new Error("请检查股票代码是否正确")); }); - it("交易所股票组代码转换", async () => { - expect((new SinaApiCodeTransform()).transforms(["SZ000000"])) - .toStrictEqual(["sz000000"]); + it("交易所股票代码转换统一代码", async () => { + expect((new SinaApiCodeTransform()).transforms(["sz000000"])) + .toStrictEqual(["SZ000000"]); - expect((new SinaApiCodeTransform()).transforms(["SH000000"])) - .toStrictEqual(["sh000000"]); + expect((new SinaApiCodeTransform()).transforms(["sh000000"])) + .toStrictEqual(["SH000000"]); - expect((new SinaApiCodeTransform()).transforms(["HK000000"])) - .toStrictEqual(["hk000000"]); + expect((new SinaApiCodeTransform()).transforms(["hk000000"])) + .toStrictEqual(["HK000000"]); - expect((new SinaApiCodeTransform()).transforms(["US000000"])) - .toStrictEqual(["gb_000000"]); + expect((new SinaApiCodeTransform()).transforms(["gb_000000"])) + .toStrictEqual(["US000000"]); - expect(() => (new SinaApiCodeTransform()).transforms(["000000"])) + expect(() => (new SinaApiCodeTransform()).transforms(["STOCKAPI"])) .toThrow(new Error("请检查股票代码是否正确")); }); }); diff --git a/test/stocks/sina/transforms/common-code.test.ts b/test/stocks/sina/transforms/common-code.test.ts new file mode 100644 index 0000000..f8df222 --- /dev/null +++ b/test/stocks/sina/transforms/common-code.test.ts @@ -0,0 +1,70 @@ +// Stock +const SinaCommonCodeTransform = require("stocks/sina/transforms/common-code").default; + +describe("【新浪】统一代码转换股票代码", () => { + it("深交所统一代码转换股票代码", async () => { + expect(() => (new SinaCommonCodeTransform()).SZTransform("000000")) + .toThrow(new Error("请检查统一代码是否正确")); + + expect((new SinaCommonCodeTransform()).SZTransform("SZ000000")) + .toBe("sz000000"); + }); + + it("上交所统一代码转换股票代码", async () => { + expect(() => (new SinaCommonCodeTransform()).SHTransform("000000")) + .toThrow(new Error("请检查统一代码是否正确")); + + expect((new SinaCommonCodeTransform()).SHTransform("SH000000")) + .toBe("sh000000"); + }); + + it("港交所统一代码转换股票代码", async () => { + expect(() => (new SinaCommonCodeTransform()).HKTransform("000000")) + .toThrow(new Error("请检查统一代码是否正确")); + + expect((new SinaCommonCodeTransform()).HKTransform("HK000000")) + .toBe("hk000000"); + }); + + it("美交所统一代码转换股票代码", async () => { + expect(() => (new SinaCommonCodeTransform()).USTransform("000000")) + .toThrow(new Error("请检查统一代码是否正确")); + + expect((new SinaCommonCodeTransform()).USTransform("US000000")) + .toBe("gb_000000"); + }); + + it("交易所统一代码转换股票代码", async () => { + expect((new SinaCommonCodeTransform()).transform("SZ000000")) + .toBe("sz000000"); + + expect((new SinaCommonCodeTransform()).transform("SH000000")) + .toBe("sh000000"); + + expect((new SinaCommonCodeTransform()).transform("HK000000")) + .toBe("hk000000"); + + expect((new SinaCommonCodeTransform()).transform("US000000")) + .toBe("gb_000000"); + + expect(() => (new SinaCommonCodeTransform()).transform("000000")) + .toThrow(new Error("请检查统一代码是否正确")); + }); + + it("交易所统一代码组转换股票代码组", async () => { + expect((new SinaCommonCodeTransform()).transforms(["SZ000000"])) + .toStrictEqual(["sz000000"]); + + expect((new SinaCommonCodeTransform()).transforms(["SH000000"])) + .toStrictEqual(["sh000000"]); + + expect((new SinaCommonCodeTransform()).transforms(["HK000000"])) + .toStrictEqual(["hk000000"]); + + expect((new SinaCommonCodeTransform()).transforms(["US000000"])) + .toStrictEqual(["gb_000000"]); + + expect(() => (new SinaCommonCodeTransform()).transforms(["000000"])) + .toThrow(new Error("请检查统一代码是否正确")); + }); +});