From d58e1fcc50fcee95020975b4f9adbebf0c0fb217 Mon Sep 17 00:00:00 2001 From: Tobias Kraus <49694150+tobiaskraus@users.noreply.github.com> Date: Mon, 13 May 2019 19:30:29 +0200 Subject: [PATCH] Updated "delaunator" typings to version 3.0 (#35165) * Updated "delaunator" typings to version 3.0 * delaunator: fix wrong $ExpectType in test * delaunator: export class -> export default class * delaunator: handle both: UMD & ES module files. * TypeScript >= 2.7 required * flag 'esModuleInterop' in tsconfig required * thanks @weswigham for the help --- types/delaunator/delaunator-tests.ts | 24 ++++++++++++++++---- types/delaunator/index.d.ts | 34 +++++++++++++--------------- types/delaunator/tsconfig.json | 3 ++- 3 files changed, 37 insertions(+), 24 deletions(-) diff --git a/types/delaunator/delaunator-tests.ts b/types/delaunator/delaunator-tests.ts index 6310448c83..4334aa8dbb 100644 --- a/types/delaunator/delaunator-tests.ts +++ b/types/delaunator/delaunator-tests.ts @@ -1,4 +1,4 @@ -import * as Delaunator from 'delaunator'; +import Delaunator from 'delaunator'; // Zipped points [x0, y0, x1, y1, ...] const zippedPoints = [168, 180, 168, 178, 168, 179, 168, 181, 168, 183, 167, 183, 167, 184]; @@ -13,7 +13,7 @@ interface CustomPoint { x: number; y: number; } -const customPoints = [{x: 168, y: 180}, {x: 168, y: 178}, {x: 168, y: 179}, {x: 168, y: 181}, {x: 168, y: 183}, {x: 167, y: 183}, {x: 167, y: 184}]; +const customPoints = [{ x: 168, y: 180 }, { x: 168, y: 178 }, { x: 168, y: 179 }, { x: 168, y: 181 }, { x: 168, y: 183 }, { x: 167, y: 183 }, { x: 167, y: 184 }]; const getX = (point: CustomPoint) => point.x; const getY = (point: CustomPoint) => point.y; @@ -22,9 +22,10 @@ Delaunator.from(customPoints, point => point.x, point => point.y); Delaunator.from(customPoints, getX, getY); // To get the coordinates of all triangles, use: -const triangles = d.triangles; -const halfedges = d.halfedges; -const hull = d.hull; +const triangles = d.triangles; // $ExpectType Uint32Array +const halfedges = d.halfedges; // $ExpectType Int32Array +const hull = d.hull; // $ExpectType Uint32Array +const coords = d.coords; // $ExpectType ArrayLike | Float64Array const coordinates: number[][][] = []; for (let i = 0; i < triangles.length; i += 3) { coordinates.push([ @@ -33,3 +34,16 @@ for (let i = 0; i < triangles.length; i += 3) { defaultPoints[triangles[i + 2]] ]); } + +// Or use Delaunator.coords (but coords is a flat array in the form of [x0, y0, x1, y1, ...]) +const coordinates2: number[][][] = []; +for (let i = 0; i < triangles.length; i += 3) { + coordinates2.push([ + [coords[triangles[i] * 2], coords[triangles[i] * 2 + 1]], + [coords[triangles[i + 1] * 2], coords[triangles[i + 1] * 2 + 1]], + [coords[triangles[i + 2] * 2], coords[triangles[i + 2] * 2 + 1]] + ]); +} + +// both approaches should give the same result +JSON.stringify(coordinates) === JSON.stringify(coordinates2); diff --git a/types/delaunator/index.d.ts b/types/delaunator/index.d.ts index c717feec95..ee1126a67e 100644 --- a/types/delaunator/index.d.ts +++ b/types/delaunator/index.d.ts @@ -1,17 +1,20 @@ -// Type definitions for delaunator 2.0 +// Type definitions for delaunator 3.0 // Project: https://github.com/mapbox/delaunator#readme // Definitions by: Denis Carriere // Bradley Odell +// Tobias Kraus // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.7 declare class Delaunator

{ /** - * A flat Uint32Array array of triangle vertex indices (each group of three numbers forms a triangle). All triangles are directed counterclockwise. + * A Uint32Array array of triangle vertex indices (each group of three numbers forms a triangle). + * All triangles are directed counterclockwise. */ triangles: Uint32Array; /** - * A flat Int32Array array of triangle half-edge indices that allows you to traverse the triangulation. + * A Int32Array array of triangle half-edge indices that allows you to traverse the triangulation. * i-th half-edge in the array corresponds to vertex triangles[i] the half-edge is coming from. * halfedges[i] is the index of a twin half-edge in an adjacent triangle (or -1 for outer half-edges on the convex hull). * @@ -20,36 +23,31 @@ declare class Delaunator

{ halfedges: Int32Array; /** - * A circular doubly-linked list that holds a convex hull of the delaunay triangulation. + * A Uint32Array array of indices that reference points on the convex hull of the input data, counter-clockwise. */ - hull: Delaunator.Node; + hull: Uint32Array; + + /** + * An array of input coordinates in the form [x0, y0, x1, y1, ....], of the type provided in the constructor (or Float64Array if you used Delaunator.from). + */ + coords: ArrayLike | Float64Array; /** * Constructs a delaunay triangulation object given a typed array of point coordinates of the form: [x0, y0, x1, y1, ...]. + * (use a typed array for best performance). */ constructor(points: ArrayLike); /** - * Constructs a delaunay triangulation object given an array of points (e.g. [x, y]). Duplicate points are skipped. + * Constructs a delaunay triangulation object given an array of points ([x, y] by default). */ static from(points: ArrayLike>): Delaunator>; /** * Constructs a delaunay triangulation object given an array of custom points. Duplicate points are skipped. + * getX and getY are optional functions for custom point formats. Duplicate points are skipped. */ static from

(points: ArrayLike

, getX: (point: P) => number, getY: (point: P) => number): Delaunator

; } -declare namespace Delaunator { - interface Node { - i: number; - x: number; - y: number; - t: number; - prev: Node|null; - next: Node|null; - removed: boolean; - } -} - export = Delaunator; diff --git a/types/delaunator/tsconfig.json b/types/delaunator/tsconfig.json index a9c94600d8..97bca0c508 100644 --- a/types/delaunator/tsconfig.json +++ b/types/delaunator/tsconfig.json @@ -14,7 +14,8 @@ ], "types": [], "noEmit": true, - "forceConsistentCasingInFileNames": true + "forceConsistentCasingInFileNames": true, + "esModuleInterop": true }, "files": [ "index.d.ts",