From 74140e862cc7ba44082353de824cb76e0024a9c1 Mon Sep 17 00:00:00 2001 From: Denis Carriere Date: Sat, 26 Nov 2016 17:19:39 -0500 Subject: [PATCH] Add Typescript definition for Meta --- packages/turf-meta/index.d.ts | 72 ++++++++++++++++++++++++++++ packages/turf-meta/test-types.ts | 80 ++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 packages/turf-meta/index.d.ts create mode 100644 packages/turf-meta/test-types.ts diff --git a/packages/turf-meta/index.d.ts b/packages/turf-meta/index.d.ts new file mode 100644 index 0000000000..af27f4f040 --- /dev/null +++ b/packages/turf-meta/index.d.ts @@ -0,0 +1,72 @@ +/// + +type Points = GeoJSON.FeatureCollection +type Point = GeoJSON.Feature | GeoJSON.Point +type MultiPoints = GeoJSON.FeatureCollection +type MultiPoint = GeoJSON.Feature | GeoJSON.MultiPoint +type LineStrings = GeoJSON.FeatureCollection +type LineString = GeoJSON.Feature | GeoJSON.LineString +type MultiLineStrings = GeoJSON.FeatureCollection +type MultiLineString = GeoJSON.Feature | GeoJSON.MultiLineString +type Polygons = GeoJSON.FeatureCollection +type Polygon = GeoJSON.Feature | GeoJSON.Polygon +type MultiPolygons = GeoJSON.FeatureCollection +type MultiPolygon = GeoJSON.Feature | GeoJSON.MultiPolygon +type Feature = GeoJSON.Feature +type Features = GeoJSON.FeatureCollection +type GeometryCollection = GeoJSON.GeometryCollection +type GeometryObject = GeoJSON.GeometryObject + +interface MetaStatic { + /** + * http://turfjs.org/docs/#coordEach + */ + coordEach(layer: Points | Point | MultiPoint | MultiPoints, callback: (coords: Array) => void, excludeWrapCoord?: boolean): void; + coordEach(layer: LineStrings | LineString | MultiLineString | MultiLineStrings, callback: (coords: Array>) => void, excludeWrapCoord?: boolean): void; + coordEach(layer: Polygons | Polygon | MultiPolygons | MultiPolygon, callback: (coords: Array>>) => void, excludeWrapCoord?: boolean): void; + coordEach(layer: GeometryCollection | GeometryObject, callback: (coords: Array) => void, excludeWrapCoord?: boolean): void; + + /** + * http://turfjs.org/docs/#coordEach + */ + coordReduce(layer: Points | Point | MultiPoint | MultiPoints, callback: (memo: any, coords: Array) => void, memo: any, excludeWrapCoord?: boolean): any; + coordReduce(layer: LineStrings | LineString | MultiLineString | MultiLineStrings, callback: (memo: any, coords: Array>) => void, memo: any, excludeWrapCoord?: boolean): any; + coordReduce(layer: Polygons | Polygon | MultiPolygons | MultiPolygon, callback: (memo: any, coords: Array>>) => void, memo: any, excludeWrapCoord?: boolean): any; + coordReduce(layer: GeometryCollection | GeometryObject, callback: (memo: any, coords: Array) => void, memo: any, excludeWrapCoord?: boolean): any; + + /** + * http://turfjs.org/docs/#propEach + */ + propEach(layer: Feature | Features, callback: (properties: any) => void): void; + + /** + * http://turfjs.org/docs/#featureEach + */ + featureEach(layer: Point | Points, callback: (feature: Point) => void): void; + featureEach(layer: LineString | LineStrings, callback: (feature: LineString) => void): void; + featureEach(layer: Polygon | Polygons, callback: (feature: Polygon) => void): void; + featureEach(layer: MultiPoint | MultiPoints, callback: (feature: MultiPoint) => void): void; + featureEach(layer: MultiLineString | MultiLineStrings, callback: (feature: MultiLineString) => void): void; + featureEach(layer: MultiPolygon | MultiPolygons, callback: (feature: MultiPolygon) => void): void; + featureEach(layer: Feature | Features, callback: (feature: Feature) => void): void; + + /** + * http://turfjs.org/docs/#coordAll + */ + coordAll(layer: Feature | Features | GeometryCollection | GeometryObject): Array> + + /** + * http://turfjs.org/docs/#geomEach + */ + geomEach(layer: Point | Points, callback: (geom: GeoJSON.Point) => void): void; + geomEach(layer: LineString | LineStrings, callback: (geom: GeoJSON.LineString) => void): void; + geomEach(layer: Polygon | Polygons, callback: (geom: GeoJSON.Polygon) => void): void; + geomEach(layer: MultiPoint | MultiPoints, callback: (geom: GeoJSON.MultiPoint) => void): void; + geomEach(layer: MultiLineString | MultiLineStrings, callback: (geom: GeoJSON.MultiLineString) => void): void; + geomEach(layer: MultiPolygon | MultiPolygons, callback: (geom: GeoJSON.MultiPolygon) => void): void; + geomEach(layer: Feature | Features, callback: (geom: GeometryObject) => void): void; + geomEach(layer: GeometryCollection | GeometryObject, callback: (geom: GeometryObject) => void): void; +} + +declare const meta: MetaStatic +export = meta diff --git a/packages/turf-meta/test-types.ts b/packages/turf-meta/test-types.ts new file mode 100644 index 0000000000..f38e26b013 --- /dev/null +++ b/packages/turf-meta/test-types.ts @@ -0,0 +1,80 @@ +import * as meta from './index' + +const pointGeometry: GeoJSON.Point = { + type: 'Point', + coordinates: [0, 0] +}; + +const lineStringGeometry: GeoJSON.LineString = { + type: 'LineString', + coordinates: [[0, 0], [1, 1]] +}; + +const polygonGeometry: GeoJSON.Polygon = { + type: 'Polygon', + coordinates: [[[0, 0], [1, 1], [0, 1], [0, 0]]] +}; + +const multiPolygonGeometry: GeoJSON.MultiPolygon = { + type: 'MultiPolygon', + coordinates: [[[[0, 0], [1, 1], [0, 1], [0, 0]]]] +}; + +const geometryCollection: GeoJSON.GeometryCollection = { + type: 'GeometryCollection', + geometries: [pointGeometry, lineStringGeometry] +}; + +const pointFeature: GeoJSON.Feature = { + type: 'Feature', + properties: { a: 1}, + geometry: pointGeometry +}; + +// pointGeometry +meta.coordEach(pointGeometry, coords => { + const equal: Array = coords +}) + +// lineStringGeometry +meta.coordEach(lineStringGeometry, coords => { + const equal: Array> = coords +}) + +// polygonGeometry +meta.coordEach(polygonGeometry, coords => { + const equal: Array>> = coords +}) + +// multiPolygonGeometry +meta.coordEach(multiPolygonGeometry, coords => { + const equal: Array>> = coords +}) + +// geometryCollection +meta.coordEach(geometryCollection, coords => { + const equal: Array>> = coords +}) + +// pointFeature +meta.coordEach(pointFeature, coords => { + const equal: Array = coords +}) + +// coordReduce +meta.coordReduce(pointFeature, (memo, coords) => { + const equal: Array = coords +}, 'foo') + +// propEach +meta.propEach(pointFeature, properties => { + const equal: any = properties +}) + +// coordAll +const coords: Array> = meta.coordAll(polygonGeometry) + +// geomEach +meta.geomEach(polygonGeometry, geom => { + const equal: GeoJSON.Polygon = geom +}) \ No newline at end of file