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
f366a3df50
commit
28eb3161e3
@ -1,10 +1,11 @@
|
||||
// Types
|
||||
import Stock from "types/utils/stock";
|
||||
import StockApi from "types/stocks/index";
|
||||
|
||||
/**
|
||||
* 基础股票代码接口
|
||||
*/
|
||||
class Base {
|
||||
class Base implements StockApi {
|
||||
/**
|
||||
* 构造函数
|
||||
*/
|
||||
|
@ -2,12 +2,12 @@
|
||||
import { SZ, SH, HK, US } from "@utils/constant";
|
||||
|
||||
// Types
|
||||
import ExchangeTransform from "types/stocks/transforms/exchange";
|
||||
import ApiCodeTransform from "types/stocks/transforms/api-code";
|
||||
|
||||
/**
|
||||
* 基础股票代码转换
|
||||
*/
|
||||
class BaseExchangeTransform implements ExchangeTransform {
|
||||
class BaseApiCodeTransform implements ApiCodeTransform {
|
||||
/**
|
||||
* 构造函数
|
||||
*/
|
||||
@ -19,19 +19,19 @@ class BaseExchangeTransform implements ExchangeTransform {
|
||||
*/
|
||||
public transform(code: string): string {
|
||||
if (code.includes(SH)) {
|
||||
return this.SHExchangeTransform(code);
|
||||
return this.SHTransform(code);
|
||||
}
|
||||
|
||||
if (code.includes(SZ)) {
|
||||
return this.SZExchangeTransform(code);
|
||||
return this.SZTransform(code);
|
||||
}
|
||||
|
||||
if (code.includes(HK)) {
|
||||
return this.HKExchangeTransform(code);
|
||||
return this.HKTransform(code);
|
||||
}
|
||||
|
||||
if (code.search(US) !== -1) {
|
||||
return this.USExchangeTransform(code);
|
||||
if (code.includes(US)) {
|
||||
return this.USTransform(code);
|
||||
}
|
||||
|
||||
throw new Error("请检查股票代码是否正确");
|
||||
@ -49,7 +49,7 @@ class BaseExchangeTransform implements ExchangeTransform {
|
||||
* 深交所股票代码转换
|
||||
* @param code 股票代码
|
||||
*/
|
||||
public SZExchangeTransform(code: string): string {
|
||||
public SZTransform(code: string): string {
|
||||
if (!code.includes(SZ)) {
|
||||
throw new Error("请检查股票代码是否正确");
|
||||
}
|
||||
@ -61,7 +61,7 @@ class BaseExchangeTransform implements ExchangeTransform {
|
||||
* 上交所股票代码转换
|
||||
* @param code 股票代码
|
||||
*/
|
||||
public SHExchangeTransform(code: string): string {
|
||||
public SHTransform(code: string): string {
|
||||
if (!code.includes(SH)) {
|
||||
throw new Error("请检查股票代码是否正确");
|
||||
}
|
||||
@ -73,7 +73,7 @@ class BaseExchangeTransform implements ExchangeTransform {
|
||||
* 港交所股票代码转换
|
||||
* @param code 股票代码
|
||||
*/
|
||||
public HKExchangeTransform(code: string): string {
|
||||
public HKTransform(code: string): string {
|
||||
if (!code.includes(HK)) {
|
||||
throw new Error("请检查股票代码是否正确");
|
||||
}
|
||||
@ -85,7 +85,7 @@ class BaseExchangeTransform implements ExchangeTransform {
|
||||
* 美交所股票代码转换
|
||||
* @param code 股票代码
|
||||
*/
|
||||
public USExchangeTransform(code: string): string {
|
||||
public USTransform(code: string): string {
|
||||
if (!code.includes(US)) {
|
||||
throw new Error("请检查股票代码是否正确");
|
||||
}
|
||||
@ -94,4 +94,4 @@ class BaseExchangeTransform implements ExchangeTransform {
|
||||
}
|
||||
}
|
||||
|
||||
export default BaseExchangeTransform;
|
||||
export default BaseApiCodeTransform;
|
62
src/stocks/base/transforms/local-code.ts
Normal file
62
src/stocks/base/transforms/local-code.ts
Normal file
@ -0,0 +1,62 @@
|
||||
// Types
|
||||
import LocalCodeTransform from "types/stocks/transforms/local-code";
|
||||
|
||||
/**
|
||||
* 基础股票代码转换统一码
|
||||
*/
|
||||
class BaseLocalCodeTransform implements LocalCodeTransform {
|
||||
/**
|
||||
* 构造函数
|
||||
*/
|
||||
constructor() { }
|
||||
|
||||
/**
|
||||
* 交易所股票代码转换统一码
|
||||
* @param code 股票代码
|
||||
*/
|
||||
public transform(code: string): string {
|
||||
throw new Error("未实现股票代码转换统一码");
|
||||
}
|
||||
|
||||
/**
|
||||
* 交易所股票组代码转换统一码
|
||||
* @param codes 股票代码
|
||||
*/
|
||||
public transforms(codes: string[]): string[] {
|
||||
return codes.map((code) => this.transform(code));
|
||||
}
|
||||
|
||||
/**
|
||||
* 深交所股票代码转换统一码
|
||||
* @param code 股票代码
|
||||
*/
|
||||
public SZTransform(code: string): string {
|
||||
throw new Error("未实现深交所股票代码转换统一码");
|
||||
}
|
||||
|
||||
/**
|
||||
* 上交所股票代码转换统一码
|
||||
* @param code 股票代码
|
||||
*/
|
||||
public SHTransform(code: string): string {
|
||||
throw new Error("未实现上交所股票代码转换统一码");
|
||||
}
|
||||
|
||||
/**
|
||||
* 港交所股票代码转换统一码
|
||||
* @param code 股票代码
|
||||
*/
|
||||
public HKTransform(code: string): string {
|
||||
throw new Error("未实现港交所股票代码转换统一码");
|
||||
}
|
||||
|
||||
/**
|
||||
* 美交所股票代码转换统一码
|
||||
* @param code 股票代码
|
||||
*/
|
||||
public USTransform(code: string): string {
|
||||
throw new Error("未实现美交所股票代码转换统一码");
|
||||
}
|
||||
}
|
||||
|
||||
export default BaseLocalCodeTransform;
|
@ -1,71 +1,70 @@
|
||||
// Stock
|
||||
const BaseExchangeTransform = require("stocks/base/transforms/exchange").default;
|
||||
const BaseApiCodeTransform = require("stocks/base/transforms/api-code").default;
|
||||
|
||||
describe("【基础】股票代码转换测试", () => {
|
||||
it("深交所股票代码转换", async () => {
|
||||
expect(() => (new BaseExchangeTransform()).SZExchangeTransform("000000"))
|
||||
expect(() => (new BaseApiCodeTransform()).SZTransform("000000"))
|
||||
.toThrow(new Error("请检查股票代码是否正确"));
|
||||
|
||||
expect(() => (new BaseExchangeTransform()).SZExchangeTransform("SZ000000"))
|
||||
expect(() => (new BaseApiCodeTransform()).SZTransform("SZ000000"))
|
||||
.toThrow(new Error("未实现深交所股票代码转换"));
|
||||
});
|
||||
|
||||
it("上交所股票代码转换", async () => {
|
||||
expect(() => (new BaseExchangeTransform()).SHExchangeTransform("000000"))
|
||||
expect(() => (new BaseApiCodeTransform()).SHTransform("000000"))
|
||||
.toThrow(new Error("请检查股票代码是否正确"));
|
||||
|
||||
expect(() => (new BaseExchangeTransform())
|
||||
.SHExchangeTransform("SH000000"))
|
||||
expect(() => (new BaseApiCodeTransform()).SHTransform("SH000000"))
|
||||
.toThrow(new Error("未实现上交所股票代码转换"));
|
||||
});
|
||||
|
||||
it("港交所股票代码转换", async () => {
|
||||
expect(() => new BaseExchangeTransform().HKExchangeTransform("000000"))
|
||||
expect(() => new BaseApiCodeTransform().HKTransform("000000"))
|
||||
.toThrow(new Error("请检查股票代码是否正确"));
|
||||
|
||||
expect(() => new BaseExchangeTransform().HKExchangeTransform("HK000000"))
|
||||
expect(() => new BaseApiCodeTransform().HKTransform("HK000000"))
|
||||
.toThrow(new Error("未实现港交所股票代码转换"));
|
||||
});
|
||||
|
||||
it("美交所股票代码转换", async () => {
|
||||
expect(() => (new BaseExchangeTransform()).USExchangeTransform("000000"))
|
||||
expect(() => (new BaseApiCodeTransform()).USTransform("000000"))
|
||||
.toThrow(new Error("请检查股票代码是否正确"));
|
||||
|
||||
expect(() => (new BaseExchangeTransform()).USExchangeTransform("US000000"))
|
||||
expect(() => (new BaseApiCodeTransform()).USTransform("US000000"))
|
||||
.toThrow(new Error("未实现美交所股票代码转换"));
|
||||
});
|
||||
|
||||
it("交易所股票代码转换", async () => {
|
||||
expect(() => (new BaseExchangeTransform()).transform("000000"))
|
||||
expect(() => (new BaseApiCodeTransform()).transform("000000"))
|
||||
.toThrow(new Error("请检查股票代码是否正确"));
|
||||
|
||||
expect(() => (new BaseExchangeTransform()).transform("SZ000000"))
|
||||
expect(() => (new BaseApiCodeTransform()).transform("SZ000000"))
|
||||
.toThrow(new Error("未实现深交所股票代码转换"));
|
||||
|
||||
expect(() => (new BaseExchangeTransform()).transform("SH000000"))
|
||||
expect(() => (new BaseApiCodeTransform()).transform("SH000000"))
|
||||
.toThrow(new Error("未实现上交所股票代码转换"));
|
||||
|
||||
expect(() => (new BaseExchangeTransform()).transform("HK000000"))
|
||||
expect(() => (new BaseApiCodeTransform()).transform("HK000000"))
|
||||
.toThrow(new Error("未实现港交所股票代码转换"));
|
||||
|
||||
expect(() => (new BaseExchangeTransform()).transform("US000000"))
|
||||
expect(() => (new BaseApiCodeTransform()).transform("US000000"))
|
||||
.toThrow(new Error("未实现美交所股票代码转换"));
|
||||
});
|
||||
|
||||
it("交易所股票组代码转换", async () => {
|
||||
expect(() => (new BaseExchangeTransform()).transforms(["000000"]))
|
||||
expect(() => (new BaseApiCodeTransform()).transforms(["000000"]))
|
||||
.toThrow(new Error("请检查股票代码是否正确"));
|
||||
|
||||
expect(() => (new BaseExchangeTransform()).transforms(["SZ000000"]))
|
||||
expect(() => (new BaseApiCodeTransform()).transforms(["SZ000000"]))
|
||||
.toThrow(new Error("未实现深交所股票代码转换"));
|
||||
|
||||
expect(() => (new BaseExchangeTransform()).transforms(["SH000000"]))
|
||||
expect(() => (new BaseApiCodeTransform()).transforms(["SH000000"]))
|
||||
.toThrow(new Error("未实现上交所股票代码转换"));
|
||||
|
||||
expect(() => (new BaseExchangeTransform()).transforms(["HK000000"]))
|
||||
expect(() => (new BaseApiCodeTransform()).transforms(["HK000000"]))
|
||||
.toThrow(new Error("未实现港交所股票代码转换"));
|
||||
|
||||
expect(() => (new BaseExchangeTransform()).transforms(["US000000"]))
|
||||
expect(() => (new BaseApiCodeTransform()).transforms(["US000000"]))
|
||||
.toThrow(new Error("未实现美交所股票代码转换"));
|
||||
});
|
||||
});
|
7
types/stocks/index.d.ts
vendored
7
types/stocks/index.d.ts
vendored
@ -1,11 +1,6 @@
|
||||
import Stock from '../utils/stock';
|
||||
|
||||
export interface StockApi {
|
||||
/**
|
||||
* 构造函数
|
||||
*/
|
||||
new(): StockApi;
|
||||
|
||||
declare class StockApi {
|
||||
/**
|
||||
* 构造函数
|
||||
*/
|
||||
|
10
types/stocks/transforms/api-code.d.ts
vendored
Normal file
10
types/stocks/transforms/api-code.d.ts
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
export interface ApiCodeTransform {
|
||||
transform(code: string): string;
|
||||
transforms(codes: string[]): string[];
|
||||
SZTransform(code: string): string;
|
||||
SHTransform(code: string): string;
|
||||
HKTransform(code: string): string;
|
||||
USTransform(code: string): string;
|
||||
}
|
||||
|
||||
export default ApiCodeTransform;
|
10
types/stocks/transforms/exchange.d.ts
vendored
10
types/stocks/transforms/exchange.d.ts
vendored
@ -1,10 +0,0 @@
|
||||
export interface ExchangeTransform {
|
||||
transform(code: string): string;
|
||||
transforms(codes: string[]): string[];
|
||||
SZExchangeTransform(code: string): string;
|
||||
SHExchangeTransform(code: string): string;
|
||||
HKExchangeTransform(code: string): string;
|
||||
USExchangeTransform(code: string): string;
|
||||
}
|
||||
|
||||
export default ExchangeTransform;
|
10
types/stocks/transforms/local-code.d.ts
vendored
Normal file
10
types/stocks/transforms/local-code.d.ts
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
export interface LocalCodeTransform {
|
||||
transform(code: string): string;
|
||||
transforms(codes: string[]): string[];
|
||||
SZTransform(code: string): string;
|
||||
SHTransform(code: string): string;
|
||||
HKTransform(code: string): string;
|
||||
USTransform(code: string): string;
|
||||
}
|
||||
|
||||
export default LocalCodeTransform;
|
Loading…
Reference in New Issue
Block a user