typescript - 如何验证方法中的数据并明确功能?
问题描述
我有以下方法:
getGeometry(): { polygon: Polygon; circle: Circle } {
let geometry = { circle: null, polygon: null };
if (this.geometry instanceof CircleRegistryPath) {
if (!this.geometry.circle) return this.toastr.error('Не выбрана геометрия объекта');
geometry.circle = this.geometry.circle;
}
if (this.geometry instanceof PolygonRegistryPath || this.geometry instanceof RectangleRegistryPath) {
if (!this.geometry.polygons.length) return this.toastr.error('No geometry');
const [polygon, ...rest] = this.geometry.polygons;
const firstVertex = polygon.Vertexes[0];
if (!pointsAreEqual(firstVertex, polygon.Vertexes[polygon.Vertexes.length - 1])) polygon.Vertexes.push(firstVertex);
geometry.polygon = polygon;
}
return geometry;
}
我试图让这个方法清楚,但你注意到它在错误情况和{ polygon: Polygon; circle: Circle }
其他情况下返回 false。
如何验证数据并使该方法清晰?
我的第二次尝试:
getGeometry(geometry: CircleRegistryPath | PolygonRegistryPath | RectangleRegistryPath): { polygon: Polygon; circle: Circle } {
const data = { circle: null, polygon: null };
if (geometry instanceof CircleRegistryPath) {
if (!geometry.circle) throw '';
data.circle = geometry.circle;
}
if (geometry instanceof PolygonRegistryPath || geometry instanceof RectangleRegistryPath) {
if (!geometry.polygons.length) throw '';
const [polygon, ...rest] = geometry.polygons;
const firstVertex = polygon.Vertexes[0];
if (!pointsAreEqual(firstVertex, polygon.Vertexes[polygon.Vertexes.length - 1])) polygon.Vertexes.push(firstVertex);
data.polygon = polygon;
}
return data;
}
解决方案
多态性和解构可能有助于获得干净的代码。
type Circle = {
x: number,
y: number,
r: number
} | null |undefined;
type Vertex = {
x: number,
y: number
} | null | undefined
type Polygon = {
Vertexes: Vertex[]
} | null | undefined;
type Data = {
circle?: Circle,
polygons?: Polygon,
} | null;
class Geometry {
data: Data = null;
constructor(data: Data){
this.data = data
}
getData(): Data {
return this.data as Data;
}
}
class CircleRegistryPath extends Geometry {
circle: Circle;
constructor(init: Circle){
super({circle: init});
}
}
class PolygonRegistryPath extends Geometry{
polygons: Polygon;
constructor(init: Polygon){
super({polygons: init});
}
}
class RectangleRegistryPath extends Geometry{
polygons: Polygon;
constructor(init: Polygon){
super({polygons: init});
}
}
const pointsAreEqual = (a: Vertex, b: Vertex):boolean =>{
if (!a || !b) return false;
return a.x === b.x && a.y === b.y;
}
const closePath = (polygon: Polygon): Polygon => {
if (polygon?.Vertexes.length === 0) return polygon;
const firstVertex = polygon?.Vertexes[0];
const lastVertex = polygon?.Vertexes[polygon?.Vertexes.length - 1];
if (!pointsAreEqual(firstVertex, lastVertex) && firstVertex)
polygon?.Vertexes.push( {x: firstVertex.x, y: firstVertex.y });
return polygon;
}
class GeometryObject {
getGeometry( geometry: Geometry): Data {
if (geometry instanceof PolygonRegistryPath
|| geometry instanceof RectangleRegistryPath) {
closePath(geometry.polygons);
}
return { circle: null, polygons: null, ...geometry.data };
}
}
const c = new CircleRegistryPath({x:10, y:20, r:30})
const r = new RectangleRegistryPath(
{Vertexes: [
{ x:0, y:0},
{ x:50, y:0},
{ x:50, y:50},
{ x:0, y:50}
]}
)
const p = new PolygonRegistryPath(
{Vertexes: [
{ x:0, y:0},
{ x:50, y:0},
{ x:50, y:50},
]}
)
const go = new GeometryObject();
console.log(go.getGeometry(c));
console.log(go.getGeometry(r));
console.log(go.getGeometry(p));
推荐阅读
- php - 如何 preg_match 一个 PCRE 表达式?
- c++ - 不使用 Malloc 的联合查找算法(查找循环)
- firebase - 如何在颤振应用程序中进行 Firebase 电话身份验证
- python - 使用 linux 的 windows 子系统时无法导入“collections.abc”
- json - GULP 从另一个 JSON 文件创建 JSON 文件
- javascript - 如何返回 HTML来自 React 中的 javascript 函数的标记?
- macos - 为什么将 ESC 键映射到明文突出显示的 Vim 映射会在 vim 中导致奇怪的行为?
- ios - Swift:像 Facebook 一样使用滚动视图缩放图像
- angular - 从异步函数调用非异步函数时返回无法正常工作
- javascript - 如何在javascript中正确地用时区对日期进行字符串化?