Skip to content

Commit 83b8db1

Browse files
authored
Improve propsEach TypeScript signature (#3162)
- Tighten `geojson` parameter type signatures to only Feature and FeatureCollection - Improve inference of `currentProperties` argument in callback - Expose `false` return from callback to TypeScript - Remove @ts-expect-errors - Modernize the loop syntax
1 parent 7c36bc9 commit 83b8db1

3 files changed

Lines changed: 26 additions & 17 deletions

File tree

packages/turf-meta/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,15 @@ Type: [Function][1]
122122
* `currentProperties` **[GeoJsonProperties][9]** The current Properties being processed.
123123
* `featureIndex` **[number][3]** The current index of the Feature being processed.
124124

125-
Returns **void** 
125+
Returns **(`false` | void)** Return false to stop iteration
126126

127127
## propEach
128128

129129
Iterate over properties in any GeoJSON object, similar to Array.forEach()
130130

131131
### Parameters
132132

133-
* `geojson` **([FeatureCollection][10] | [Feature][9])** any GeoJSON object
133+
* `geojson` **([FeatureCollection][10] | [Feature][9])** any GeoJSON Feature or FeatureCollection
134134
* `callback` **[propEachCallback][11]** a method that takes (currentProperties, featureIndex)
135135

136136
### Examples

packages/turf-meta/index.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -327,14 +327,14 @@ function coordReduce<Reducer>(
327327
* @callback propEachCallback
328328
* @param {GeoJsonProperties} currentProperties The current Properties being processed.
329329
* @param {number} featureIndex The current index of the Feature being processed.
330-
* @returns {void}
330+
* @returns {false | void} Return false to stop iteration
331331
*/
332332

333333
/**
334334
* Iterate over properties in any GeoJSON object, similar to Array.forEach()
335335
*
336336
* @function
337-
* @param {FeatureCollection|Feature} geojson any GeoJSON object
337+
* @param {FeatureCollection|Feature} geojson any GeoJSON Feature or FeatureCollection
338338
* @param {propEachCallback} callback a method that takes (currentProperties, featureIndex)
339339
* @returns {void}
340340
* @example
@@ -349,19 +349,16 @@ function coordReduce<Reducer>(
349349
* });
350350
*/
351351
function propEach<Props extends GeoJsonProperties>(
352-
geojson: Feature<any> | FeatureCollection<any> | Feature<GeometryCollection>,
353-
callback: (currentProperties: Props, featureIndex: number) => void
352+
geojson: Feature<Geometry, Props> | FeatureCollection<Geometry, Props>,
353+
callback: (currentProperties: Props, featureIndex: number) => false | void
354354
): void {
355-
var i;
356355
switch (geojson.type) {
357356
case "FeatureCollection":
358-
for (i = 0; i < geojson.features.length; i++) {
359-
// @ts-expect-error: Known type conflict
357+
for (let i = 0; i < geojson.features.length; i++) {
360358
if (callback(geojson.features[i].properties, i) === false) break;
361359
}
362360
break;
363361
case "Feature":
364-
// @ts-expect-error: Known type conflict
365362
callback(geojson.properties, 0);
366363
break;
367364
}

packages/turf-meta/types.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Point, LineString } from "geojson";
1+
import { Point, LineString, Polygon, Feature } from "geojson";
22
import * as helpers from "@turf/helpers";
33
import { featureCollection, point, lineString } from "@turf/helpers";
44
import * as meta from "./index.js";
@@ -128,12 +128,24 @@ meta.propReduce(geomCollection, (previous, prop) => prop);
128128
/**
129129
* meta.propEach
130130
*/
131-
const propEachValue: void = meta.propEach(poly, (prop) => prop);
132-
propEach(features, (prop) => prop);
133-
meta.propEach(features, (prop) => prop);
134-
meta.propEach(poly, (prop, index) => prop);
135-
meta.propEach<{ bar: string }>(poly, (prop) => prop.bar);
136-
meta.propEach(geomCollection, (prop) => prop);
131+
const propEachValue: void = meta.propEach(poly, (prop) => {
132+
prop;
133+
});
134+
propEach(features, (prop) => {
135+
prop;
136+
});
137+
meta.propEach(features, (prop) => {
138+
prop;
139+
});
140+
meta.propEach(poly, (prop, index) => {
141+
prop;
142+
});
143+
meta.propEach(poly as Feature<Polygon, { bar: string }>, (prop) => {
144+
prop.bar;
145+
});
146+
meta.propEach(geomCollection, (prop) => {
147+
prop;
148+
});
137149

138150
/**
139151
* meta.coordAll

0 commit comments

Comments
 (0)