feat: 修改 基础接口类型

This commit is contained in:
zhangxiangliang 2020-08-19 22:21:10 +08:00
parent f366a3df50
commit 28eb3161e3
8 changed files with 116 additions and 49 deletions

View File

@ -1,10 +1,11 @@
// Types
import Stock from "types/utils/stock";
import StockApi from "types/stocks/index";
/**
*
*/
class Base {
class Base implements StockApi {
/**
*
*/

View File

@ -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;

View 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;

View File

@ -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("未实现美交所股票代码转换"));
});
});

View File

@ -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
View 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;

View File

@ -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
View 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;