From eb680b04ffe51eba24b3140e8eea3d4cfe3e1fcd Mon Sep 17 00:00:00 2001 From: William Bergeron-Drouin Date: Fri, 1 Nov 2019 18:01:47 -0400 Subject: [PATCH] Added types for svg-intersections (#39899) * Generated svg-intersections files using npx * Added base typings for shape method with conditional second argument type * Added missing types in SvgProperties * Added basic Shape interface * Added types for intersect function * Refactored intersection to interface and added typings to Matrix2D * Linted index.d.ts * Added line test * Added tests for rect, circle, ellipse & polygon * Added test for path * Rectangle rx&ry should be optional * Added intersections test * Replaced unnecessary typeof with generic type --- types/svg-intersections/index.d.ts | 85 +++++++++++++++++++ .../svg-intersections-tests.ts | 11 +++ types/svg-intersections/tsconfig.json | 23 +++++ types/svg-intersections/tslint.json | 1 + 4 files changed, 120 insertions(+) create mode 100644 types/svg-intersections/index.d.ts create mode 100644 types/svg-intersections/svg-intersections-tests.ts create mode 100644 types/svg-intersections/tsconfig.json create mode 100644 types/svg-intersections/tslint.json diff --git a/types/svg-intersections/index.d.ts b/types/svg-intersections/index.d.ts new file mode 100644 index 0000000000..3909b708c0 --- /dev/null +++ b/types/svg-intersections/index.d.ts @@ -0,0 +1,85 @@ +// Type definitions for svg-intersections 0.4 +// Project: https://github.com/effektif/svg-intersections#readme +// Definitions by: xWiiLLz +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.8 + +export type SvgElements = 'line' | 'rect' | 'circle' | 'ellipse' | 'polygon' | 'polyline' | 'path'; + +// Svg element properties +export interface LineProps { + x1: number; + y1: number; + x2: number; + y2: number; +} + +export interface RectProps { + rx?: number; + ry?: number; + x: number; + y: number; + width: number; + height: number; +} + +export interface CircleProps { + cx: number; + cy: number; + r: number; +} + +export interface EllipseProps { + cx: number; + cy: number; + rx: number; + ry: number; +} + +export interface PolygonProps { + points: string; +} + +export type PolylineProps = PolygonProps; + +export interface PathProps { + d: string; +} + +export type SvgProperties = + T extends 'line' ? LineProps : + T extends 'rect' ? RectProps : + T extends 'circle' ? CircleProps : + T extends 'ellipse' ? EllipseProps : + T extends 'polygon' ? PolygonProps : + T extends 'polyline' ? PolylineProps : + T extends 'path' ? PathProps : + never; + +export interface Shape { + type: string; + meta: object; + params: object; +} + +export interface Matrix2D { + a: number; + b: number; + c: number; + d: number; + e: number; + f: number; +} + +export interface Point2D { + x: number; + y: number; +} + +export interface Intersection { + status: string; + points: Point2D[]; +} + +export function shape(svgElementName: T, svgProps: SvgProperties): Shape; +export function intersect(shape1: Shape, shape2: Shape, m1?: Matrix2D, m2?: Matrix2D): Intersection; diff --git a/types/svg-intersections/svg-intersections-tests.ts b/types/svg-intersections/svg-intersections-tests.ts new file mode 100644 index 0000000000..fe134b3e5c --- /dev/null +++ b/types/svg-intersections/svg-intersections-tests.ts @@ -0,0 +1,11 @@ +import { intersect, shape } from 'svg-intersections'; + +const line = shape('line', {x1: 0, y1: 0, x2: 2, y2: 3}); +const rect = shape('rect', {x: 0, y: 0, width: 200, height: 100}); +const rectWithOptional = shape('rect', {x: 0, y: 0, width: 200, height: 100, rx: 4, ry: 4}); +const circle = shape('circle', {cx: 0, cy: 0, r: 100}); +const ellipse = shape('ellipse', {rx: 100, ry: 150, cx: 0, cy: 0}); +const polygon = shape('polygon', {points: '-5,0 0,10 5,0 0,-10'}); +const path = shape('path', {d: 'M 10 10 h 80 v 80 h -80 Z'}); + +const intersections = intersect(line, rect); diff --git a/types/svg-intersections/tsconfig.json b/types/svg-intersections/tsconfig.json new file mode 100644 index 0000000000..f84547d420 --- /dev/null +++ b/types/svg-intersections/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "svg-intersections-tests.ts" + ] +} diff --git a/types/svg-intersections/tslint.json b/types/svg-intersections/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/svg-intersections/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }