Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions packages/turf-invariant/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ Get GeoJSON object's type, Geometry type is prioritize.
### Parameters

* `geojson` **[GeoJSON][7]** GeoJSON object
* `_name` **[string][8]?** 
* `name` **[string][8]** name of the variable to display in error message (unused) (optional, default `"geojson"`)

### Examples

Expand Down
24 changes: 11 additions & 13 deletions packages/turf-invariant/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ function getGeom<G extends Geometry>(geojson: Feature<G> | G): G {
* Get GeoJSON object's type, Geometry type is prioritize.
*
* @param {GeoJSON} geojson GeoJSON object
* @param {string} [name="geojson"] name of the variable to display in error message (unused)
* @returns {string} GeoJSON type
* @example
* var point = {
Expand All @@ -264,20 +263,19 @@ function getGeom<G extends Geometry>(geojson: Feature<G> | G): G {
* var geom = turf.getType(point)
* //="Point"
*/
function getType(
geojson: Feature<any> | FeatureCollection<any> | Geometry,
_name?: string
): string {
if (geojson.type === "FeatureCollection") {
return "FeatureCollection";
}
if (geojson.type === "GeometryCollection") {
return "GeometryCollection";
}
function getType<
T extends Geometry | Feature<Geometry | null> | FeatureCollection,
>(
geojson: T
): T extends Feature<infer G>
? G extends Geometry
? G["type"]
: "Feature"
: T["type"] {
if (geojson.type === "Feature" && geojson.geometry !== null) {
return geojson.geometry.type;
return geojson.geometry.type as any;
}
return geojson.type;
return geojson.type as any;
}

export {
Expand Down
6 changes: 5 additions & 1 deletion packages/turf-invariant/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import {
polygon,
featureCollection,
geometryCollection,
feature,
} from "@turf/helpers";
import * as invariant from "./index.js";
import { LineString, Point } from "geojson";

test("invariant -- containsNumber", (t) => {
t.equals(invariant.containsNumber([1, 1]), true);
Expand Down Expand Up @@ -367,13 +369,15 @@ test("invariant -- getType", (t) => {
[0, 1],
[1, 1],
]);
const collection = featureCollection([pt, line]);
const collection = featureCollection<Point | LineString>([pt, line]);
const geomCollection = geometryCollection([pt.geometry, line.geometry]);
const nullFeature = feature(null);

t.deepEqual(invariant.getType(pt), "Point");
t.deepEqual(invariant.getType(line.geometry), "LineString");
t.deepEqual(invariant.getType(geomCollection), "GeometryCollection");
t.deepEqual(invariant.getType(collection), "FeatureCollection");
t.deepEqual(invariant.getType(nullFeature), "Feature");
// t.throws(() => invariant.getType(null), /geojson is required/, 'geojson is required');
t.end();
});
Expand Down