[earcut] Added types. (#22979)

* [earcut] Added types.
Double export matches source: https://github.com/mapbox/earcut/blob/master/src/earcut.js#L3-L4

* [earcut] Used interface, added flatten and deviation.
This commit is contained in:
Adrian Leonhard
2018-01-17 20:13:18 +01:00
committed by Wesley Wigham
parent 91471d97e2
commit 426d98a191
4 changed files with 90 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
const a = earcut([10, 0, 0, 50, 60, 60, 70, 10]);
// @example with a hole:
const b = earcut([0, 0, 100, 0, 100, 100, 0, 100, 20, 20, 80, 20, 80, 80, 20, 80], [4]); // [3,0,4, 5,4,0, 3,4,7, 5,0,1, 2,3,7, 6,5,1, 2,7,6, 6,1,2]
// @example with 3d coords:
const c = earcut([10, 0, 1, 0, 50, 2, 60, 60, 3, 70, 10, 4], undefined, 3); // [1,0,3, 3,2,1]
declare const vertices: number[];
declare const holes: number[];
declare const dimensions: number;
const triangles = earcut(vertices, holes, dimensions);
const deviation = earcut.deviation(vertices, holes, dimensions, triangles);
const data = earcut.flatten([[[0, 0], [100, 0], [0, 100]], [[10, 10], [0, 10], [10, 0]]]);
const triangles2 = earcut(data.vertices, data.holes, data.dimensions);
declare const multiDimData: number[][][];
const triangles3 = earcut(data.vertices, data.holes, data.dimensions);

49
types/earcut/index.d.ts vendored Normal file
View File

@@ -0,0 +1,49 @@
// Type definitions for earcut 2.1
// Project: https://github.com/mapbox/earcut#readme
// Definitions by: Adrian Leonhard <https://github.com/NaridaL>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// Duplicated export mirrors actual source: https://github.com/mapbox/earcut/blob/master/src/earcut.js#L3-L4
interface EarcutStatic {
/**
* Triangulate an outline.
*
* @param vertices A flat array of vertice coordinates like [x0,y0, x1,y1, x2,y2, ...].
* @param holes An array of hole indices if any (e.g. [5, 8] for a 12-vertice input would mean one hole with vertices 57 and another with 811).
* @param dimensions The number of coordinates per vertice in the input array (2 by default).
* @return A flat array with each group of three numbers indexing a triangle in the `vertices` array.
* @example earcut([10,0, 0,50, 60,60, 70,10]); // returns [1,0,3, 3,2,1]
* @example with a hole: earcut([0,0, 100,0, 100,100, 0,100, 20,20, 80,20, 80,80, 20,80], [4]); // [3,0,4, 5,4,0, 3,4,7, 5,0,1, 2,3,7, 6,5,1, 2,7,6, 6,1,2]
* @example with 3d coords: earcut([10,0,1, 0,50,2, 60,60,3, 70,10,4], null, 3); // [1,0,3, 3,2,1]
*/
(vertices: ArrayLike<number>, holes?: ArrayLike<number>, dimensions?: number): number[];
/**
* Transforms multi-dimensional array (e.g. GeoJSON Polygon) into the format expected by earcut.
* @example Transforming GeoJSON data.
* const data = earcut.flatten(geojson.geometry.coordinates);
* const triangles = earcut(data.vertices, data.holes, data.dimensions);
* @example Transforming simple triangle with hole:
* const data = earcut.flatten([[[0, 0], [100, 0], [0, 100]], [[10, 10], [0, 10], [10, 0]]]);
* const triangles = earcut(data.vertices, data.holes, data.dimensions);
* @param data Arrays of rings, with the first being the outline and the rest holes. A ring is an array points, each point being an array of numbers.
*/
flatten(data: ArrayLike<ArrayLike<ArrayLike<number>>>): { vertices: number[], holes: number[], dimensions: number };
/**
* Returns the relative difference between the total area of triangles and the area of the input polygon. 0 means the triangulation is fully correct.
* @param vertices same as earcut
* @param holes same as earcut
* @param dimensions same as earcut
* @param triangles see return value of earcut
* @example
* const triangles = earcut(vertices, holes, dimensions);
* const deviation = earcut.deviation(vertices, holes, dimensions, triangles);
*/
deviation(vertices: ArrayLike<number>, holes: ArrayLike<number> | undefined, dimensions: number, triangles: ArrayLike<number>): number;
default: EarcutStatic;
}
declare const exports: EarcutStatic;
export = exports;
export as namespace earcut;

View File

@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true,
"strictFunctionTypes": true
},
"files": [
"index.d.ts",
"earcut-tests.ts"
]
}

1
types/earcut/tslint.json Normal file
View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }