@turf/invariant 7.0.0-alpha.2 → 7.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -9
- package/dist/cjs/index.cjs +138 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +108 -0
- package/dist/{js → esm}/index.d.ts +12 -9
- package/dist/esm/index.js +138 -0
- package/dist/esm/index.js.map +1 -0
- package/package.json +30 -24
- package/dist/es/index.js +0 -222
- package/dist/es/package.json +0 -1
- package/dist/js/index.js +0 -232
package/README.md
CHANGED
|
@@ -174,26 +174,21 @@ Returns **[string][8]** GeoJSON type
|
|
|
174
174
|
|
|
175
175
|
[13]: https://tools.ietf.org/html/rfc7946#section-3.3
|
|
176
176
|
|
|
177
|
-
<!-- This file is automatically generated. Please don't edit it directly
|
|
178
|
-
if you find an error, edit the source file (likely index.js), and re-run
|
|
179
|
-
./scripts/generate-readmes in the turf project. -->
|
|
177
|
+
<!-- This file is automatically generated. Please don't edit it directly. If you find an error, edit the source file of the module in question (likely index.js or index.ts), and re-run "yarn docs" from the root of the turf project. -->
|
|
180
178
|
|
|
181
179
|
---
|
|
182
180
|
|
|
183
|
-
This module is part of the [Turfjs project](
|
|
184
|
-
module collection dedicated to geographic algorithms. It is maintained in the
|
|
185
|
-
[Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create
|
|
186
|
-
PRs and issues.
|
|
181
|
+
This module is part of the [Turfjs project](https://turfjs.org/), an open source module collection dedicated to geographic algorithms. It is maintained in the [Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create PRs and issues.
|
|
187
182
|
|
|
188
183
|
### Installation
|
|
189
184
|
|
|
190
|
-
Install this module individually:
|
|
185
|
+
Install this single module individually:
|
|
191
186
|
|
|
192
187
|
```sh
|
|
193
188
|
$ npm install @turf/invariant
|
|
194
189
|
```
|
|
195
190
|
|
|
196
|
-
Or install the
|
|
191
|
+
Or install the all-encompassing @turf/turf module that includes all modules as functions:
|
|
197
192
|
|
|
198
193
|
```sh
|
|
199
194
|
$ npm install @turf/turf
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// index.ts
|
|
5
|
+
var _helpers = require('@turf/helpers');
|
|
6
|
+
function getCoord(coord) {
|
|
7
|
+
if (!coord) {
|
|
8
|
+
throw new Error("coord is required");
|
|
9
|
+
}
|
|
10
|
+
if (!Array.isArray(coord)) {
|
|
11
|
+
if (coord.type === "Feature" && coord.geometry !== null && coord.geometry.type === "Point") {
|
|
12
|
+
return [...coord.geometry.coordinates];
|
|
13
|
+
}
|
|
14
|
+
if (coord.type === "Point") {
|
|
15
|
+
return [...coord.coordinates];
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
if (Array.isArray(coord) && coord.length >= 2 && !Array.isArray(coord[0]) && !Array.isArray(coord[1])) {
|
|
19
|
+
return [...coord];
|
|
20
|
+
}
|
|
21
|
+
throw new Error("coord must be GeoJSON Point or an Array of numbers");
|
|
22
|
+
}
|
|
23
|
+
__name(getCoord, "getCoord");
|
|
24
|
+
function getCoords(coords) {
|
|
25
|
+
if (Array.isArray(coords)) {
|
|
26
|
+
return coords;
|
|
27
|
+
}
|
|
28
|
+
if (coords.type === "Feature") {
|
|
29
|
+
if (coords.geometry !== null) {
|
|
30
|
+
return coords.geometry.coordinates;
|
|
31
|
+
}
|
|
32
|
+
} else {
|
|
33
|
+
if (coords.coordinates) {
|
|
34
|
+
return coords.coordinates;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
throw new Error(
|
|
38
|
+
"coords must be GeoJSON Feature, Geometry Object or an Array"
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
__name(getCoords, "getCoords");
|
|
42
|
+
function containsNumber(coordinates) {
|
|
43
|
+
if (coordinates.length > 1 && _helpers.isNumber.call(void 0, coordinates[0]) && _helpers.isNumber.call(void 0, coordinates[1])) {
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
if (Array.isArray(coordinates[0]) && coordinates[0].length) {
|
|
47
|
+
return containsNumber(coordinates[0]);
|
|
48
|
+
}
|
|
49
|
+
throw new Error("coordinates must only contain numbers");
|
|
50
|
+
}
|
|
51
|
+
__name(containsNumber, "containsNumber");
|
|
52
|
+
function geojsonType(value, type, name) {
|
|
53
|
+
if (!type || !name) {
|
|
54
|
+
throw new Error("type and name required");
|
|
55
|
+
}
|
|
56
|
+
if (!value || value.type !== type) {
|
|
57
|
+
throw new Error(
|
|
58
|
+
"Invalid input to " + name + ": must be a " + type + ", given " + value.type
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
__name(geojsonType, "geojsonType");
|
|
63
|
+
function featureOf(feature, type, name) {
|
|
64
|
+
if (!feature) {
|
|
65
|
+
throw new Error("No feature passed");
|
|
66
|
+
}
|
|
67
|
+
if (!name) {
|
|
68
|
+
throw new Error(".featureOf() requires a name");
|
|
69
|
+
}
|
|
70
|
+
if (!feature || feature.type !== "Feature" || !feature.geometry) {
|
|
71
|
+
throw new Error(
|
|
72
|
+
"Invalid input to " + name + ", Feature with geometry required"
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
if (!feature.geometry || feature.geometry.type !== type) {
|
|
76
|
+
throw new Error(
|
|
77
|
+
"Invalid input to " + name + ": must be a " + type + ", given " + feature.geometry.type
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
__name(featureOf, "featureOf");
|
|
82
|
+
function collectionOf(featureCollection, type, name) {
|
|
83
|
+
if (!featureCollection) {
|
|
84
|
+
throw new Error("No featureCollection passed");
|
|
85
|
+
}
|
|
86
|
+
if (!name) {
|
|
87
|
+
throw new Error(".collectionOf() requires a name");
|
|
88
|
+
}
|
|
89
|
+
if (!featureCollection || featureCollection.type !== "FeatureCollection") {
|
|
90
|
+
throw new Error(
|
|
91
|
+
"Invalid input to " + name + ", FeatureCollection required"
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
for (const feature of featureCollection.features) {
|
|
95
|
+
if (!feature || feature.type !== "Feature" || !feature.geometry) {
|
|
96
|
+
throw new Error(
|
|
97
|
+
"Invalid input to " + name + ", Feature with geometry required"
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
if (!feature.geometry || feature.geometry.type !== type) {
|
|
101
|
+
throw new Error(
|
|
102
|
+
"Invalid input to " + name + ": must be a " + type + ", given " + feature.geometry.type
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
__name(collectionOf, "collectionOf");
|
|
108
|
+
function getGeom(geojson) {
|
|
109
|
+
if (geojson.type === "Feature") {
|
|
110
|
+
return geojson.geometry;
|
|
111
|
+
}
|
|
112
|
+
return geojson;
|
|
113
|
+
}
|
|
114
|
+
__name(getGeom, "getGeom");
|
|
115
|
+
function getType(geojson, _name) {
|
|
116
|
+
if (geojson.type === "FeatureCollection") {
|
|
117
|
+
return "FeatureCollection";
|
|
118
|
+
}
|
|
119
|
+
if (geojson.type === "GeometryCollection") {
|
|
120
|
+
return "GeometryCollection";
|
|
121
|
+
}
|
|
122
|
+
if (geojson.type === "Feature" && geojson.geometry !== null) {
|
|
123
|
+
return geojson.geometry.type;
|
|
124
|
+
}
|
|
125
|
+
return geojson.type;
|
|
126
|
+
}
|
|
127
|
+
__name(getType, "getType");
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
exports.collectionOf = collectionOf; exports.containsNumber = containsNumber; exports.featureOf = featureOf; exports.geojsonType = geojsonType; exports.getCoord = getCoord; exports.getCoords = getCoords; exports.getGeom = getGeom; exports.getType = getType;
|
|
138
|
+
//# sour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,MAC/B;AAAA,IACF;AACA,QAAI,CAAC,QAAQ,YAAY,QAAQ,SAAS,SAAS,MAAM;AACvD,YAAM,IAAI;AAAA,QACR,sBACE,OACA,iBACA,OACA,aACA,QAAQ,SAAS;AAAA,MACrB;AAAA,IACF;AAAA,EACF;AACF;AAjCS;AAqDT,SAAS,QAA4B,SAA4B;AAC/D,MAAI,QAAQ,SAAS,WAAW;AAC9B,WAAO,QAAQ;AAAA,EACjB;AACA,SAAO;AACT;AALS;AAyBT,SAAS,QACP,SACA,OACQ;AACR,MAAI,QAAQ,SAAS,qBAAqB;AACxC,WAAO;AAAA,EACT;AACA,MAAI,QAAQ,SAAS,sBAAsB;AACzC,WAAO;AAAA,EACT;AACA,MAAI,QAAQ,SAAS,aAAa,QAAQ,aAAa,MAAM;AAC3D,WAAO,QAAQ,SAAS;AAAA,EAC1B;AACA,SAAO,QAAQ;AACjB;AAdS","sourcesContent":["import {\n Feature,\n FeatureCollection,\n Geometry,\n LineString,\n MultiPoint,\n MultiLineString,\n MultiPolygon,\n Point,\n Polygon,\n} from \"geojson\";\nimport { isNumber } from \"@turf/helpers\";\n\n/**\n * Unwrap a coordinate from a Point Feature, Geometry or a single coordinate.\n *\n * @name getCoord\n * @param {Array<number>|Geometry<Point>|Feature<Point>} coord GeoJSON Point or an Array of numbers\n * @returns {Array<number>} coordinates\n * @example\n * var pt = turf.point([10, 10]);\n *\n * var coord = turf.getCoord(pt);\n * //= [10, 10]\n */\nfunction getCoord(coord: Feature<Point> | Point | number[]): number[] {\n if (!coord) {\n throw new Error(\"coord is required\");\n }\n\n if (!Array.isArray(coord)) {\n if (\n coord.type === \"Feature\" &&\n coord.geometry !== null &&\n coord.geometry.type === \"Point\"\n ) {\n return [...coord.geometry.coordinates];\n }\n if (coord.type === \"Point\") {\n return [...coord.coordinates];\n }\n }\n if (\n Array.isArray(coord) &&\n coord.length >= 2 &&\n !Array.isArray(coord[0]) &&\n !Array.isArray(coord[1])\n ) {\n return [...coord];\n }\n\n throw new Error(\"coord must be GeoJSON Point or an Array of numbers\");\n}\n\n/**\n * Unwrap coordinates from a Feature, Geometry Object or an Array\n *\n * @name getCoords\n * @param {Array<any>|Geometry|Feature} coords Feature, Geometry Object or an Array\n * @returns {Array<any>} coordinates\n * @example\n * var poly = turf.polygon([[[119.32, -8.7], [119.55, -8.69], [119.51, -8.54], [119.32, -8.7]]]);\n *\n * var coords = turf.getCoords(poly);\n * //= [[[119.32, -8.7], [119.55, -8.69], [119.51, -8.54], [119.32, -8.7]]]\n */\nfunction getCoords<\n G extends\n | Point\n | LineString\n | Polygon\n | MultiPoint\n | MultiLineString\n | MultiPolygon,\n>(coords: any[] | Feature<G> | G): any[] {\n if (Array.isArray(coords)) {\n return coords;\n }\n\n // Feature\n if (coords.type === \"Feature\") {\n if (coords.geometry !== null) {\n return coords.geometry.coordinates;\n }\n } else {\n // Geometry\n if (coords.coordinates) {\n return coords.coordinates;\n }\n }\n\n throw new Error(\n \"coords must be GeoJSON Feature, Geometry Object or an Array\"\n );\n}\n\n/**\n * Checks if coordinates contains a number\n *\n * @name containsNumber\n * @param {Array<any>} coordinates GeoJSON Coordinates\n * @returns {boolean} true if Array contains a number\n */\nfunction containsNumber(coordinates: any[]): boolean {\n if (\n coordinates.length > 1 &&\n isNumber(coordinates[0]) &&\n isNumber(coordinates[1])\n ) {\n return true;\n }\n\n if (Array.isArray(coordinates[0]) && coordinates[0].length) {\n return containsNumber(coordinates[0]);\n }\n throw new Error(\"coordinates must only contain numbers\");\n}\n\n/**\n * Enforce expectations about types of GeoJSON objects for Turf.\n *\n * @name geojsonType\n * @param {GeoJSON} value any GeoJSON object\n * @param {string} type expected GeoJSON type\n * @param {string} name name of calling function\n * @throws {Error} if value is not the expected type.\n */\nfunction geojsonType(value: any, type: string, name: string): void {\n if (!type || !name) {\n throw new Error(\"type and name required\");\n }\n\n if (!value || value.type !== type) {\n throw new Error(\n \"Invalid input to \" +\n name +\n \": must be a \" +\n type +\n \", given \" +\n value.type\n );\n }\n}\n\n/**\n * Enforce expectations about types of {@link Feature} inputs for Turf.\n * Internally this uses {@link geojsonType} to judge geometry types.\n *\n * @name featureOf\n * @param {Feature} feature a feature with an expected geometry type\n * @param {string} type expected GeoJSON type\n * @param {string} name name of calling function\n * @throws {Error} error if value is not the expected type.\n */\nfunction featureOf(feature: Feature<any>, type: string, name: string): void {\n if (!feature) {\n throw new Error(\"No feature passed\");\n }\n if (!name) {\n throw new Error(\".featureOf() requires a name\");\n }\n if (!feature || feature.type !== \"Feature\" || !feature.geometry) {\n throw new Error(\n \"Invalid input to \" + name + \", Feature with geometry required\"\n );\n }\n if (!feature.geometry || feature.geometry.type !== type) {\n throw new Error(\n \"Invalid input to \" +\n name +\n \": must be a \" +\n type +\n \", given \" +\n feature.geometry.type\n );\n }\n}\n\n/**\n * Enforce expectations about types of {@link FeatureCollection} inputs for Turf.\n * Internally this uses {@link geojsonType} to judge geometry types.\n *\n * @name collectionOf\n * @param {FeatureCollection} featureCollection a FeatureCollection for which features will be judged\n * @param {string} type expected GeoJSON type\n * @param {string} name name of calling function\n * @throws {Error} if value is not the expected type.\n */\nfunction collectionOf(\n featureCollection: FeatureCollection<any>,\n type: string,\n name: string\n) {\n if (!featureCollection) {\n throw new Error(\"No featureCollection passed\");\n }\n if (!name) {\n throw new Error(\".collectionOf() requires a name\");\n }\n if (!featureCollection || featureCollection.type !== \"FeatureCollection\") {\n throw new Error(\n \"Invalid input to \" + name + \", FeatureCollection required\"\n );\n }\n for (const feature of featureCollection.features) {\n if (!feature || feature.type !== \"Feature\" || !feature.geometry) {\n throw new Error(\n \"Invalid input to \" + name + \", Feature with geometry required\"\n );\n }\n if (!feature.geometry || feature.geometry.type !== type) {\n throw new Error(\n \"Invalid input to \" +\n name +\n \": must be a \" +\n type +\n \", given \" +\n feature.geometry.type\n );\n }\n }\n}\n\n/**\n * Get Geometry from Feature or Geometry Object\n *\n * @param {Feature|Geometry} geojson GeoJSON Feature or Geometry Object\n * @returns {Geometry|null} GeoJSON Geometry Object\n * @throws {Error} if geojson is not a Feature or Geometry Object\n * @example\n * var point = {\n * \"type\": \"Feature\",\n * \"properties\": {},\n * \"geometry\": {\n * \"type\": \"Point\",\n * \"coordinates\": [110, 40]\n * }\n * }\n * var geom = turf.getGeom(point)\n * //={\"type\": \"Point\", \"coordinates\": [110, 40]}\n */\nfunction getGeom<G extends Geometry>(geojson: Feature<G> | G): G {\n if (geojson.type === \"Feature\") {\n return geojson.geometry;\n }\n return geojson;\n}\n\n/**\n * Get GeoJSON object's type, Geometry type is prioritize.\n *\n * @param {GeoJSON} geojson GeoJSON object\n * @param {string} [name=\"geojson\"] name of the variable to display in error message (unused)\n * @returns {string} GeoJSON type\n * @example\n * var point = {\n * \"type\": \"Feature\",\n * \"properties\": {},\n * \"geometry\": {\n * \"type\": \"Point\",\n * \"coordinates\": [110, 40]\n * }\n * }\n * var geom = turf.getType(point)\n * //=\"Point\"\n */\nfunction getType(\n geojson: Feature<any> | FeatureCollection<any> | Geometry,\n _name?: string\n): string {\n if (geojson.type === \"FeatureCollection\") {\n return \"FeatureCollection\";\n }\n if (geojson.type === \"GeometryCollection\") {\n return \"GeometryCollection\";\n }\n if (geojson.type === \"Feature\" && geojson.geometry !== null) {\n return geojson.geometry.type;\n }\n return geojson.type;\n}\n\nexport {\n getCoord,\n getCoords,\n containsNumber,\n geojsonType,\n featureOf,\n collectionOf,\n getGeom,\n getType,\n};\n// No default export!\n"]}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { Feature, Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, FeatureCollection, Geometry } from 'geojson';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Unwrap a coordinate from a Point Feature, Geometry or a single coordinate.
|
|
5
|
+
*
|
|
6
|
+
* @name getCoord
|
|
7
|
+
* @param {Array<number>|Geometry<Point>|Feature<Point>} coord GeoJSON Point or an Array of numbers
|
|
8
|
+
* @returns {Array<number>} coordinates
|
|
9
|
+
* @example
|
|
10
|
+
* var pt = turf.point([10, 10]);
|
|
11
|
+
*
|
|
12
|
+
* var coord = turf.getCoord(pt);
|
|
13
|
+
* //= [10, 10]
|
|
14
|
+
*/
|
|
15
|
+
declare function getCoord(coord: Feature<Point> | Point | number[]): number[];
|
|
16
|
+
/**
|
|
17
|
+
* Unwrap coordinates from a Feature, Geometry Object or an Array
|
|
18
|
+
*
|
|
19
|
+
* @name getCoords
|
|
20
|
+
* @param {Array<any>|Geometry|Feature} coords Feature, Geometry Object or an Array
|
|
21
|
+
* @returns {Array<any>} coordinates
|
|
22
|
+
* @example
|
|
23
|
+
* var poly = turf.polygon([[[119.32, -8.7], [119.55, -8.69], [119.51, -8.54], [119.32, -8.7]]]);
|
|
24
|
+
*
|
|
25
|
+
* var coords = turf.getCoords(poly);
|
|
26
|
+
* //= [[[119.32, -8.7], [119.55, -8.69], [119.51, -8.54], [119.32, -8.7]]]
|
|
27
|
+
*/
|
|
28
|
+
declare function getCoords<G extends Point | LineString | Polygon | MultiPoint | MultiLineString | MultiPolygon>(coords: any[] | Feature<G> | G): any[];
|
|
29
|
+
/**
|
|
30
|
+
* Checks if coordinates contains a number
|
|
31
|
+
*
|
|
32
|
+
* @name containsNumber
|
|
33
|
+
* @param {Array<any>} coordinates GeoJSON Coordinates
|
|
34
|
+
* @returns {boolean} true if Array contains a number
|
|
35
|
+
*/
|
|
36
|
+
declare function containsNumber(coordinates: any[]): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Enforce expectations about types of GeoJSON objects for Turf.
|
|
39
|
+
*
|
|
40
|
+
* @name geojsonType
|
|
41
|
+
* @param {GeoJSON} value any GeoJSON object
|
|
42
|
+
* @param {string} type expected GeoJSON type
|
|
43
|
+
* @param {string} name name of calling function
|
|
44
|
+
* @throws {Error} if value is not the expected type.
|
|
45
|
+
*/
|
|
46
|
+
declare function geojsonType(value: any, type: string, name: string): void;
|
|
47
|
+
/**
|
|
48
|
+
* Enforce expectations about types of {@link Feature} inputs for Turf.
|
|
49
|
+
* Internally this uses {@link geojsonType} to judge geometry types.
|
|
50
|
+
*
|
|
51
|
+
* @name featureOf
|
|
52
|
+
* @param {Feature} feature a feature with an expected geometry type
|
|
53
|
+
* @param {string} type expected GeoJSON type
|
|
54
|
+
* @param {string} name name of calling function
|
|
55
|
+
* @throws {Error} error if value is not the expected type.
|
|
56
|
+
*/
|
|
57
|
+
declare function featureOf(feature: Feature<any>, type: string, name: string): void;
|
|
58
|
+
/**
|
|
59
|
+
* Enforce expectations about types of {@link FeatureCollection} inputs for Turf.
|
|
60
|
+
* Internally this uses {@link geojsonType} to judge geometry types.
|
|
61
|
+
*
|
|
62
|
+
* @name collectionOf
|
|
63
|
+
* @param {FeatureCollection} featureCollection a FeatureCollection for which features will be judged
|
|
64
|
+
* @param {string} type expected GeoJSON type
|
|
65
|
+
* @param {string} name name of calling function
|
|
66
|
+
* @throws {Error} if value is not the expected type.
|
|
67
|
+
*/
|
|
68
|
+
declare function collectionOf(featureCollection: FeatureCollection<any>, type: string, name: string): void;
|
|
69
|
+
/**
|
|
70
|
+
* Get Geometry from Feature or Geometry Object
|
|
71
|
+
*
|
|
72
|
+
* @param {Feature|Geometry} geojson GeoJSON Feature or Geometry Object
|
|
73
|
+
* @returns {Geometry|null} GeoJSON Geometry Object
|
|
74
|
+
* @throws {Error} if geojson is not a Feature or Geometry Object
|
|
75
|
+
* @example
|
|
76
|
+
* var point = {
|
|
77
|
+
* "type": "Feature",
|
|
78
|
+
* "properties": {},
|
|
79
|
+
* "geometry": {
|
|
80
|
+
* "type": "Point",
|
|
81
|
+
* "coordinates": [110, 40]
|
|
82
|
+
* }
|
|
83
|
+
* }
|
|
84
|
+
* var geom = turf.getGeom(point)
|
|
85
|
+
* //={"type": "Point", "coordinates": [110, 40]}
|
|
86
|
+
*/
|
|
87
|
+
declare function getGeom<G extends Geometry>(geojson: Feature<G> | G): G;
|
|
88
|
+
/**
|
|
89
|
+
* Get GeoJSON object's type, Geometry type is prioritize.
|
|
90
|
+
*
|
|
91
|
+
* @param {GeoJSON} geojson GeoJSON object
|
|
92
|
+
* @param {string} [name="geojson"] name of the variable to display in error message (unused)
|
|
93
|
+
* @returns {string} GeoJSON type
|
|
94
|
+
* @example
|
|
95
|
+
* var point = {
|
|
96
|
+
* "type": "Feature",
|
|
97
|
+
* "properties": {},
|
|
98
|
+
* "geometry": {
|
|
99
|
+
* "type": "Point",
|
|
100
|
+
* "coordinates": [110, 40]
|
|
101
|
+
* }
|
|
102
|
+
* }
|
|
103
|
+
* var geom = turf.getType(point)
|
|
104
|
+
* //="Point"
|
|
105
|
+
*/
|
|
106
|
+
declare function getType(geojson: Feature<any> | FeatureCollection<any> | Geometry, _name?: string): string;
|
|
107
|
+
|
|
108
|
+
export { collectionOf, containsNumber, featureOf, geojsonType, getCoord, getCoords, getGeom, getType };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { Feature,
|
|
1
|
+
import { Feature, Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, FeatureCollection, Geometry } from 'geojson';
|
|
2
|
+
|
|
2
3
|
/**
|
|
3
4
|
* Unwrap a coordinate from a Point Feature, Geometry or a single coordinate.
|
|
4
5
|
*
|
|
@@ -11,7 +12,7 @@ import { Feature, FeatureCollection, Geometry, LineString, MultiPoint, MultiLine
|
|
|
11
12
|
* var coord = turf.getCoord(pt);
|
|
12
13
|
* //= [10, 10]
|
|
13
14
|
*/
|
|
14
|
-
|
|
15
|
+
declare function getCoord(coord: Feature<Point> | Point | number[]): number[];
|
|
15
16
|
/**
|
|
16
17
|
* Unwrap coordinates from a Feature, Geometry Object or an Array
|
|
17
18
|
*
|
|
@@ -24,7 +25,7 @@ export declare function getCoord(coord: Feature<Point> | Point | number[]): numb
|
|
|
24
25
|
* var coords = turf.getCoords(poly);
|
|
25
26
|
* //= [[[119.32, -8.7], [119.55, -8.69], [119.51, -8.54], [119.32, -8.7]]]
|
|
26
27
|
*/
|
|
27
|
-
|
|
28
|
+
declare function getCoords<G extends Point | LineString | Polygon | MultiPoint | MultiLineString | MultiPolygon>(coords: any[] | Feature<G> | G): any[];
|
|
28
29
|
/**
|
|
29
30
|
* Checks if coordinates contains a number
|
|
30
31
|
*
|
|
@@ -32,7 +33,7 @@ export declare function getCoords<G extends Point | LineString | Polygon | Multi
|
|
|
32
33
|
* @param {Array<any>} coordinates GeoJSON Coordinates
|
|
33
34
|
* @returns {boolean} true if Array contains a number
|
|
34
35
|
*/
|
|
35
|
-
|
|
36
|
+
declare function containsNumber(coordinates: any[]): boolean;
|
|
36
37
|
/**
|
|
37
38
|
* Enforce expectations about types of GeoJSON objects for Turf.
|
|
38
39
|
*
|
|
@@ -42,7 +43,7 @@ export declare function containsNumber(coordinates: any[]): boolean;
|
|
|
42
43
|
* @param {string} name name of calling function
|
|
43
44
|
* @throws {Error} if value is not the expected type.
|
|
44
45
|
*/
|
|
45
|
-
|
|
46
|
+
declare function geojsonType(value: any, type: string, name: string): void;
|
|
46
47
|
/**
|
|
47
48
|
* Enforce expectations about types of {@link Feature} inputs for Turf.
|
|
48
49
|
* Internally this uses {@link geojsonType} to judge geometry types.
|
|
@@ -53,7 +54,7 @@ export declare function geojsonType(value: any, type: string, name: string): voi
|
|
|
53
54
|
* @param {string} name name of calling function
|
|
54
55
|
* @throws {Error} error if value is not the expected type.
|
|
55
56
|
*/
|
|
56
|
-
|
|
57
|
+
declare function featureOf(feature: Feature<any>, type: string, name: string): void;
|
|
57
58
|
/**
|
|
58
59
|
* Enforce expectations about types of {@link FeatureCollection} inputs for Turf.
|
|
59
60
|
* Internally this uses {@link geojsonType} to judge geometry types.
|
|
@@ -64,7 +65,7 @@ export declare function featureOf(feature: Feature<any>, type: string, name: str
|
|
|
64
65
|
* @param {string} name name of calling function
|
|
65
66
|
* @throws {Error} if value is not the expected type.
|
|
66
67
|
*/
|
|
67
|
-
|
|
68
|
+
declare function collectionOf(featureCollection: FeatureCollection<any>, type: string, name: string): void;
|
|
68
69
|
/**
|
|
69
70
|
* Get Geometry from Feature or Geometry Object
|
|
70
71
|
*
|
|
@@ -83,7 +84,7 @@ export declare function collectionOf(featureCollection: FeatureCollection<any>,
|
|
|
83
84
|
* var geom = turf.getGeom(point)
|
|
84
85
|
* //={"type": "Point", "coordinates": [110, 40]}
|
|
85
86
|
*/
|
|
86
|
-
|
|
87
|
+
declare function getGeom<G extends Geometry>(geojson: Feature<G> | G): G;
|
|
87
88
|
/**
|
|
88
89
|
* Get GeoJSON object's type, Geometry type is prioritize.
|
|
89
90
|
*
|
|
@@ -102,4 +103,6 @@ export declare function getGeom<G extends Geometry>(geojson: Feature<G> | G): G;
|
|
|
102
103
|
* var geom = turf.getType(point)
|
|
103
104
|
* //="Point"
|
|
104
105
|
*/
|
|
105
|
-
|
|
106
|
+
declare function getType(geojson: Feature<any> | FeatureCollection<any> | Geometry, _name?: string): string;
|
|
107
|
+
|
|
108
|
+
export { collectionOf, containsNumber, featureOf, geojsonType, getCoord, getCoords, getGeom, getType };
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// index.ts
|
|
5
|
+
import { isNumber } from "@turf/helpers";
|
|
6
|
+
function getCoord(coord) {
|
|
7
|
+
if (!coord) {
|
|
8
|
+
throw new Error("coord is required");
|
|
9
|
+
}
|
|
10
|
+
if (!Array.isArray(coord)) {
|
|
11
|
+
if (coord.type === "Feature" && coord.geometry !== null && coord.geometry.type === "Point") {
|
|
12
|
+
return [...coord.geometry.coordinates];
|
|
13
|
+
}
|
|
14
|
+
if (coord.type === "Point") {
|
|
15
|
+
return [...coord.coordinates];
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
if (Array.isArray(coord) && coord.length >= 2 && !Array.isArray(coord[0]) && !Array.isArray(coord[1])) {
|
|
19
|
+
return [...coord];
|
|
20
|
+
}
|
|
21
|
+
throw new Error("coord must be GeoJSON Point or an Array of numbers");
|
|
22
|
+
}
|
|
23
|
+
__name(getCoord, "getCoord");
|
|
24
|
+
function getCoords(coords) {
|
|
25
|
+
if (Array.isArray(coords)) {
|
|
26
|
+
return coords;
|
|
27
|
+
}
|
|
28
|
+
if (coords.type === "Feature") {
|
|
29
|
+
if (coords.geometry !== null) {
|
|
30
|
+
return coords.geometry.coordinates;
|
|
31
|
+
}
|
|
32
|
+
} else {
|
|
33
|
+
if (coords.coordinates) {
|
|
34
|
+
return coords.coordinates;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
throw new Error(
|
|
38
|
+
"coords must be GeoJSON Feature, Geometry Object or an Array"
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
__name(getCoords, "getCoords");
|
|
42
|
+
function containsNumber(coordinates) {
|
|
43
|
+
if (coordinates.length > 1 && isNumber(coordinates[0]) && isNumber(coordinates[1])) {
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
if (Array.isArray(coordinates[0]) && coordinates[0].length) {
|
|
47
|
+
return containsNumber(coordinates[0]);
|
|
48
|
+
}
|
|
49
|
+
throw new Error("coordinates must only contain numbers");
|
|
50
|
+
}
|
|
51
|
+
__name(containsNumber, "containsNumber");
|
|
52
|
+
function geojsonType(value, type, name) {
|
|
53
|
+
if (!type || !name) {
|
|
54
|
+
throw new Error("type and name required");
|
|
55
|
+
}
|
|
56
|
+
if (!value || value.type !== type) {
|
|
57
|
+
throw new Error(
|
|
58
|
+
"Invalid input to " + name + ": must be a " + type + ", given " + value.type
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
__name(geojsonType, "geojsonType");
|
|
63
|
+
function featureOf(feature, type, name) {
|
|
64
|
+
if (!feature) {
|
|
65
|
+
throw new Error("No feature passed");
|
|
66
|
+
}
|
|
67
|
+
if (!name) {
|
|
68
|
+
throw new Error(".featureOf() requires a name");
|
|
69
|
+
}
|
|
70
|
+
if (!feature || feature.type !== "Feature" || !feature.geometry) {
|
|
71
|
+
throw new Error(
|
|
72
|
+
"Invalid input to " + name + ", Feature with geometry required"
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
if (!feature.geometry || feature.geometry.type !== type) {
|
|
76
|
+
throw new Error(
|
|
77
|
+
"Invalid input to " + name + ": must be a " + type + ", given " + feature.geometry.type
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
__name(featureOf, "featureOf");
|
|
82
|
+
function collectionOf(featureCollection, type, name) {
|
|
83
|
+
if (!featureCollection) {
|
|
84
|
+
throw new Error("No featureCollection passed");
|
|
85
|
+
}
|
|
86
|
+
if (!name) {
|
|
87
|
+
throw new Error(".collectionOf() requires a name");
|
|
88
|
+
}
|
|
89
|
+
if (!featureCollection || featureCollection.type !== "FeatureCollection") {
|
|
90
|
+
throw new Error(
|
|
91
|
+
"Invalid input to " + name + ", FeatureCollection required"
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
for (const feature of featureCollection.features) {
|
|
95
|
+
if (!feature || feature.type !== "Feature" || !feature.geometry) {
|
|
96
|
+
throw new Error(
|
|
97
|
+
"Invalid input to " + name + ", Feature with geometry required"
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
if (!feature.geometry || feature.geometry.type !== type) {
|
|
101
|
+
throw new Error(
|
|
102
|
+
"Invalid input to " + name + ": must be a " + type + ", given " + feature.geometry.type
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
__name(collectionOf, "collectionOf");
|
|
108
|
+
function getGeom(geojson) {
|
|
109
|
+
if (geojson.type === "Feature") {
|
|
110
|
+
return geojson.geometry;
|
|
111
|
+
}
|
|
112
|
+
return geojson;
|
|
113
|
+
}
|
|
114
|
+
__name(getGeom, "getGeom");
|
|
115
|
+
function getType(geojson, _name) {
|
|
116
|
+
if (geojson.type === "FeatureCollection") {
|
|
117
|
+
return "FeatureCollection";
|
|
118
|
+
}
|
|
119
|
+
if (geojson.type === "GeometryCollection") {
|
|
120
|
+
return "GeometryCollection";
|
|
121
|
+
}
|
|
122
|
+
if (geojson.type === "Feature" && geojson.geometry !== null) {
|
|
123
|
+
return geojson.geometry.type;
|
|
124
|
+
}
|
|
125
|
+
return geojson.type;
|
|
126
|
+
}
|
|
127
|
+
__name(getType, "getType");
|
|
128
|
+
export {
|
|
129
|
+
collectionOf,
|
|
130
|
+
containsNumber,
|
|
131
|
+
featureOf,
|
|
132
|
+
geojsonType,
|
|
133
|
+
getCoord,
|
|
134
|
+
getCoords,
|
|
135
|
+
getGeom,
|
|
136
|
+
getType
|
|
137
|
+
};
|
|
138
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../index.ts"],"sourcesContent":["import {\n Feature,\n FeatureCollection,\n Geometry,\n LineString,\n MultiPoint,\n MultiLineString,\n MultiPolygon,\n Point,\n Polygon,\n} from \"geojson\";\nimport { isNumber } from \"@turf/helpers\";\n\n/**\n * Unwrap a coordinate from a Point Feature, Geometry or a single coordinate.\n *\n * @name getCoord\n * @param {Array<number>|Geometry<Point>|Feature<Point>} coord GeoJSON Point or an Array of numbers\n * @returns {Array<number>} coordinates\n * @example\n * var pt = turf.point([10, 10]);\n *\n * var coord = turf.getCoord(pt);\n * //= [10, 10]\n */\nfunction getCoord(coord: Feature<Point> | Point | number[]): number[] {\n if (!coord) {\n throw new Error(\"coord is required\");\n }\n\n if (!Array.isArray(coord)) {\n if (\n coord.type === \"Feature\" &&\n coord.geometry !== null &&\n coord.geometry.type === \"Point\"\n ) {\n return [...coord.geometry.coordinates];\n }\n if (coord.type === \"Point\") {\n return [...coord.coordinates];\n }\n }\n if (\n Array.isArray(coord) &&\n coord.length >= 2 &&\n !Array.isArray(coord[0]) &&\n !Array.isArray(coord[1])\n ) {\n return [...coord];\n }\n\n throw new Error(\"coord must be GeoJSON Point or an Array of numbers\");\n}\n\n/**\n * Unwrap coordinates from a Feature, Geometry Object or an Array\n *\n * @name getCoords\n * @param {Array<any>|Geometry|Feature} coords Feature, Geometry Object or an Array\n * @returns {Array<any>} coordinates\n * @example\n * var poly = turf.polygon([[[119.32, -8.7], [119.55, -8.69], [119.51, -8.54], [119.32, -8.7]]]);\n *\n * var coords = turf.getCoords(poly);\n * //= [[[119.32, -8.7], [119.55, -8.69], [119.51, -8.54], [119.32, -8.7]]]\n */\nfunction getCoords<\n G extends\n | Point\n | LineString\n | Polygon\n | MultiPoint\n | MultiLineString\n | MultiPolygon,\n>(coords: any[] | Feature<G> | G): any[] {\n if (Array.isArray(coords)) {\n return coords;\n }\n\n // Feature\n if (coords.type === \"Feature\") {\n if (coords.geometry !== null) {\n return coords.geometry.coordinates;\n }\n } else {\n // Geometry\n if (coords.coordinates) {\n return coords.coordinates;\n }\n }\n\n throw new Error(\n \"coords must be GeoJSON Feature, Geometry Object or an Array\"\n );\n}\n\n/**\n * Checks if coordinates contains a number\n *\n * @name containsNumber\n * @param {Array<any>} coordinates GeoJSON Coordinates\n * @returns {boolean} true if Array contains a number\n */\nfunction containsNumber(coordinates: any[]): boolean {\n if (\n coordinates.length > 1 &&\n isNumber(coordinates[0]) &&\n isNumber(coordinates[1])\n ) {\n return true;\n }\n\n if (Array.isArray(coordinates[0]) && coordinates[0].length) {\n return containsNumber(coordinates[0]);\n }\n throw new Error(\"coordinates must only contain numbers\");\n}\n\n/**\n * Enforce expectations about types of GeoJSON objects for Turf.\n *\n * @name geojsonType\n * @param {GeoJSON} value any GeoJSON object\n * @param {string} type expected GeoJSON type\n * @param {string} name name of calling function\n * @throws {Error} if value is not the expected type.\n */\nfunction geojsonType(value: any, type: string, name: string): void {\n if (!type || !name) {\n throw new Error(\"type and name required\");\n }\n\n if (!value || value.type !== type) {\n throw new Error(\n \"Invalid input to \" +\n name +\n \": must be a \" +\n type +\n \", given \" +\n value.type\n );\n }\n}\n\n/**\n * Enforce expectations about types of {@link Feature} inputs for Turf.\n * Internally this uses {@link geojsonType} to judge geometry types.\n *\n * @name featureOf\n * @param {Feature} feature a feature with an expected geometry type\n * @param {string} type expected GeoJSON type\n * @param {string} name name of calling function\n * @throws {Error} error if value is not the expected type.\n */\nfunction featureOf(feature: Feature<any>, type: string, name: string): void {\n if (!feature) {\n throw new Error(\"No feature passed\");\n }\n if (!name) {\n throw new Error(\".featureOf() requires a name\");\n }\n if (!feature || feature.type !== \"Feature\" || !feature.geometry) {\n throw new Error(\n \"Invalid input to \" + name + \", Feature with geometry required\"\n );\n }\n if (!feature.geometry || feature.geometry.type !== type) {\n throw new Error(\n \"Invalid input to \" +\n name +\n \": must be a \" +\n type +\n \", given \" +\n feature.geometry.type\n );\n }\n}\n\n/**\n * Enforce expectations about types of {@link FeatureCollection} inputs for Turf.\n * Internally this uses {@link geojsonType} to judge geometry types.\n *\n * @name collectionOf\n * @param {FeatureCollection} featureCollection a FeatureCollection for which features will be judged\n * @param {string} type expected GeoJSON type\n * @param {string} name name of calling function\n * @throws {Error} if value is not the expected type.\n */\nfunction collectionOf(\n featureCollection: FeatureCollection<any>,\n type: string,\n name: string\n) {\n if (!featureCollection) {\n throw new Error(\"No featureCollection passed\");\n }\n if (!name) {\n throw new Error(\".collectionOf() requires a name\");\n }\n if (!featureCollection || featureCollection.type !== \"FeatureCollection\") {\n throw new Error(\n \"Invalid input to \" + name + \", FeatureCollection required\"\n );\n }\n for (const feature of featureCollection.features) {\n if (!feature || feature.type !== \"Feature\" || !feature.geometry) {\n throw new Error(\n \"Invalid input to \" + name + \", Feature with geometry required\"\n );\n }\n if (!feature.geometry || feature.geometry.type !== type) {\n throw new Error(\n \"Invalid input to \" +\n name +\n \": must be a \" +\n type +\n \", given \" +\n feature.geometry.type\n );\n }\n }\n}\n\n/**\n * Get Geometry from Feature or Geometry Object\n *\n * @param {Feature|Geometry} geojson GeoJSON Feature or Geometry Object\n * @returns {Geometry|null} GeoJSON Geometry Object\n * @throws {Error} if geojson is not a Feature or Geometry Object\n * @example\n * var point = {\n * \"type\": \"Feature\",\n * \"properties\": {},\n * \"geometry\": {\n * \"type\": \"Point\",\n * \"coordinates\": [110, 40]\n * }\n * }\n * var geom = turf.getGeom(point)\n * //={\"type\": \"Point\", \"coordinates\": [110, 40]}\n */\nfunction getGeom<G extends Geometry>(geojson: Feature<G> | G): G {\n if (geojson.type === \"Feature\") {\n return geojson.geometry;\n }\n return geojson;\n}\n\n/**\n * Get GeoJSON object's type, Geometry type is prioritize.\n *\n * @param {GeoJSON} geojson GeoJSON object\n * @param {string} [name=\"geojson\"] name of the variable to display in error message (unused)\n * @returns {string} GeoJSON type\n * @example\n * var point = {\n * \"type\": \"Feature\",\n * \"properties\": {},\n * \"geometry\": {\n * \"type\": \"Point\",\n * \"coordinates\": [110, 40]\n * }\n * }\n * var geom = turf.getType(point)\n * //=\"Point\"\n */\nfunction getType(\n geojson: Feature<any> | FeatureCollection<any> | Geometry,\n _name?: string\n): string {\n if (geojson.type === \"FeatureCollection\") {\n return \"FeatureCollection\";\n }\n if (geojson.type === \"GeometryCollection\") {\n return \"GeometryCollection\";\n }\n if (geojson.type === \"Feature\" && geojson.geometry !== null) {\n return geojson.geometry.type;\n }\n return geojson.type;\n}\n\nexport {\n getCoord,\n getCoords,\n containsNumber,\n geojsonType,\n featureOf,\n collectionOf,\n getGeom,\n getType,\n};\n// No default export!\n"],"mappings":";;;;AAWA,SAAS,gBAAgB;AAczB,SAAS,SAAS,OAAoD;AACpE,MAAI,CAAC,OAAO;AACV,UAAM,IAAI,MAAM,mBAAmB;AAAA,EACrC;AAEA,MAAI,CAAC,MAAM,QAAQ,KAAK,GAAG;AACzB,QACE,MAAM,SAAS,aACf,MAAM,aAAa,QACnB,MAAM,SAAS,SAAS,SACxB;AACA,aAAO,CAAC,GAAG,MAAM,SAAS,WAAW;AAAA,IACvC;AACA,QAAI,MAAM,SAAS,SAAS;AAC1B,aAAO,CAAC,GAAG,MAAM,WAAW;AAAA,IAC9B;AAAA,EACF;AACA,MACE,MAAM,QAAQ,KAAK,KACnB,MAAM,UAAU,KAChB,CAAC,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,EAC1B;AACA,SAAO,QAAQ;AACjB;AAdS;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turf/invariant",
|
|
3
|
-
"version": "7.0.0
|
|
3
|
+
"version": "7.0.0",
|
|
4
4
|
"description": "turf invariant module",
|
|
5
5
|
"author": "Turf Authors",
|
|
6
6
|
"contributors": [
|
|
@@ -25,42 +25,48 @@
|
|
|
25
25
|
"invariant",
|
|
26
26
|
"expectations"
|
|
27
27
|
],
|
|
28
|
-
"
|
|
29
|
-
"
|
|
28
|
+
"type": "module",
|
|
29
|
+
"main": "dist/cjs/index.cjs",
|
|
30
|
+
"module": "dist/esm/index.js",
|
|
31
|
+
"types": "dist/esm/index.d.ts",
|
|
30
32
|
"exports": {
|
|
31
33
|
"./package.json": "./package.json",
|
|
32
34
|
".": {
|
|
33
|
-
"
|
|
34
|
-
|
|
35
|
-
|
|
35
|
+
"import": {
|
|
36
|
+
"types": "./dist/esm/index.d.ts",
|
|
37
|
+
"default": "./dist/esm/index.js"
|
|
38
|
+
},
|
|
39
|
+
"require": {
|
|
40
|
+
"types": "./dist/cjs/index.d.cts",
|
|
41
|
+
"default": "./dist/cjs/index.cjs"
|
|
42
|
+
}
|
|
36
43
|
}
|
|
37
44
|
},
|
|
38
|
-
"types": "dist/js/index.d.ts",
|
|
39
45
|
"sideEffects": false,
|
|
40
46
|
"files": [
|
|
41
47
|
"dist"
|
|
42
48
|
],
|
|
43
49
|
"scripts": {
|
|
44
|
-
"bench": "tsx bench.
|
|
45
|
-
"build": "
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"test": "
|
|
50
|
-
"test:tape": "tsx test.js",
|
|
51
|
-
"test:types": "tsc --esModuleInterop --noEmit --strict types.ts"
|
|
50
|
+
"bench": "tsx bench.ts",
|
|
51
|
+
"build": "tsup --config ../../tsup.config.ts",
|
|
52
|
+
"docs": "tsx ../../scripts/generate-readmes.ts",
|
|
53
|
+
"test": "npm-run-all --npm-path npm test:*",
|
|
54
|
+
"test:tape": "tsx test.ts",
|
|
55
|
+
"test:types": "tsc --esModuleInterop --module node16 --moduleResolution node16 --noEmit --strict types.ts"
|
|
52
56
|
},
|
|
53
57
|
"devDependencies": {
|
|
54
|
-
"benchmark": "
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
58
|
+
"@types/benchmark": "^2.1.5",
|
|
59
|
+
"@types/tape": "^4.2.32",
|
|
60
|
+
"benchmark": "^2.1.4",
|
|
61
|
+
"npm-run-all": "^4.1.5",
|
|
62
|
+
"tape": "^5.7.2",
|
|
63
|
+
"tsup": "^8.0.1",
|
|
64
|
+
"tsx": "^4.6.2",
|
|
65
|
+
"typescript": "^5.2.2"
|
|
60
66
|
},
|
|
61
67
|
"dependencies": {
|
|
62
|
-
"@turf/helpers": "^7.0.0
|
|
63
|
-
"tslib": "^2.
|
|
68
|
+
"@turf/helpers": "^7.0.0",
|
|
69
|
+
"tslib": "^2.6.2"
|
|
64
70
|
},
|
|
65
|
-
"gitHead": "
|
|
71
|
+
"gitHead": "3d3a7917025fbabe191dbddbc89754b86f9c7739"
|
|
66
72
|
}
|
package/dist/es/index.js
DELETED
|
@@ -1,222 +0,0 @@
|
|
|
1
|
-
import { isNumber } from "@turf/helpers";
|
|
2
|
-
/**
|
|
3
|
-
* Unwrap a coordinate from a Point Feature, Geometry or a single coordinate.
|
|
4
|
-
*
|
|
5
|
-
* @name getCoord
|
|
6
|
-
* @param {Array<number>|Geometry<Point>|Feature<Point>} coord GeoJSON Point or an Array of numbers
|
|
7
|
-
* @returns {Array<number>} coordinates
|
|
8
|
-
* @example
|
|
9
|
-
* var pt = turf.point([10, 10]);
|
|
10
|
-
*
|
|
11
|
-
* var coord = turf.getCoord(pt);
|
|
12
|
-
* //= [10, 10]
|
|
13
|
-
*/
|
|
14
|
-
export function getCoord(coord) {
|
|
15
|
-
if (!coord) {
|
|
16
|
-
throw new Error("coord is required");
|
|
17
|
-
}
|
|
18
|
-
if (!Array.isArray(coord)) {
|
|
19
|
-
if (coord.type === "Feature" &&
|
|
20
|
-
coord.geometry !== null &&
|
|
21
|
-
coord.geometry.type === "Point") {
|
|
22
|
-
return [...coord.geometry.coordinates];
|
|
23
|
-
}
|
|
24
|
-
if (coord.type === "Point") {
|
|
25
|
-
return [...coord.coordinates];
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
if (Array.isArray(coord) &&
|
|
29
|
-
coord.length >= 2 &&
|
|
30
|
-
!Array.isArray(coord[0]) &&
|
|
31
|
-
!Array.isArray(coord[1])) {
|
|
32
|
-
return [...coord];
|
|
33
|
-
}
|
|
34
|
-
throw new Error("coord must be GeoJSON Point or an Array of numbers");
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Unwrap coordinates from a Feature, Geometry Object or an Array
|
|
38
|
-
*
|
|
39
|
-
* @name getCoords
|
|
40
|
-
* @param {Array<any>|Geometry|Feature} coords Feature, Geometry Object or an Array
|
|
41
|
-
* @returns {Array<any>} coordinates
|
|
42
|
-
* @example
|
|
43
|
-
* var poly = turf.polygon([[[119.32, -8.7], [119.55, -8.69], [119.51, -8.54], [119.32, -8.7]]]);
|
|
44
|
-
*
|
|
45
|
-
* var coords = turf.getCoords(poly);
|
|
46
|
-
* //= [[[119.32, -8.7], [119.55, -8.69], [119.51, -8.54], [119.32, -8.7]]]
|
|
47
|
-
*/
|
|
48
|
-
export function getCoords(coords) {
|
|
49
|
-
if (Array.isArray(coords)) {
|
|
50
|
-
return coords;
|
|
51
|
-
}
|
|
52
|
-
// Feature
|
|
53
|
-
if (coords.type === "Feature") {
|
|
54
|
-
if (coords.geometry !== null) {
|
|
55
|
-
return coords.geometry.coordinates;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
// Geometry
|
|
60
|
-
if (coords.coordinates) {
|
|
61
|
-
return coords.coordinates;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
throw new Error("coords must be GeoJSON Feature, Geometry Object or an Array");
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Checks if coordinates contains a number
|
|
68
|
-
*
|
|
69
|
-
* @name containsNumber
|
|
70
|
-
* @param {Array<any>} coordinates GeoJSON Coordinates
|
|
71
|
-
* @returns {boolean} true if Array contains a number
|
|
72
|
-
*/
|
|
73
|
-
export function containsNumber(coordinates) {
|
|
74
|
-
if (coordinates.length > 1 &&
|
|
75
|
-
isNumber(coordinates[0]) &&
|
|
76
|
-
isNumber(coordinates[1])) {
|
|
77
|
-
return true;
|
|
78
|
-
}
|
|
79
|
-
if (Array.isArray(coordinates[0]) && coordinates[0].length) {
|
|
80
|
-
return containsNumber(coordinates[0]);
|
|
81
|
-
}
|
|
82
|
-
throw new Error("coordinates must only contain numbers");
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Enforce expectations about types of GeoJSON objects for Turf.
|
|
86
|
-
*
|
|
87
|
-
* @name geojsonType
|
|
88
|
-
* @param {GeoJSON} value any GeoJSON object
|
|
89
|
-
* @param {string} type expected GeoJSON type
|
|
90
|
-
* @param {string} name name of calling function
|
|
91
|
-
* @throws {Error} if value is not the expected type.
|
|
92
|
-
*/
|
|
93
|
-
export function geojsonType(value, type, name) {
|
|
94
|
-
if (!type || !name) {
|
|
95
|
-
throw new Error("type and name required");
|
|
96
|
-
}
|
|
97
|
-
if (!value || value.type !== type) {
|
|
98
|
-
throw new Error("Invalid input to " +
|
|
99
|
-
name +
|
|
100
|
-
": must be a " +
|
|
101
|
-
type +
|
|
102
|
-
", given " +
|
|
103
|
-
value.type);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* Enforce expectations about types of {@link Feature} inputs for Turf.
|
|
108
|
-
* Internally this uses {@link geojsonType} to judge geometry types.
|
|
109
|
-
*
|
|
110
|
-
* @name featureOf
|
|
111
|
-
* @param {Feature} feature a feature with an expected geometry type
|
|
112
|
-
* @param {string} type expected GeoJSON type
|
|
113
|
-
* @param {string} name name of calling function
|
|
114
|
-
* @throws {Error} error if value is not the expected type.
|
|
115
|
-
*/
|
|
116
|
-
export function featureOf(feature, type, name) {
|
|
117
|
-
if (!feature) {
|
|
118
|
-
throw new Error("No feature passed");
|
|
119
|
-
}
|
|
120
|
-
if (!name) {
|
|
121
|
-
throw new Error(".featureOf() requires a name");
|
|
122
|
-
}
|
|
123
|
-
if (!feature || feature.type !== "Feature" || !feature.geometry) {
|
|
124
|
-
throw new Error("Invalid input to " + name + ", Feature with geometry required");
|
|
125
|
-
}
|
|
126
|
-
if (!feature.geometry || feature.geometry.type !== type) {
|
|
127
|
-
throw new Error("Invalid input to " +
|
|
128
|
-
name +
|
|
129
|
-
": must be a " +
|
|
130
|
-
type +
|
|
131
|
-
", given " +
|
|
132
|
-
feature.geometry.type);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Enforce expectations about types of {@link FeatureCollection} inputs for Turf.
|
|
137
|
-
* Internally this uses {@link geojsonType} to judge geometry types.
|
|
138
|
-
*
|
|
139
|
-
* @name collectionOf
|
|
140
|
-
* @param {FeatureCollection} featureCollection a FeatureCollection for which features will be judged
|
|
141
|
-
* @param {string} type expected GeoJSON type
|
|
142
|
-
* @param {string} name name of calling function
|
|
143
|
-
* @throws {Error} if value is not the expected type.
|
|
144
|
-
*/
|
|
145
|
-
export function collectionOf(featureCollection, type, name) {
|
|
146
|
-
if (!featureCollection) {
|
|
147
|
-
throw new Error("No featureCollection passed");
|
|
148
|
-
}
|
|
149
|
-
if (!name) {
|
|
150
|
-
throw new Error(".collectionOf() requires a name");
|
|
151
|
-
}
|
|
152
|
-
if (!featureCollection || featureCollection.type !== "FeatureCollection") {
|
|
153
|
-
throw new Error("Invalid input to " + name + ", FeatureCollection required");
|
|
154
|
-
}
|
|
155
|
-
for (const feature of featureCollection.features) {
|
|
156
|
-
if (!feature || feature.type !== "Feature" || !feature.geometry) {
|
|
157
|
-
throw new Error("Invalid input to " + name + ", Feature with geometry required");
|
|
158
|
-
}
|
|
159
|
-
if (!feature.geometry || feature.geometry.type !== type) {
|
|
160
|
-
throw new Error("Invalid input to " +
|
|
161
|
-
name +
|
|
162
|
-
": must be a " +
|
|
163
|
-
type +
|
|
164
|
-
", given " +
|
|
165
|
-
feature.geometry.type);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
/**
|
|
170
|
-
* Get Geometry from Feature or Geometry Object
|
|
171
|
-
*
|
|
172
|
-
* @param {Feature|Geometry} geojson GeoJSON Feature or Geometry Object
|
|
173
|
-
* @returns {Geometry|null} GeoJSON Geometry Object
|
|
174
|
-
* @throws {Error} if geojson is not a Feature or Geometry Object
|
|
175
|
-
* @example
|
|
176
|
-
* var point = {
|
|
177
|
-
* "type": "Feature",
|
|
178
|
-
* "properties": {},
|
|
179
|
-
* "geometry": {
|
|
180
|
-
* "type": "Point",
|
|
181
|
-
* "coordinates": [110, 40]
|
|
182
|
-
* }
|
|
183
|
-
* }
|
|
184
|
-
* var geom = turf.getGeom(point)
|
|
185
|
-
* //={"type": "Point", "coordinates": [110, 40]}
|
|
186
|
-
*/
|
|
187
|
-
export function getGeom(geojson) {
|
|
188
|
-
if (geojson.type === "Feature") {
|
|
189
|
-
return geojson.geometry;
|
|
190
|
-
}
|
|
191
|
-
return geojson;
|
|
192
|
-
}
|
|
193
|
-
/**
|
|
194
|
-
* Get GeoJSON object's type, Geometry type is prioritize.
|
|
195
|
-
*
|
|
196
|
-
* @param {GeoJSON} geojson GeoJSON object
|
|
197
|
-
* @param {string} [name="geojson"] name of the variable to display in error message (unused)
|
|
198
|
-
* @returns {string} GeoJSON type
|
|
199
|
-
* @example
|
|
200
|
-
* var point = {
|
|
201
|
-
* "type": "Feature",
|
|
202
|
-
* "properties": {},
|
|
203
|
-
* "geometry": {
|
|
204
|
-
* "type": "Point",
|
|
205
|
-
* "coordinates": [110, 40]
|
|
206
|
-
* }
|
|
207
|
-
* }
|
|
208
|
-
* var geom = turf.getType(point)
|
|
209
|
-
* //="Point"
|
|
210
|
-
*/
|
|
211
|
-
export function getType(geojson, _name) {
|
|
212
|
-
if (geojson.type === "FeatureCollection") {
|
|
213
|
-
return "FeatureCollection";
|
|
214
|
-
}
|
|
215
|
-
if (geojson.type === "GeometryCollection") {
|
|
216
|
-
return "GeometryCollection";
|
|
217
|
-
}
|
|
218
|
-
if (geojson.type === "Feature" && geojson.geometry !== null) {
|
|
219
|
-
return geojson.geometry.type;
|
|
220
|
-
}
|
|
221
|
-
return geojson.type;
|
|
222
|
-
}
|
package/dist/es/package.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"type":"module"}
|
package/dist/js/index.js
DELETED
|
@@ -1,232 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const helpers_1 = require("@turf/helpers");
|
|
4
|
-
/**
|
|
5
|
-
* Unwrap a coordinate from a Point Feature, Geometry or a single coordinate.
|
|
6
|
-
*
|
|
7
|
-
* @name getCoord
|
|
8
|
-
* @param {Array<number>|Geometry<Point>|Feature<Point>} coord GeoJSON Point or an Array of numbers
|
|
9
|
-
* @returns {Array<number>} coordinates
|
|
10
|
-
* @example
|
|
11
|
-
* var pt = turf.point([10, 10]);
|
|
12
|
-
*
|
|
13
|
-
* var coord = turf.getCoord(pt);
|
|
14
|
-
* //= [10, 10]
|
|
15
|
-
*/
|
|
16
|
-
function getCoord(coord) {
|
|
17
|
-
if (!coord) {
|
|
18
|
-
throw new Error("coord is required");
|
|
19
|
-
}
|
|
20
|
-
if (!Array.isArray(coord)) {
|
|
21
|
-
if (coord.type === "Feature" &&
|
|
22
|
-
coord.geometry !== null &&
|
|
23
|
-
coord.geometry.type === "Point") {
|
|
24
|
-
return [...coord.geometry.coordinates];
|
|
25
|
-
}
|
|
26
|
-
if (coord.type === "Point") {
|
|
27
|
-
return [...coord.coordinates];
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
if (Array.isArray(coord) &&
|
|
31
|
-
coord.length >= 2 &&
|
|
32
|
-
!Array.isArray(coord[0]) &&
|
|
33
|
-
!Array.isArray(coord[1])) {
|
|
34
|
-
return [...coord];
|
|
35
|
-
}
|
|
36
|
-
throw new Error("coord must be GeoJSON Point or an Array of numbers");
|
|
37
|
-
}
|
|
38
|
-
exports.getCoord = getCoord;
|
|
39
|
-
/**
|
|
40
|
-
* Unwrap coordinates from a Feature, Geometry Object or an Array
|
|
41
|
-
*
|
|
42
|
-
* @name getCoords
|
|
43
|
-
* @param {Array<any>|Geometry|Feature} coords Feature, Geometry Object or an Array
|
|
44
|
-
* @returns {Array<any>} coordinates
|
|
45
|
-
* @example
|
|
46
|
-
* var poly = turf.polygon([[[119.32, -8.7], [119.55, -8.69], [119.51, -8.54], [119.32, -8.7]]]);
|
|
47
|
-
*
|
|
48
|
-
* var coords = turf.getCoords(poly);
|
|
49
|
-
* //= [[[119.32, -8.7], [119.55, -8.69], [119.51, -8.54], [119.32, -8.7]]]
|
|
50
|
-
*/
|
|
51
|
-
function getCoords(coords) {
|
|
52
|
-
if (Array.isArray(coords)) {
|
|
53
|
-
return coords;
|
|
54
|
-
}
|
|
55
|
-
// Feature
|
|
56
|
-
if (coords.type === "Feature") {
|
|
57
|
-
if (coords.geometry !== null) {
|
|
58
|
-
return coords.geometry.coordinates;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
// Geometry
|
|
63
|
-
if (coords.coordinates) {
|
|
64
|
-
return coords.coordinates;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
throw new Error("coords must be GeoJSON Feature, Geometry Object or an Array");
|
|
68
|
-
}
|
|
69
|
-
exports.getCoords = getCoords;
|
|
70
|
-
/**
|
|
71
|
-
* Checks if coordinates contains a number
|
|
72
|
-
*
|
|
73
|
-
* @name containsNumber
|
|
74
|
-
* @param {Array<any>} coordinates GeoJSON Coordinates
|
|
75
|
-
* @returns {boolean} true if Array contains a number
|
|
76
|
-
*/
|
|
77
|
-
function containsNumber(coordinates) {
|
|
78
|
-
if (coordinates.length > 1 &&
|
|
79
|
-
helpers_1.isNumber(coordinates[0]) &&
|
|
80
|
-
helpers_1.isNumber(coordinates[1])) {
|
|
81
|
-
return true;
|
|
82
|
-
}
|
|
83
|
-
if (Array.isArray(coordinates[0]) && coordinates[0].length) {
|
|
84
|
-
return containsNumber(coordinates[0]);
|
|
85
|
-
}
|
|
86
|
-
throw new Error("coordinates must only contain numbers");
|
|
87
|
-
}
|
|
88
|
-
exports.containsNumber = containsNumber;
|
|
89
|
-
/**
|
|
90
|
-
* Enforce expectations about types of GeoJSON objects for Turf.
|
|
91
|
-
*
|
|
92
|
-
* @name geojsonType
|
|
93
|
-
* @param {GeoJSON} value any GeoJSON object
|
|
94
|
-
* @param {string} type expected GeoJSON type
|
|
95
|
-
* @param {string} name name of calling function
|
|
96
|
-
* @throws {Error} if value is not the expected type.
|
|
97
|
-
*/
|
|
98
|
-
function geojsonType(value, type, name) {
|
|
99
|
-
if (!type || !name) {
|
|
100
|
-
throw new Error("type and name required");
|
|
101
|
-
}
|
|
102
|
-
if (!value || value.type !== type) {
|
|
103
|
-
throw new Error("Invalid input to " +
|
|
104
|
-
name +
|
|
105
|
-
": must be a " +
|
|
106
|
-
type +
|
|
107
|
-
", given " +
|
|
108
|
-
value.type);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
exports.geojsonType = geojsonType;
|
|
112
|
-
/**
|
|
113
|
-
* Enforce expectations about types of {@link Feature} inputs for Turf.
|
|
114
|
-
* Internally this uses {@link geojsonType} to judge geometry types.
|
|
115
|
-
*
|
|
116
|
-
* @name featureOf
|
|
117
|
-
* @param {Feature} feature a feature with an expected geometry type
|
|
118
|
-
* @param {string} type expected GeoJSON type
|
|
119
|
-
* @param {string} name name of calling function
|
|
120
|
-
* @throws {Error} error if value is not the expected type.
|
|
121
|
-
*/
|
|
122
|
-
function featureOf(feature, type, name) {
|
|
123
|
-
if (!feature) {
|
|
124
|
-
throw new Error("No feature passed");
|
|
125
|
-
}
|
|
126
|
-
if (!name) {
|
|
127
|
-
throw new Error(".featureOf() requires a name");
|
|
128
|
-
}
|
|
129
|
-
if (!feature || feature.type !== "Feature" || !feature.geometry) {
|
|
130
|
-
throw new Error("Invalid input to " + name + ", Feature with geometry required");
|
|
131
|
-
}
|
|
132
|
-
if (!feature.geometry || feature.geometry.type !== type) {
|
|
133
|
-
throw new Error("Invalid input to " +
|
|
134
|
-
name +
|
|
135
|
-
": must be a " +
|
|
136
|
-
type +
|
|
137
|
-
", given " +
|
|
138
|
-
feature.geometry.type);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
exports.featureOf = featureOf;
|
|
142
|
-
/**
|
|
143
|
-
* Enforce expectations about types of {@link FeatureCollection} inputs for Turf.
|
|
144
|
-
* Internally this uses {@link geojsonType} to judge geometry types.
|
|
145
|
-
*
|
|
146
|
-
* @name collectionOf
|
|
147
|
-
* @param {FeatureCollection} featureCollection a FeatureCollection for which features will be judged
|
|
148
|
-
* @param {string} type expected GeoJSON type
|
|
149
|
-
* @param {string} name name of calling function
|
|
150
|
-
* @throws {Error} if value is not the expected type.
|
|
151
|
-
*/
|
|
152
|
-
function collectionOf(featureCollection, type, name) {
|
|
153
|
-
if (!featureCollection) {
|
|
154
|
-
throw new Error("No featureCollection passed");
|
|
155
|
-
}
|
|
156
|
-
if (!name) {
|
|
157
|
-
throw new Error(".collectionOf() requires a name");
|
|
158
|
-
}
|
|
159
|
-
if (!featureCollection || featureCollection.type !== "FeatureCollection") {
|
|
160
|
-
throw new Error("Invalid input to " + name + ", FeatureCollection required");
|
|
161
|
-
}
|
|
162
|
-
for (const feature of featureCollection.features) {
|
|
163
|
-
if (!feature || feature.type !== "Feature" || !feature.geometry) {
|
|
164
|
-
throw new Error("Invalid input to " + name + ", Feature with geometry required");
|
|
165
|
-
}
|
|
166
|
-
if (!feature.geometry || feature.geometry.type !== type) {
|
|
167
|
-
throw new Error("Invalid input to " +
|
|
168
|
-
name +
|
|
169
|
-
": must be a " +
|
|
170
|
-
type +
|
|
171
|
-
", given " +
|
|
172
|
-
feature.geometry.type);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
exports.collectionOf = collectionOf;
|
|
177
|
-
/**
|
|
178
|
-
* Get Geometry from Feature or Geometry Object
|
|
179
|
-
*
|
|
180
|
-
* @param {Feature|Geometry} geojson GeoJSON Feature or Geometry Object
|
|
181
|
-
* @returns {Geometry|null} GeoJSON Geometry Object
|
|
182
|
-
* @throws {Error} if geojson is not a Feature or Geometry Object
|
|
183
|
-
* @example
|
|
184
|
-
* var point = {
|
|
185
|
-
* "type": "Feature",
|
|
186
|
-
* "properties": {},
|
|
187
|
-
* "geometry": {
|
|
188
|
-
* "type": "Point",
|
|
189
|
-
* "coordinates": [110, 40]
|
|
190
|
-
* }
|
|
191
|
-
* }
|
|
192
|
-
* var geom = turf.getGeom(point)
|
|
193
|
-
* //={"type": "Point", "coordinates": [110, 40]}
|
|
194
|
-
*/
|
|
195
|
-
function getGeom(geojson) {
|
|
196
|
-
if (geojson.type === "Feature") {
|
|
197
|
-
return geojson.geometry;
|
|
198
|
-
}
|
|
199
|
-
return geojson;
|
|
200
|
-
}
|
|
201
|
-
exports.getGeom = getGeom;
|
|
202
|
-
/**
|
|
203
|
-
* Get GeoJSON object's type, Geometry type is prioritize.
|
|
204
|
-
*
|
|
205
|
-
* @param {GeoJSON} geojson GeoJSON object
|
|
206
|
-
* @param {string} [name="geojson"] name of the variable to display in error message (unused)
|
|
207
|
-
* @returns {string} GeoJSON type
|
|
208
|
-
* @example
|
|
209
|
-
* var point = {
|
|
210
|
-
* "type": "Feature",
|
|
211
|
-
* "properties": {},
|
|
212
|
-
* "geometry": {
|
|
213
|
-
* "type": "Point",
|
|
214
|
-
* "coordinates": [110, 40]
|
|
215
|
-
* }
|
|
216
|
-
* }
|
|
217
|
-
* var geom = turf.getType(point)
|
|
218
|
-
* //="Point"
|
|
219
|
-
*/
|
|
220
|
-
function getType(geojson, _name) {
|
|
221
|
-
if (geojson.type === "FeatureCollection") {
|
|
222
|
-
return "FeatureCollection";
|
|
223
|
-
}
|
|
224
|
-
if (geojson.type === "GeometryCollection") {
|
|
225
|
-
return "GeometryCollection";
|
|
226
|
-
}
|
|
227
|
-
if (geojson.type === "Feature" && geojson.geometry !== null) {
|
|
228
|
-
return geojson.geometry.type;
|
|
229
|
-
}
|
|
230
|
-
return geojson.type;
|
|
231
|
-
}
|
|
232
|
-
exports.getType = getType;
|