From 0adc274678b0ab148998451ab8982b7bda35bec9 Mon Sep 17 00:00:00 2001 From: Denis Date: Mon, 8 May 2017 18:48:35 -0400 Subject: [PATCH 1/3] Update typescript definition & JSDocs --- packages/turf-meta/README.md | 22 +++---- packages/turf-meta/index.d.ts | 52 ++++++---------- packages/turf-meta/index.js | 102 +++++++++++++++---------------- packages/turf-meta/test/types.ts | 11 ++-- 4 files changed, 87 insertions(+), 100 deletions(-) diff --git a/packages/turf-meta/README.md b/packages/turf-meta/README.md index 7f3d41b07e..850cd116f2 100644 --- a/packages/turf-meta/README.md +++ b/packages/turf-meta/README.md @@ -6,7 +6,7 @@ Iterate over coordinates in any GeoJSON object, similar to Array.forEach() **Parameters** -- `layer` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** any GeoJSON object +- `geojson` **([FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) \| [Geometry](http://geojson.org/geojson-spec.html#geometry) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<any>)** any GeoJSON object - `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** a method that takes (currentCoords, currentIndex) - `excludeWrapCoord` **\[[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)]** whether or not to include the final coordinate of LinearRings that wraps the ring in its iteration. (optional, default `false`) @@ -46,7 +46,7 @@ Reduce coordinates in any GeoJSON object, similar to Array.reduce() **Parameters** -- `layer` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** any GeoJSON object +- `geojson` **([FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) \| [Geometry](http://geojson.org/geojson-spec.html#geometry) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<any>)** any GeoJSON object - `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** a method that takes (previousValue, currentCoords, currentIndex) - `initialValue` **\[Any]** Value to use as the first argument to the first call of the callback. - `excludeWrapCoord` **\[[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)]** whether or not to include @@ -92,7 +92,7 @@ Iterate over properties in any GeoJSON object, similar to Array.forEach() **Parameters** -- `layer` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** any GeoJSON object +- `geojson` **([FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<any>)** any GeoJSON object - `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** a method that takes (currentProperties, currentIndex) **Examples** @@ -133,7 +133,7 @@ the reduction, so an array of all properties is unnecessary. **Parameters** -- `layer` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** any GeoJSON object +- `geojson` **([FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<any>)** any GeoJSON object - `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** a method that takes (previousValue, currentProperties, currentIndex) - `initialValue` **\[Any]** Value to use as the first argument to the first call of the callback. @@ -178,7 +178,7 @@ Array.forEach. **Parameters** -- `layer` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** any GeoJSON object +- `geojson` **([Geometry](http://geojson.org/geojson-spec.html#geometry) \| [FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<any>)** any GeoJSON object - `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** a method that takes (currentFeature, currentIndex) **Examples** @@ -217,7 +217,7 @@ Reduce features in any GeoJSON object, similar to Array.reduce(). **Parameters** -- `layer` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** any GeoJSON object +- `geojson` **([Geometry](http://geojson.org/geojson-spec.html#geometry) \| [FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<any>)** any GeoJSON object - `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** a method that takes (previousValue, currentFeature, currentIndex) - `initialValue` **\[Any]** Value to use as the first argument to the first call of the callback. @@ -261,7 +261,7 @@ Get all coordinates from any GeoJSON object. **Parameters** -- `layer` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** any GeoJSON object +- `geojson` **([Geometry](http://geojson.org/geojson-spec.html#geometry) \| [FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<any>)** any GeoJSON object **Examples** @@ -299,7 +299,7 @@ Iterate over each geometry in any GeoJSON object, similar to Array.forEach() **Parameters** -- `layer` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** any GeoJSON object +- `geojson` **([Geometry](http://geojson.org/geojson-spec.html#geometry) \| [FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<any>)** any GeoJSON object - `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** a method that takes (currentGeometry, currentIndex, currentProperties) **Examples** @@ -339,7 +339,7 @@ Reduce geometry in any GeoJSON object, similar to Array.reduce(). **Parameters** -- `layer` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** any GeoJSON object +- `geojson` **([Geometry](http://geojson.org/geojson-spec.html#geometry) \| [FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<any>)** any GeoJSON object - `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** a method that takes (previousValue, currentGeometry, currentIndex, currentProperties) - `initialValue` **\[Any]** Value to use as the first argument to the first call of the callback. @@ -384,7 +384,7 @@ Array.forEach. **Parameters** -- `layer` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** any GeoJSON object +- `geojson` **([Geometry](http://geojson.org/geojson-spec.html#geometry) \| [FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<any>)** any GeoJSON object - `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** a method that takes (currentFeature, currentIndex, currentSubIndex) **Examples** @@ -424,7 +424,7 @@ Reduce flattened features in any GeoJSON object, similar to Array.reduce(). **Parameters** -- `layer` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** any GeoJSON object +- `geojson` **([Geometry](http://geojson.org/geojson-spec.html#geometry) \| [FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<any>)** any GeoJSON object - `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** a method that takes (previousValue, currentFeature, currentIndex, currentSubIndex) - `initialValue` **\[Any]** Value to use as the first argument to the first call of the callback. diff --git a/packages/turf-meta/index.d.ts b/packages/turf-meta/index.d.ts index ea8af47c4a..7572d6d29c 100644 --- a/packages/turf-meta/index.d.ts +++ b/packages/turf-meta/index.d.ts @@ -10,74 +10,60 @@ export type Features = GeoJSON.FeatureCollection = GeoJSON.Feature; export type GeometryObject = GeoJSON.GeometryObject; export type GeometryCollection = GeoJSON.GeometryCollection; -export type Geoms = GeoJSON.Point | GeoJSON.LineString | GeoJSON.Polygon | GeoJSON.MultiPoint | GeoJSON.MultiLineString | GeoJSON.MultiPolygon | GeometryObject; -export type Position = GeoJSON.Position; +export type Geoms = Point | LineString | Polygon | MultiPoint | MultiLineString | MultiPolygon | GeometryObject; +export type AllGeoJSON = Feature | Features | GeometryObject | GeometryCollection; -interface CoordReduce { - /** - * http://turfjs.org/docs/#coordreduce - */ - (layer: Feature | Features | Geom, callback: (previousValue: any, currentCoords: Position, currentIndex: number) => void, initialValue?: any): void; - (layer: Feature | Features | Geom, callback: (previousValue: any, currentCoords: Position[], currentIndex: number) => void, initialValue?: any): void; - (layer: Feature | Features | Geom, callback: (previousValue: any, currentCoords: Position[][], currentIndex: number) => void, initialValue?: any): void; - (layer: Feature | Features | Geom, callback: (previousValue: any, currentCoords: Position[][][], currentIndex: number) => void, initialValue?: any): void; - (layer: Feature | Features | GeometryObject | GeometryCollection, callback: (previousValue: any, currentCoords: any[], currentIndex: number) => void, initialValue?: any): void; -} -export const coordReduce: CoordReduce; +/** + * http://turfjs.org/docs/#coordreduce + */ +export function coordReduce(geojson: AllGeoJSON, callback: (previousValue: any, currentCoords: number[], currentIndex: number) => void, initialValue?: any): void; -interface CoordEach { - /** - * http://turfjs.org/docs/#coordeach - */ - (layer: Feature | Features | Geom, callback: (currentCoords: Position, currentIndex: number) => void): void; - (layer: Feature | Features | Geom, callback: (currentCoords: Position[], currentIndex: number) => void): void; - (layer: Feature | Features | Geom, callback: (currentCoords: Position[][], currentIndex: number) => void): void; - (layer: Feature | Features | Geom, callback: (currentCoords: Position[][][], currentIndex: number) => void): void; - (layer: Feature | Features | GeometryObject | GeometryCollection, callback: (currentCoords: any[], currentIndex: number) => void): void; -} -export const coordEach: CoordEach; +/** + * http://turfjs.org/docs/#coordeach + */ +export function coordEach(geojson: AllGeoJSON, callback: (currentCoords: number[], currentIndex: number) => void): void; /** * http://turfjs.org/docs/#propeach */ -export function propEach(layer: Feature | Features | Geoms | GeometryCollection, callback: (currentProperties: Props, currentIndex: number) => void): void; +export function propEach(geojson: Feature | Features, callback: (currentProperties: Props, currentIndex: number) => void): void; /** * http://turfjs.org/docs/#propreduce */ -export function propReduce(layer: Feature | Features | Geoms | GeometryCollection, callback: (previousValue: any, currentProperties: Props, currentIndex: number) => void, initialValue?: any): any; +export function propReduce(geojson: Feature | Features, callback: (previousValue: any, currentProperties: Props, currentIndex: number) => void, initialValue?: any): any; /** * http://turfjs.org/docs/#featurereduce */ -export function featureReduce(layer: Feature | Features, callback: (previousValue: any, currentFeature: Feature, currentIndex: number) => void, initialValue?: any): void; +export function featureReduce(geojson: Feature | Features, callback: (previousValue: any, currentFeature: Feature, currentIndex: number) => void, initialValue?: any): void; /** * http://turfjs.org/docs/#featureeach */ -export function featureEach(layer: Feature | Features, callback: (currentFeature: Feature, currentIndex: number) => void): void; +export function featureEach(geojson: Feature | Features, callback: (currentFeature: Feature, currentIndex: number) => void): void; /** * http://turfjs.org/docs/#coordall */ -export function coordAll(layer: Feature | Features | Geoms | GeometryCollection): Position[]; +export function coordAll(geojson: AllGeoJSON): number[][]; /** * http://turfjs.org/docs/#geomreduce */ -export function geomReduce(layer: Feature | Features | Geoms | GeometryCollection, callback: (previousValue: any, currentGeometry: Geom, currentIndex: number, currentProperties: any) => void, initialValue?: any): void; +export function geomReduce(geojson: Feature | Features | Geoms | GeometryCollection, callback: (previousValue: any, currentGeometry: Geom, currentIndex: number, currentProperties: any) => void, initialValue?: any): void; /** * http://turfjs.org/docs/#geomeach */ -export function geomEach(layer: Feature | Features | Geoms | GeometryCollection, callback: (currentGeometry: Geom, currentIndex: number, currentProperties: any) => void): void; +export function geomEach(geojson: Feature | Features | Geoms | GeometryCollection, callback: (currentGeometry: Geom, currentIndex: number, currentProperties: any) => void): void; /** * http://turfjs.org/docs/#flattenreduce */ -export function flattenReduce(layer: Feature | Features | Geoms | GeometryCollection, callback: (previousValue: any, currentGeometry: Feature, currentIndex: number, currentProperties: any) => void, initialValue?: any): void; +export function flattenReduce(geojson: Feature | Features | Geoms | GeometryCollection, callback: (previousValue: any, currentFeature: Feature, currentIndex: number, currentSubIndex: number) => void, initialValue?: any): void; /** * http://turfjs.org/docs/#flatteneach */ -export function flattenEach(layer: Feature | Features | Geoms | GeometryCollection, callback: (currentGeometry: Feature, currentIndex: number, currentProperties: any) => void): void; +export function flattenEach(geojson: Feature | Features | Geoms | GeometryCollection, callback: (currentFeature: Feature, currentIndex: number, currentSubIndex: number) => void): void; diff --git a/packages/turf-meta/index.js b/packages/turf-meta/index.js index 20b63fe28f..5b49e5651d 100644 --- a/packages/turf-meta/index.js +++ b/packages/turf-meta/index.js @@ -3,7 +3,7 @@ * * @private * @callback coordEachCallback - * @param {[number, number]} currentCoords The current coordinates being processed. + * @param {Array} currentCoords The current coordinates being processed. * @param {number} currentIndex The index of the current element being processed in the * array.Starts at index 0, if an initialValue is provided, and at index 1 otherwise. */ @@ -12,7 +12,7 @@ * Iterate over coordinates in any GeoJSON object, similar to Array.forEach() * * @name coordEach - * @param {Object} layer any GeoJSON object + * @param {FeatureCollection|Geometry|Feature} geojson any GeoJSON object * @param {Function} callback a method that takes (currentCoords, currentIndex) * @param {boolean} [excludeWrapCoord=false] whether or not to include the final coordinate of LinearRings that wraps the ring in its iteration. * @example @@ -42,15 +42,15 @@ * //=currentIndex * }); */ -function coordEach(layer, callback, excludeWrapCoord) { +function coordEach(geojson, callback, excludeWrapCoord) { var i, j, k, g, l, geometry, stopG, coords, geometryMaybeCollection, wrapShrink = 0, currentIndex = 0, isGeometryCollection, - isFeatureCollection = layer.type === 'FeatureCollection', - isFeature = layer.type === 'Feature', - stop = isFeatureCollection ? layer.features.length : 1; + isFeatureCollection = geojson.type === 'FeatureCollection', + isFeature = geojson.type === 'Feature', + stop = isFeatureCollection ? geojson.features.length : 1; // This logic may look a little weird. The reason why it is that way // is because it's trying to be fast. GeoJSON supports multiple kinds @@ -66,8 +66,8 @@ function coordEach(layer, callback, excludeWrapCoord) { // be required with the normalization approach. for (i = 0; i < stop; i++) { - geometryMaybeCollection = (isFeatureCollection ? layer.features[i].geometry : - (isFeature ? layer.geometry : layer)); + geometryMaybeCollection = (isFeatureCollection ? geojson.features[i].geometry : + (isFeature ? geojson.geometry : geojson)); isGeometryCollection = geometryMaybeCollection.type === 'GeometryCollection'; stopG = isGeometryCollection ? geometryMaybeCollection.geometries.length : 1; @@ -138,7 +138,7 @@ function coordEach(layer, callback, excludeWrapCoord) { * Reduce coordinates in any GeoJSON object, similar to Array.reduce() * * @name coordReduce - * @param {Object} layer any GeoJSON object + * @param {FeatureCollection|Geometry|Feature} geojson any GeoJSON object * @param {Function} callback a method that takes (previousValue, currentCoords, currentIndex) * @param {*} [initialValue] Value to use as the first argument to the first call of the callback. * @param {boolean} [excludeWrapCoord=false] whether or not to include @@ -173,9 +173,9 @@ function coordEach(layer, callback, excludeWrapCoord) { * return currentCoords; * }); */ -function coordReduce(layer, callback, initialValue, excludeWrapCoord) { +function coordReduce(geojson, callback, initialValue, excludeWrapCoord) { var previousValue = initialValue; - coordEach(layer, function (currentCoords, currentIndex) { + coordEach(geojson, function (currentCoords, currentIndex) { if (currentIndex === 0 && initialValue === undefined) { previousValue = currentCoords; } else { @@ -199,7 +199,7 @@ function coordReduce(layer, callback, initialValue, excludeWrapCoord) { * Iterate over properties in any GeoJSON object, similar to Array.forEach() * * @name propEach - * @param {Object} layer any GeoJSON object + * @param {FeatureCollection|Feature} geojson any GeoJSON object * @param {Function} callback a method that takes (currentProperties, currentIndex) * @example * var features = { @@ -228,16 +228,16 @@ function coordReduce(layer, callback, initialValue, excludeWrapCoord) { * //=currentIndex * }); */ -function propEach(layer, callback) { +function propEach(geojson, callback) { var i; - switch (layer.type) { + switch (geojson.type) { case 'FeatureCollection': - for (i = 0; i < layer.features.length; i++) { - callback(layer.features[i].properties, i); + for (i = 0; i < geojson.features.length; i++) { + callback(geojson.features[i].properties, i); } break; case 'Feature': - callback(layer.properties, 0); + callback(geojson.properties, 0); break; } } @@ -272,7 +272,7 @@ function propEach(layer, callback) { * the reduction, so an array of all properties is unnecessary. * * @name propReduce - * @param {Object} layer any GeoJSON object + * @param {FeatureCollection|Feature} geojson any GeoJSON object * @param {Function} callback a method that takes (previousValue, currentProperties, currentIndex) * @param {*} [initialValue] Value to use as the first argument to the first call of the callback. * @returns {*} The value that results from the reduction. @@ -305,9 +305,9 @@ function propEach(layer, callback) { * return currentProperties * }); */ -function propReduce(layer, callback, initialValue) { +function propReduce(geojson, callback, initialValue) { var previousValue = initialValue; - propEach(layer, function (currentProperties, currentIndex) { + propEach(geojson, function (currentProperties, currentIndex) { if (currentIndex === 0 && initialValue === undefined) { previousValue = currentProperties; } else { @@ -332,7 +332,7 @@ function propReduce(layer, callback, initialValue) { * Array.forEach. * * @name featureEach - * @param {Object} layer any GeoJSON object + * @param {Geometry|FeatureCollection|Feature} geojson any GeoJSON object * @param {Function} callback a method that takes (currentFeature, currentIndex) * @example * var features = { @@ -361,12 +361,12 @@ function propReduce(layer, callback, initialValue) { * //=currentIndex * }); */ -function featureEach(layer, callback) { - if (layer.type === 'Feature') { - callback(layer, 0); - } else if (layer.type === 'FeatureCollection') { - for (var i = 0; i < layer.features.length; i++) { - callback(layer.features[i], i); +function featureEach(geojson, callback) { + if (geojson.type === 'Feature') { + callback(geojson, 0); + } else if (geojson.type === 'FeatureCollection') { + for (var i = 0; i < geojson.features.length; i++) { + callback(geojson.features[i], i); } } } @@ -398,7 +398,7 @@ function featureEach(layer, callback) { * Reduce features in any GeoJSON object, similar to Array.reduce(). * * @name featureReduce - * @param {Object} layer any GeoJSON object + * @param {Geometry|FeatureCollection|Feature} geojson any GeoJSON object * @param {Function} callback a method that takes (previousValue, currentFeature, currentIndex) * @param {*} [initialValue] Value to use as the first argument to the first call of the callback. * @returns {*} The value that results from the reduction. @@ -431,9 +431,9 @@ function featureEach(layer, callback) { * return currentFeature * }); */ -function featureReduce(layer, callback, initialValue) { +function featureReduce(geojson, callback, initialValue) { var previousValue = initialValue; - featureEach(layer, function (currentFeature, currentIndex) { + featureEach(geojson, function (currentFeature, currentIndex) { if (currentIndex === 0 && initialValue === undefined) { previousValue = currentFeature; } else { @@ -447,7 +447,7 @@ function featureReduce(layer, callback, initialValue) { * Get all coordinates from any GeoJSON object. * * @name coordAll - * @param {Object} layer any GeoJSON object + * @param {Geometry|FeatureCollection|Feature} geojson any GeoJSON object * @returns {Array>} coordinate position array * @example * var features = { @@ -474,9 +474,9 @@ function featureReduce(layer, callback, initialValue) { * var coords = turf.coordAll(features); * //=coords */ -function coordAll(layer) { +function coordAll(geojson) { var coords = []; - coordEach(layer, function (coord) { + coordEach(geojson, function (coord) { coords.push(coord); }); return coords; @@ -486,7 +486,7 @@ function coordAll(layer) { * Iterate over each geometry in any GeoJSON object, similar to Array.forEach() * * @name geomEach - * @param {Object} layer any GeoJSON object + * @param {Geometry|FeatureCollection|Feature} geojson any GeoJSON object * @param {Function} callback a method that takes (currentGeometry, currentIndex, currentProperties) * @example * var features = { @@ -516,15 +516,15 @@ function coordAll(layer) { * //=currentProperties * }); */ -function geomEach(layer, callback) { +function geomEach(geojson, callback) { var i, j, g, geometry, stopG, geometryMaybeCollection, isGeometryCollection, geometryProperties, currentIndex = 0, - isFeatureCollection = layer.type === 'FeatureCollection', - isFeature = layer.type === 'Feature', - stop = isFeatureCollection ? layer.features.length : 1; + isFeatureCollection = geojson.type === 'FeatureCollection', + isFeature = geojson.type === 'Feature', + stop = isFeatureCollection ? geojson.features.length : 1; // This logic may look a little weird. The reason why it is that way // is because it's trying to be fast. GeoJSON supports multiple kinds @@ -540,10 +540,10 @@ function geomEach(layer, callback) { // be required with the normalization approach. for (i = 0; i < stop; i++) { - geometryMaybeCollection = (isFeatureCollection ? layer.features[i].geometry : - (isFeature ? layer.geometry : layer)); - geometryProperties = (isFeatureCollection ? layer.features[i].properties : - (isFeature ? layer.properties : {})); + geometryMaybeCollection = (isFeatureCollection ? geojson.features[i].geometry : + (isFeature ? geojson.geometry : geojson)); + geometryProperties = (isFeatureCollection ? geojson.features[i].properties : + (isFeature ? geojson.properties : {})); isGeometryCollection = geometryMaybeCollection.type === 'GeometryCollection'; stopG = isGeometryCollection ? geometryMaybeCollection.geometries.length : 1; @@ -599,7 +599,7 @@ function geomEach(layer, callback) { * Reduce geometry in any GeoJSON object, similar to Array.reduce(). * * @name geomReduce - * @param {Object} layer any GeoJSON object + * @param {Geometry|FeatureCollection|Feature} geojson any GeoJSON object * @param {Function} callback a method that takes (previousValue, currentGeometry, currentIndex, currentProperties) * @param {*} [initialValue] Value to use as the first argument to the first call of the callback. * @returns {*} The value that results from the reduction. @@ -632,9 +632,9 @@ function geomEach(layer, callback) { * return currentGeometry * }); */ -function geomReduce(layer, callback, initialValue) { +function geomReduce(geojson, callback, initialValue) { var previousValue = initialValue; - geomEach(layer, function (currentGeometry, currentIndex, currentProperties) { + geomEach(geojson, function (currentGeometry, currentIndex, currentProperties) { if (currentIndex === 0 && initialValue === undefined) { previousValue = currentGeometry; } else { @@ -661,7 +661,7 @@ function geomReduce(layer, callback, initialValue) { * Array.forEach. * * @name flattenEach - * @param {Object} layer any GeoJSON object + * @param {Geometry|FeatureCollection|Feature} geojson any GeoJSON object * @param {Function} callback a method that takes (currentFeature, currentIndex, currentSubIndex) * @example * var features = { @@ -691,8 +691,8 @@ function geomReduce(layer, callback, initialValue) { * //=currentSubIndex * }); */ -function flattenEach(layer, callback) { - geomEach(layer, function (geometry, index, properties) { +function flattenEach(geojson, callback) { + geomEach(geojson, function (geometry, index, properties) { // Callback for single geometry switch (geometry.type) { @@ -758,7 +758,7 @@ function flattenEach(layer, callback) { * Reduce flattened features in any GeoJSON object, similar to Array.reduce(). * * @name flattenReduce - * @param {Object} layer any GeoJSON object + * @param {Geometry|FeatureCollection|Feature} geojson any GeoJSON object * @param {Function} callback a method that takes (previousValue, currentFeature, currentIndex, currentSubIndex) * @param {*} [initialValue] Value to use as the first argument to the first call of the callback. * @returns {*} The value that results from the reduction. @@ -792,9 +792,9 @@ function flattenEach(layer, callback) { * return currentFeature * }); */ -function flattenReduce(layer, callback, initialValue) { +function flattenReduce(geojson, callback, initialValue) { var previousValue = initialValue; - flattenEach(layer, function (currentFeature, currentIndex, currentSubIndex) { + flattenEach(geojson, function (currentFeature, currentIndex, currentSubIndex) { if (currentIndex === 0 && currentSubIndex === 0 && initialValue === undefined) { previousValue = currentFeature; } else { diff --git a/packages/turf-meta/test/types.ts b/packages/turf-meta/test/types.ts index 2594d793c9..45d6bb419e 100644 --- a/packages/turf-meta/test/types.ts +++ b/packages/turf-meta/test/types.ts @@ -14,10 +14,10 @@ const features = featureCollection([pt, line]) // coordEach meta.coordEach(pt, coords => coords) meta.coordEach(pt, (coords, index) => coords) -meta.coordEach(pt, coords => { const equal: number[] = coords }) -meta.coordEach(line, coords => { const equal: number[][] = coords }) -meta.coordEach(poly, coords => { const equal: number[][][] = coords }) -meta.coordEach(multiPoly, coords => { const equal: number[][][][] = coords }) +meta.coordEach(pt.geometry, coords => { const equal: number[] = coords }) +meta.coordEach(line, coords => { const equal: number[] = coords }) +meta.coordEach(poly, coords => { const equal: number[] = coords }) +meta.coordEach(multiPoly, coords => { const equal: number[] = coords }) // coordReduce meta.coordReduce(pt, (previous, coords) => coords) @@ -77,4 +77,5 @@ meta.flattenReduce(poly, (previous, feature, index, props) => feature) // flattenEach meta.flattenEach(poly, feature => feature) meta.flattenEach(features, feature => feature) -meta.flattenEach(poly, (feature, index, props) => feature) \ No newline at end of file +meta.flattenEach(poly, (feature, index, props) => feature) +meta.flattenEach(geomCollection, (feature, index, props) => feature) From b49ce34532d5ddf292120700681536137456dc61 Mon Sep 17 00:00:00 2001 From: Denis Date: Mon, 8 May 2017 19:11:04 -0400 Subject: [PATCH 2/3] Update GeometryCollection typescript definition --- packages/turf-helpers/index.d.ts | 6 +++++- packages/turf-helpers/test/types.ts | 7 +++++-- packages/turf-meta/index.d.ts | 14 +++++++------- packages/turf-meta/test/types.ts | 13 +++++++++++-- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/packages/turf-helpers/index.d.ts b/packages/turf-helpers/index.d.ts index 6bb15a3502..6b13b4cc87 100644 --- a/packages/turf-helpers/index.d.ts +++ b/packages/turf-helpers/index.d.ts @@ -23,6 +23,10 @@ export type GeometryObject = GeoJSON.GeometryObject; export type GeometryCollection = GeoJSON.GeometryCollection; export type Geoms = GeoJSON.Point | GeoJSON.LineString | GeoJSON.Polygon | GeoJSON.MultiPoint | GeoJSON.MultiLineString | GeoJSON.MultiPolygon; +export interface FeatureGeometryCollection extends GeoJSON.Feature { + geometry: GeometryCollection +} + /** * http://turfjs.org/docs/#feature */ @@ -70,7 +74,7 @@ export function multiPolygon(coordinates: Position[][][], properties?: any): Mul /** * http://turfjs.org/docs/#geometrycollection */ -export function geometryCollection(geometries: GeometryObject[], properties?: any): GeometryCollection; +export function geometryCollection(geometries: GeometryObject[], properties?: any): FeatureGeometryCollection; /** * http://turfjs.org/docs/#radianstodistance diff --git a/packages/turf-helpers/test/types.ts b/packages/turf-helpers/test/types.ts index 196b815e23..d0f4742bd6 100644 --- a/packages/turf-helpers/test/types.ts +++ b/packages/turf-helpers/test/types.ts @@ -12,7 +12,6 @@ feature({coordinates: [1, 0], type: 'point'}) multiLineString([[[0, 1], [2, 3], [0, 1]]]) multiPoint([[0, 1], [2, 3], [0, 1]]) multiPolygon([[[[0, 1], [2, 3], [0, 1]]]]) -geometryCollection([{coordinates: [1, 0], type: 'point'}]) radiansToDistance(5) distanceToRadians(10) distanceToDegrees(45) @@ -42,4 +41,8 @@ lines.features.push(line) // Collection with only Polygons const polygons = featureCollection([]) -polygons.features.push(poly) \ No newline at end of file +polygons.features.push(poly) + +// Geometry Collection +const geomCollection = geometryCollection([{coordinates: [1, 0], type: 'point'}]) +geomCollection.geometry.geometries diff --git a/packages/turf-meta/index.d.ts b/packages/turf-meta/index.d.ts index 7572d6d29c..b7819a42f7 100644 --- a/packages/turf-meta/index.d.ts +++ b/packages/turf-meta/index.d.ts @@ -10,7 +10,7 @@ export type Features = GeoJSON.FeatureCollection = GeoJSON.Feature; export type GeometryObject = GeoJSON.GeometryObject; export type GeometryCollection = GeoJSON.GeometryCollection; -export type Geoms = Point | LineString | Polygon | MultiPoint | MultiLineString | MultiPolygon | GeometryObject; +export type Geoms = Point | LineString | Polygon | MultiPoint | MultiLineString | MultiPolygon; export type AllGeoJSON = Feature | Features | GeometryObject | GeometryCollection; /** @@ -36,12 +36,12 @@ export function propReduce(geojson: Feature | Features(geojson: Feature | Features, callback: (previousValue: any, currentFeature: Feature, currentIndex: number) => void, initialValue?: any): void; +export function featureReduce(geojson: Feature | Features, callback: (previousValue: any, currentFeature: Feature, currentIndex: number) => void, initialValue?: any): void; /** * http://turfjs.org/docs/#featureeach */ -export function featureEach(geojson: Feature | Features, callback: (currentFeature: Feature, currentIndex: number) => void): void; +export function featureEach(geojson: Feature | Features, callback: (currentFeature: Feature, currentIndex: number) => void): void; /** * http://turfjs.org/docs/#coordall @@ -51,19 +51,19 @@ export function coordAll(geojson: AllGeoJSON): number[][]; /** * http://turfjs.org/docs/#geomreduce */ -export function geomReduce(geojson: Feature | Features | Geoms | GeometryCollection, callback: (previousValue: any, currentGeometry: Geom, currentIndex: number, currentProperties: any) => void, initialValue?: any): void; +export function geomReduce(geojson: Feature | Features | Geom | GeometryCollection, callback: (previousValue: any, currentGeometry: Geom, currentIndex: number, currentProperties: any) => void, initialValue?: any): void; /** * http://turfjs.org/docs/#geomeach */ -export function geomEach(geojson: Feature | Features | Geoms | GeometryCollection, callback: (currentGeometry: Geom, currentIndex: number, currentProperties: any) => void): void; +export function geomEach(geojson: Feature | Features | Geom | GeometryCollection, callback: (currentGeometry: Geom, currentIndex: number, currentProperties: any) => void): void; /** * http://turfjs.org/docs/#flattenreduce */ -export function flattenReduce(geojson: Feature | Features | Geoms | GeometryCollection, callback: (previousValue: any, currentFeature: Feature, currentIndex: number, currentSubIndex: number) => void, initialValue?: any): void; +export function flattenReduce(geojson: Feature | Features | Geom | GeometryCollection, callback: (previousValue: any, currentFeature: Feature, currentIndex: number, currentSubIndex: number) => void, initialValue?: any): void; /** * http://turfjs.org/docs/#flatteneach */ -export function flattenEach(geojson: Feature | Features | Geoms | GeometryCollection, callback: (currentFeature: Feature, currentIndex: number, currentSubIndex: number) => void): void; +export function flattenEach(geojson: Feature | Features | Geom | GeometryCollection, callback: (currentFeature: Feature, currentIndex: number, currentSubIndex: number) => void): void; diff --git a/packages/turf-meta/test/types.ts b/packages/turf-meta/test/types.ts index 45d6bb419e..c3c573c510 100644 --- a/packages/turf-meta/test/types.ts +++ b/packages/turf-meta/test/types.ts @@ -18,12 +18,14 @@ meta.coordEach(pt.geometry, coords => { const equal: number[] = coords }) meta.coordEach(line, coords => { const equal: number[] = coords }) meta.coordEach(poly, coords => { const equal: number[] = coords }) meta.coordEach(multiPoly, coords => { const equal: number[] = coords }) +meta.coordEach(geomCollection, coords => coords) // coordReduce meta.coordReduce(pt, (previous, coords) => coords) meta.coordReduce(pt, (previous, coords, index) => coords) meta.coordReduce(pt, (previous, coords, index) => coords, 0) meta.coordReduce(pt, (previous, coords) => { const equal: Array = coords }) +meta.coordReduce(geomCollection, (previous, coords) => coords) interface CustomProps { foo: string @@ -36,12 +38,14 @@ meta.propReduce(poly, (previous, prop) => prop, 0) meta.propReduce(features, (previous, prop) => prop) meta.propReduce(poly, (previous, prop, index) => prop) meta.propReduce(poly, (previous, prop) => prop.foo) +meta.propReduce(geomCollection, (previous, prop) => prop) // propEach meta.propEach(poly, prop => prop) meta.propEach(features, prop => prop) meta.propEach(poly, (prop, index) => prop) meta.propEach(poly, prop => prop.bar) +meta.propEach(geomCollection, prop => prop) // // coordAll const coords: Array> = meta.coordAll(poly) @@ -51,31 +55,36 @@ meta.featureReduce(poly, (previous, feature) => feature) meta.featureReduce(poly, (previous, feature) => feature, 0) meta.featureReduce(features, (previous, feature) => feature) meta.featureReduce(poly, (previous, feature, index) => feature) +// meta.featureReduce(geomCollection, (previous, feature, index) => feature) // featureEach meta.featureEach(poly, feature => feature) meta.featureEach(features, feature => feature) meta.featureEach(poly, (feature, index) => feature) +// meta.featureEach(geomCollection, (feature, index) => feature) // geomReduce meta.geomReduce(poly, (previous, geom) => geom) meta.geomReduce(poly, (previous, geom) => geom, 0) meta.geomReduce(features, (previous, geom) => geom) meta.geomReduce(poly, (previous, geom, index, props) => geom) +// meta.geomReduce(geomCollection, (previous, geom, index, props) => geom) // geomEach meta.geomEach(poly, geom => geom) meta.geomEach(features, geom => geom) meta.geomEach(poly, (geom, index, props) => geom) +// meta.geomEach(geomCollection, (geom, index, props) => geom) // flattenReduce meta.flattenReduce(poly, (previous, feature) => feature) meta.flattenReduce(poly, (previous, feature) => feature, 0) meta.flattenReduce(features, (previous, feature) => feature) meta.flattenReduce(poly, (previous, feature, index, props) => feature) +// meta.flattenReduce(geomCollection, (previous, feature, index, props) => feature) // flattenEach meta.flattenEach(poly, feature => feature) meta.flattenEach(features, feature => feature) -meta.flattenEach(poly, (feature, index, props) => feature) -meta.flattenEach(geomCollection, (feature, index, props) => feature) +meta.flattenEach(poly.geometry, (feature, index, props) => feature) +// meta.flattenEach(geomCollection, (feature, index, props) => feature) From e798e07cccd65540b2d54e1cfd097f9e23eeca88 Mon Sep 17 00:00:00 2001 From: Denis Date: Mon, 8 May 2017 19:17:49 -0400 Subject: [PATCH 3/3] Use flattenEach with @turf/flatten --- packages/turf-flatten/README.md | 8 +- packages/turf-flatten/index.js | 141 +++----------------------------- 2 files changed, 16 insertions(+), 133 deletions(-) diff --git a/packages/turf-flatten/README.md b/packages/turf-flatten/README.md index 8f336e54ad..5c41ebd98a 100644 --- a/packages/turf-flatten/README.md +++ b/packages/turf-flatten/README.md @@ -6,12 +6,12 @@ Flattens any [GeoJSON](GeoJSON) to a [FeatureCollection](http://geojson.org/geoj **Parameters** -- `geojson` **[Feature](http://geojson.org/geojson-spec.html#feature-objects)** any valid [GeoJSON](GeoJSON) with multi-geometry [Feature](http://geojson.org/geojson-spec.html#feature-objects)s +- `geojson` **([FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) \| [Geometry](http://geojson.org/geojson-spec.html#geometry) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<any>)** any valid GeoJSON Object **Examples** ```javascript -var geometry = { +var multiGeometry = { "type": "MultiPolygon", "coordinates": [ [[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]], @@ -20,13 +20,13 @@ var geometry = { ] }; -var flatten = turf.flatten(geometry); +var flatten = turf.flatten(multiGeometry); //addToMap var addToMap = [flatten] ``` -Returns **[FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects)** a flattened [FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) +Returns **[FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects)<any>** all Multi-Geometries are flattened into single Features