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
d3a68341f2
commit
5410bbcde3
@ -5,7 +5,7 @@ import XueqiuCommonCodeTransform from "@stocks/xueqiu/transforms/common-code";
|
||||
|
||||
// Utils
|
||||
import fetch from "@utils/fetch";
|
||||
import { DEFAULT_STOCK } from "@stocks/base/utils/constant";
|
||||
import { DEFAULT_STOCK } from "@utils/constant";
|
||||
|
||||
// Types
|
||||
import Stock from "types/utils/stock";
|
||||
|
@ -2,6 +2,7 @@
|
||||
import BaseApiCodeTransform from "@stocks/base/transforms/api-code";
|
||||
|
||||
// Utils
|
||||
import { ERROR_API_CODE } from "@utils/constant";
|
||||
import { COMMON_SH, COMMON_SZ, COMMON_HK, COMMON_US } from "@stocks/base/utils/constant";
|
||||
import { XUEQIU_SZ, XUEQIU_SH, XUEQIU_HK, XUEQIU_US } from "@stocks/xueqiu/utils/constant";
|
||||
|
||||
@ -43,7 +44,7 @@ class XueqiuApiCodeTransform extends BaseApiCodeTransform {
|
||||
*/
|
||||
public SZTransform(code: string): string {
|
||||
if (!code.includes(XUEQIU_SZ)) {
|
||||
throw new Error("请检查股票代码是否正确");
|
||||
throw new Error(ERROR_API_CODE);
|
||||
}
|
||||
|
||||
return COMMON_SZ + code.replace(XUEQIU_SZ, "");
|
||||
@ -55,7 +56,7 @@ class XueqiuApiCodeTransform extends BaseApiCodeTransform {
|
||||
*/
|
||||
public SHTransform(code: string): string {
|
||||
if (!code.includes(XUEQIU_SH)) {
|
||||
throw new Error("请检查股票代码是否正确");
|
||||
throw new Error(ERROR_API_CODE);
|
||||
}
|
||||
|
||||
return COMMON_SH + code.replace(XUEQIU_SH, "");
|
||||
@ -67,7 +68,7 @@ class XueqiuApiCodeTransform extends BaseApiCodeTransform {
|
||||
*/
|
||||
public HKTransform(code: string): string {
|
||||
if (!code.includes(XUEQIU_HK)) {
|
||||
throw new Error("请检查股票代码是否正确");
|
||||
throw new Error(ERROR_API_CODE);
|
||||
}
|
||||
|
||||
return COMMON_HK + code.replace(XUEQIU_HK, "");
|
||||
@ -79,7 +80,7 @@ class XueqiuApiCodeTransform extends BaseApiCodeTransform {
|
||||
*/
|
||||
public USTransform(code: string): string {
|
||||
if (code.includes(XUEQIU_SZ) || code.includes(XUEQIU_SH) || code.includes(XUEQIU_HK)) {
|
||||
throw new Error("请检查股票代码是否正确");
|
||||
throw new Error(ERROR_API_CODE);
|
||||
}
|
||||
|
||||
return COMMON_US + code.replace(XUEQIU_US, "");
|
||||
|
@ -2,6 +2,7 @@
|
||||
import BaseCommonCodeTransform from "@stocks/base/transforms/common-code";
|
||||
|
||||
// Utils
|
||||
import { ERROR_COMMON_CODE } from "@utils/constant";
|
||||
import { COMMON_SH, COMMON_SZ, COMMON_HK, COMMON_US } from "@stocks/base/utils/constant";
|
||||
import { XUEQIU_SZ, XUEQIU_SH, XUEQIU_HK, XUEQIU_US } from "@stocks/xueqiu/utils/constant";
|
||||
|
||||
@ -31,7 +32,7 @@ class XueqiuCommonCodeTransform extends BaseCommonCodeTransform {
|
||||
*/
|
||||
public SZTransform(code: string): string {
|
||||
if (!code.includes(COMMON_SZ)) {
|
||||
throw new Error("请检查统一代码是否正确");
|
||||
throw new Error(ERROR_COMMON_CODE);
|
||||
}
|
||||
|
||||
return XUEQIU_SZ + code.replace(COMMON_SZ, "");
|
||||
@ -43,7 +44,7 @@ class XueqiuCommonCodeTransform extends BaseCommonCodeTransform {
|
||||
*/
|
||||
public SHTransform(code: string): string {
|
||||
if (!code.includes(COMMON_SH)) {
|
||||
throw new Error("请检查统一代码是否正确");
|
||||
throw new Error(ERROR_COMMON_CODE);
|
||||
}
|
||||
|
||||
return XUEQIU_SH + code.replace(COMMON_SH, "");
|
||||
@ -55,7 +56,7 @@ class XueqiuCommonCodeTransform extends BaseCommonCodeTransform {
|
||||
*/
|
||||
public HKTransform(code: string): string {
|
||||
if (!code.includes(COMMON_HK)) {
|
||||
throw new Error("请检查统一代码是否正确");
|
||||
throw new Error(ERROR_COMMON_CODE);
|
||||
}
|
||||
|
||||
return XUEQIU_HK + code.replace(COMMON_HK, "");
|
||||
@ -67,7 +68,7 @@ class XueqiuCommonCodeTransform extends BaseCommonCodeTransform {
|
||||
*/
|
||||
public USTransform(code: string): string {
|
||||
if (!code.includes(COMMON_US)) {
|
||||
throw new Error("请检查统一代码是否正确");
|
||||
throw new Error(ERROR_COMMON_CODE);
|
||||
}
|
||||
|
||||
return XUEQIU_US + code.replace(COMMON_US, "");
|
||||
|
@ -1,6 +1,9 @@
|
||||
// Stocks
|
||||
import BaseStockTransform from "@stocks/base/transforms/stock";
|
||||
|
||||
// Utils
|
||||
import { DEFAULT_STRING, DEFAULT_NUMBER } from "@utils/constant";
|
||||
|
||||
// Types
|
||||
import Stock from "types/utils/stock";
|
||||
import Dictionary from "types/utils/dictionary";
|
||||
@ -27,35 +30,35 @@ class XueqiuStockTransform extends BaseStockTransform {
|
||||
* 获取名称
|
||||
*/
|
||||
getName(): string {
|
||||
return String(this.params.name || this.code);
|
||||
return String(this.params.name || DEFAULT_STRING);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取现价
|
||||
*/
|
||||
getNow(): number {
|
||||
return Number(this.params.current);
|
||||
return Number(this.params.current || DEFAULT_NUMBER);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最低价
|
||||
*/
|
||||
getLow(): number {
|
||||
return Number(this.params.low);
|
||||
return Number(this.params.low || DEFAULT_NUMBER);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最高价
|
||||
*/
|
||||
getHigh(): number {
|
||||
return Number(this.params.high);
|
||||
return Number(this.params.high || DEFAULT_NUMBER);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取昨日收盘价
|
||||
*/
|
||||
getYesterday(): number {
|
||||
return Number(this.params.last_close);
|
||||
return Number(this.params.last_close || DEFAULT_NUMBER);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,3 +1,4 @@
|
||||
// 默认交易所常量
|
||||
export const XUEQIU_SZ = "SZ"; // 深交所相关常量
|
||||
export const XUEQIU_SH = "SH"; // 上交所相关常量
|
||||
export const XUEQIU_HK = "HK"; // 港交所相关常量
|
||||
|
@ -2,9 +2,13 @@
|
||||
const XueqiuApiCodeTransform = require("stocks/xueqiu/transforms/api-code").default;
|
||||
|
||||
describe("【雪球】股票代码转换统一代码", () => {
|
||||
const {
|
||||
ERROR_API_CODE,
|
||||
} = require("utils/constant");
|
||||
|
||||
it("深交所股票代码转换统一代码", async () => {
|
||||
expect(() => (new XueqiuApiCodeTransform()).SZTransform("000000"))
|
||||
.toThrow(new Error("请检查股票代码是否正确"));
|
||||
.toThrow(new Error(ERROR_API_CODE));
|
||||
|
||||
expect((new XueqiuApiCodeTransform()).SZTransform("SZ000000"))
|
||||
.toBe("SZ000000");
|
||||
@ -12,7 +16,7 @@ describe("【雪球】股票代码转换统一代码", () => {
|
||||
|
||||
it("上交所股票代码转换统一代码", async () => {
|
||||
expect(() => (new XueqiuApiCodeTransform()).SHTransform("000000"))
|
||||
.toThrow(new Error("请检查股票代码是否正确"));
|
||||
.toThrow(new Error(ERROR_API_CODE));
|
||||
|
||||
expect((new XueqiuApiCodeTransform()).SHTransform("SH000000"))
|
||||
.toBe("SH000000");
|
||||
@ -20,7 +24,7 @@ describe("【雪球】股票代码转换统一代码", () => {
|
||||
|
||||
it("港交所股票代码转换统一代码", async () => {
|
||||
expect(() => (new XueqiuApiCodeTransform()).HKTransform("000000"))
|
||||
.toThrow(new Error("请检查股票代码是否正确"));
|
||||
.toThrow(new Error(ERROR_API_CODE));
|
||||
|
||||
expect((new XueqiuApiCodeTransform()).HKTransform("HK000000"))
|
||||
.toBe("HK000000");
|
||||
@ -28,7 +32,7 @@ describe("【雪球】股票代码转换统一代码", () => {
|
||||
|
||||
it("美交所股票代码转换统一代码", async () => {
|
||||
expect(() => (new XueqiuApiCodeTransform()).USTransform("SZ000000"))
|
||||
.toThrow(new Error("请检查股票代码是否正确"));
|
||||
.toThrow(new Error(ERROR_API_CODE));
|
||||
|
||||
expect((new XueqiuApiCodeTransform()).USTransform("000000"))
|
||||
.toBe("US000000");
|
||||
|
@ -2,9 +2,13 @@
|
||||
const XueqiuCommonCodeTransform = require("stocks/xueqiu/transforms/common-code").default;
|
||||
|
||||
describe("【雪球】统一代码转换股票代码", () => {
|
||||
const {
|
||||
ERROR_COMMON_CODE,
|
||||
} = require("utils/constant");
|
||||
|
||||
it("深交所统一代码转换股票代码", async () => {
|
||||
expect(() => (new XueqiuCommonCodeTransform()).SZTransform("000000"))
|
||||
.toThrow(new Error("请检查统一代码是否正确"));
|
||||
.toThrow(new Error(ERROR_COMMON_CODE));
|
||||
|
||||
expect((new XueqiuCommonCodeTransform()).SZTransform("SZ000000"))
|
||||
.toBe("SZ000000");
|
||||
@ -12,7 +16,7 @@ describe("【雪球】统一代码转换股票代码", () => {
|
||||
|
||||
it("上交所统一代码转换股票代码", async () => {
|
||||
expect(() => (new XueqiuCommonCodeTransform()).SHTransform("000000"))
|
||||
.toThrow(new Error("请检查统一代码是否正确"));
|
||||
.toThrow(new Error(ERROR_COMMON_CODE));
|
||||
|
||||
expect((new XueqiuCommonCodeTransform()).SHTransform("SH000000"))
|
||||
.toBe("SH000000");
|
||||
@ -20,7 +24,7 @@ describe("【雪球】统一代码转换股票代码", () => {
|
||||
|
||||
it("港交所统一代码转换股票代码", async () => {
|
||||
expect(() => (new XueqiuCommonCodeTransform()).HKTransform("000000"))
|
||||
.toThrow(new Error("请检查统一代码是否正确"));
|
||||
.toThrow(new Error(ERROR_COMMON_CODE));
|
||||
|
||||
expect((new XueqiuCommonCodeTransform()).HKTransform("HK000000"))
|
||||
.toBe("HK000000");
|
||||
@ -28,7 +32,7 @@ describe("【雪球】统一代码转换股票代码", () => {
|
||||
|
||||
it("美交所统一代码转换股票代码", async () => {
|
||||
expect(() => (new XueqiuCommonCodeTransform()).USTransform("000000"))
|
||||
.toThrow(new Error("请检查统一代码是否正确"));
|
||||
.toThrow(new Error(ERROR_COMMON_CODE));
|
||||
|
||||
expect((new XueqiuCommonCodeTransform()).USTransform("US000000"))
|
||||
.toBe("000000");
|
||||
@ -48,7 +52,7 @@ describe("【雪球】统一代码转换股票代码", () => {
|
||||
.toBe("000000");
|
||||
|
||||
expect(() => (new XueqiuCommonCodeTransform()).transform("000000"))
|
||||
.toThrow(new Error("请检查统一代码是否正确"));
|
||||
.toThrow(new Error(ERROR_COMMON_CODE));
|
||||
});
|
||||
|
||||
it("交易所统一代码组转换股票代码组", async () => {
|
||||
@ -65,6 +69,6 @@ describe("【雪球】统一代码转换股票代码", () => {
|
||||
.toStrictEqual(["000000"]);
|
||||
|
||||
expect(() => (new XueqiuCommonCodeTransform()).transforms(["000000"]))
|
||||
.toThrow(new Error("请检查统一代码是否正确"));
|
||||
.toThrow(new Error(ERROR_COMMON_CODE));
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user