Skip to content

Commit

Permalink
Add Typescript definition for Meta
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Nov 26, 2016
1 parent 64aad55 commit 74140e8
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
72 changes: 72 additions & 0 deletions packages/turf-meta/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/// <reference types="geojson" />

type Points = GeoJSON.FeatureCollection<GeoJSON.Point>
type Point = GeoJSON.Feature<GeoJSON.Point> | GeoJSON.Point
type MultiPoints = GeoJSON.FeatureCollection<GeoJSON.MultiPoint>
type MultiPoint = GeoJSON.Feature<GeoJSON.MultiPoint> | GeoJSON.MultiPoint
type LineStrings = GeoJSON.FeatureCollection<GeoJSON.LineString>
type LineString = GeoJSON.Feature<GeoJSON.LineString> | GeoJSON.LineString
type MultiLineStrings = GeoJSON.FeatureCollection<GeoJSON.MultiLineString>
type MultiLineString = GeoJSON.Feature<GeoJSON.MultiLineString> | GeoJSON.MultiLineString
type Polygons = GeoJSON.FeatureCollection<GeoJSON.Polygon>
type Polygon = GeoJSON.Feature<GeoJSON.Polygon> | GeoJSON.Polygon
type MultiPolygons = GeoJSON.FeatureCollection<GeoJSON.MultiPolygon>
type MultiPolygon = GeoJSON.Feature<GeoJSON.MultiPolygon> | GeoJSON.MultiPolygon
type Feature = GeoJSON.Feature<any>
type Features = GeoJSON.FeatureCollection<any>
type GeometryCollection = GeoJSON.GeometryCollection
type GeometryObject = GeoJSON.GeometryObject

interface MetaStatic {
/**
* http://turfjs.org/docs/#coordEach
*/
coordEach(layer: Points | Point | MultiPoint | MultiPoints, callback: (coords: Array<number>) => void, excludeWrapCoord?: boolean): void;
coordEach(layer: LineStrings | LineString | MultiLineString | MultiLineStrings, callback: (coords: Array<Array<number>>) => void, excludeWrapCoord?: boolean): void;
coordEach(layer: Polygons | Polygon | MultiPolygons | MultiPolygon, callback: (coords: Array<Array<Array<number>>>) => void, excludeWrapCoord?: boolean): void;
coordEach(layer: GeometryCollection | GeometryObject, callback: (coords: Array<any>) => void, excludeWrapCoord?: boolean): void;

/**
* http://turfjs.org/docs/#coordEach
*/
coordReduce(layer: Points | Point | MultiPoint | MultiPoints, callback: (memo: any, coords: Array<number>) => void, memo: any, excludeWrapCoord?: boolean): any;
coordReduce(layer: LineStrings | LineString | MultiLineString | MultiLineStrings, callback: (memo: any, coords: Array<Array<number>>) => void, memo: any, excludeWrapCoord?: boolean): any;
coordReduce(layer: Polygons | Polygon | MultiPolygons | MultiPolygon, callback: (memo: any, coords: Array<Array<Array<number>>>) => void, memo: any, excludeWrapCoord?: boolean): any;
coordReduce(layer: GeometryCollection | GeometryObject, callback: (memo: any, coords: Array<any>) => 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<Array<number>>

/**
* 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
80 changes: 80 additions & 0 deletions packages/turf-meta/test-types.ts
Original file line number Diff line number Diff line change
@@ -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<GeoJSON.Point> = {
type: 'Feature',
properties: { a: 1},
geometry: pointGeometry
};

// pointGeometry
meta.coordEach(pointGeometry, coords => {
const equal: Array<number> = coords
})

// lineStringGeometry
meta.coordEach(lineStringGeometry, coords => {
const equal: Array<Array<number>> = coords
})

// polygonGeometry
meta.coordEach(polygonGeometry, coords => {
const equal: Array<Array<Array<number>>> = coords
})

// multiPolygonGeometry
meta.coordEach(multiPolygonGeometry, coords => {
const equal: Array<Array<Array<number>>> = coords
})

// geometryCollection
meta.coordEach(geometryCollection, coords => {
const equal: Array<Array<Array<number>>> = coords
})

// pointFeature
meta.coordEach(pointFeature, coords => {
const equal: Array<number> = coords
})

// coordReduce
meta.coordReduce(pointFeature, (memo, coords) => {
const equal: Array<number> = coords
}, 'foo')

// propEach
meta.propEach(pointFeature, properties => {
const equal: any = properties
})

// coordAll
const coords: Array<Array<number>> = meta.coordAll(polygonGeometry)

// geomEach
meta.geomEach(polygonGeometry, geom => {
const equal: GeoJSON.Polygon = geom
})

0 comments on commit 74140e8

Please sign in to comment.