Skip to content

Commit 4862998

Browse files
authored
Fix @turf/buffer types to allow for undefined returns when the inputs 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
1 parent 09e9ebd commit 4862998

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- [`@turf/union`](union) Accept FeatureCollection for multiple inputs (#2247)
1515
- [`@turf/difference`](difference) Accept FeatureCollection for multiple inputs (#2247)
1616
- [`@turf/intersect`](intersect) Accept FeatureCollection for multiple inputs (#2247)
17+
- [`@turf/buffer`](buffer) Add undefined return for when the geometry is invalid (#2613)
1718

1819
## 🏅 New Features/Enhancements
1920
- [`@turf/kinks`](kinks) Move to sweepline-intersections library for performance (#1896)

packages/turf-buffer/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ declare function buffer(
3131
| MultiPolygon,
3232
radius?: number,
3333
options?: Options
34-
): Feature<Polygon | MultiPolygon>;
34+
): Feature<Polygon | MultiPolygon> | undefined;
3535
declare function buffer(
3636
feature: FeatureCollection<GeometryObject> | GeometryCollection,
3737
radius?: number,
3838
options?: Options
39-
): FeatureCollection<Polygon | MultiPolygon>;
39+
): FeatureCollection<Polygon | MultiPolygon> | undefined;
4040

4141
export { buffer };
4242
export default buffer;

packages/turf-buffer/test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,32 @@ test("turf-buffer - morphological closing", (t) => {
156156
t.end();
157157
});
158158

159+
test("turf-buffer - undefined return", (t) => {
160+
const poly: GeoJSON.Feature<GeoJSON.Polygon> = {
161+
type: "Feature",
162+
properties: {},
163+
geometry: {
164+
type: "Polygon",
165+
coordinates: [
166+
[
167+
[-101.87842323574378, 52.250446362382775],
168+
[-101.87842323574378, 49.56446202085259],
169+
[-98.29404114999511, 49.56446202085259],
170+
[-98.29404114999511, 52.250446362382775],
171+
[-101.87842323574378, 52.250446362382775],
172+
],
173+
],
174+
},
175+
};
176+
177+
t.equal(
178+
buffer(poly, -100000000),
179+
undefined,
180+
"empty geometry should be undefined if the resulting geometry is invalid"
181+
);
182+
t.end();
183+
});
184+
159185
function colorize(feature, color) {
160186
color = color || "#F00";
161187
if (feature.properties) {

0 commit comments

Comments
 (0)