diff --git a/packages/turf-helpers/index.ts b/packages/turf-helpers/index.ts index b4fc01ee5..55538fc22 100644 --- a/packages/turf-helpers/index.ts +++ b/packages/turf-helpers/index.ts @@ -598,15 +598,20 @@ export function multiPolygon

( * // => collection */ export function geometryCollection< + T extends + | Point + | LineString + | Polygon + | MultiPoint + | MultiLineString + | MultiPolygon, P extends GeoJsonProperties = GeoJsonProperties, >( - geometries: Array< - Point | LineString | Polygon | MultiPoint | MultiLineString | MultiPolygon - >, + geometries: Array, properties?: P, options: { bbox?: BBox; id?: Id } = {} -): Feature { - const geom: GeometryCollection = { +): Feature, P> { + const geom: GeometryCollection = { type: "GeometryCollection", geometries, }; diff --git a/packages/turf-line-chunk/index.ts b/packages/turf-line-chunk/index.ts index 34d8e3da6..41a6770d8 100644 --- a/packages/turf-line-chunk/index.ts +++ b/packages/turf-line-chunk/index.ts @@ -15,7 +15,7 @@ import { * If the line is shorter than the segment length then the original line is returned. * * @function - * @param {FeatureCollection|Geometry|Feature} geojson the lines to split + * @param {FeatureCollection|Geometry|Feature} geojson the LineString or MultiLineStrings to split * @param {number} segmentLength how long to make each segment * @param {Object} [options={}] Optional parameters * @param {Units} [options.units='kilometers'] Supports all valid Turf {@link https://turfjs.org/docs/api/types/Units Units} @@ -34,8 +34,8 @@ function lineChunk( | Feature | FeatureCollection | T - | GeometryCollection - | Feature, + | GeometryCollection + | Feature>, segmentLength: number, options: { units?: Units; @@ -57,6 +57,12 @@ function lineChunk( // Flatten each feature to simple LineString flattenEach(geojson, (feature: Feature) => { + if (feature.geometry.type !== "LineString") { + throw new Error( + "Only LineString and MultiLineString geometry types are supported" + ); + } + // reverses coordinates to start the first chunked segment at the end if (reverse) { feature.geometry.coordinates = feature.geometry.coordinates.reverse(); diff --git a/packages/turf-line-chunk/test.ts b/packages/turf-line-chunk/test.ts index 45a61f8de..8a27f1f6c 100644 --- a/packages/turf-line-chunk/test.ts +++ b/packages/turf-line-chunk/test.ts @@ -106,6 +106,57 @@ test("turf-line-chunk: Prevent input mutation", (t) => { t.end(); }); +test("turf-line-chunk: Validate behavior on unsupported geometry types", (t) => { + // Point | MultiPoint | LineString | MultiLineString | Polygon | MultiPolygon | GeometryCollection; + + // each of these `as any` casts are required to bypass the TypeScript rejection and actually test the validation underneath + t.throws(() => { + lineChunk({ type: "Point", geometry: [0, 0] } as any, 10); + }, "Point geometries are rejected"); + + t.throws(() => { + lineChunk({ type: "MultiPoint", geometry: [[0, 0]] } as any, 10); + }, "MultiPoint geometries are rejected"); + + t.throws(() => { + lineChunk( + { + type: "Polygon", + geometry: [ + [ + [0, 0], + [1, 0], + [1, 1], + [0, 0], + ], + ], + } as any, + 10 + ); + }, "Polygon geometries are rejected"); + + t.throws(() => { + lineChunk( + { + type: "MultiPolygon", + geometry: [ + [ + [ + [0, 0], + [1, 0], + [1, 1], + [0, 0], + ], + ], + ], + } as any, + 10 + ); + }, "MultiPolygons geometries are rejected"); + + t.end(); +}); + /** * Colorize FeatureCollection *