Skip to content

Commit

Permalink
Fix @turf/buffer types to allow for undefined returns when the inputs…
Browse files Browse the repository at this point in the history
… result in an invalid geometry (#2613)

* Fix @turf/buffer types to allow for undefined returns when the inputs result in an invalid geometry

* Add changelog entry, since this is technically a break
  • Loading branch information
mfedderly authored Jun 7, 2024
1 parent 09e9ebd commit 4862998
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- [`@turf/union`](union) Accept FeatureCollection for multiple inputs (#2247)
- [`@turf/difference`](difference) Accept FeatureCollection for multiple inputs (#2247)
- [`@turf/intersect`](intersect) Accept FeatureCollection for multiple inputs (#2247)
- [`@turf/buffer`](buffer) Add undefined return for when the geometry is invalid (#2613)

## 🏅 New Features/Enhancements
- [`@turf/kinks`](kinks) Move to sweepline-intersections library for performance (#1896)
Expand Down
4 changes: 2 additions & 2 deletions packages/turf-buffer/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ declare function buffer(
| MultiPolygon,
radius?: number,
options?: Options
): Feature<Polygon | MultiPolygon>;
): Feature<Polygon | MultiPolygon> | undefined;
declare function buffer(
feature: FeatureCollection<GeometryObject> | GeometryCollection,
radius?: number,
options?: Options
): FeatureCollection<Polygon | MultiPolygon>;
): FeatureCollection<Polygon | MultiPolygon> | undefined;

export { buffer };
export default buffer;
26 changes: 26 additions & 0 deletions packages/turf-buffer/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,32 @@ test("turf-buffer - morphological closing", (t) => {
t.end();
});

test("turf-buffer - undefined return", (t) => {
const poly: GeoJSON.Feature<GeoJSON.Polygon> = {
type: "Feature",
properties: {},
geometry: {
type: "Polygon",
coordinates: [
[
[-101.87842323574378, 52.250446362382775],
[-101.87842323574378, 49.56446202085259],
[-98.29404114999511, 49.56446202085259],
[-98.29404114999511, 52.250446362382775],
[-101.87842323574378, 52.250446362382775],
],
],
},
};

t.equal(
buffer(poly, -100000000),
undefined,
"empty geometry should be undefined if the resulting geometry is invalid"
);
t.end();
});

function colorize(feature, color) {
color = color || "#F00";
if (feature.properties) {
Expand Down

0 comments on commit 4862998

Please sign in to comment.