@turf/clone 7.0.0-alpha.0 → 7.0.0-alpha.110
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 +120 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/{js → cjs}/index.d.ts +15 -2
- package/dist/esm/index.d.mts +28 -0
- package/dist/esm/index.mjs +120 -0
- package/dist/esm/index.mjs.map +1 -0
- package/package.json +30 -24
- package/dist/es/index.js +0 -162
- package/dist/es/package.json +0 -1
- package/dist/js/index.js +0 -164
package/README.md
CHANGED
|
@@ -23,26 +23,21 @@ Returns **[GeoJSON][1]** cloned GeoJSON Object
|
|
|
23
23
|
|
|
24
24
|
[1]: https://tools.ietf.org/html/rfc7946#section-3
|
|
25
25
|
|
|
26
|
-
<!-- This file is automatically generated. Please don't edit it directly
|
|
27
|
-
if you find an error, edit the source file (likely index.js), and re-run
|
|
28
|
-
./scripts/generate-readmes in the turf project. -->
|
|
26
|
+
<!-- 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. -->
|
|
29
27
|
|
|
30
28
|
---
|
|
31
29
|
|
|
32
|
-
This module is part of the [Turfjs project](
|
|
33
|
-
module collection dedicated to geographic algorithms. It is maintained in the
|
|
34
|
-
[Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create
|
|
35
|
-
PRs and issues.
|
|
30
|
+
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.
|
|
36
31
|
|
|
37
32
|
### Installation
|
|
38
33
|
|
|
39
|
-
Install this module individually:
|
|
34
|
+
Install this single module individually:
|
|
40
35
|
|
|
41
36
|
```sh
|
|
42
37
|
$ npm install @turf/clone
|
|
43
38
|
```
|
|
44
39
|
|
|
45
|
-
Or install the
|
|
40
|
+
Or install the all-encompassing @turf/turf module that includes all modules as functions:
|
|
46
41
|
|
|
47
42
|
```sh
|
|
48
43
|
$ npm install @turf/turf
|
|
@@ -0,0 +1,120 @@
|
|
|
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
|
+
function clone(geojson) {
|
|
6
|
+
if (!geojson) {
|
|
7
|
+
throw new Error("geojson is required");
|
|
8
|
+
}
|
|
9
|
+
switch (geojson.type) {
|
|
10
|
+
case "Feature":
|
|
11
|
+
return cloneFeature(geojson);
|
|
12
|
+
case "FeatureCollection":
|
|
13
|
+
return cloneFeatureCollection(geojson);
|
|
14
|
+
case "Point":
|
|
15
|
+
case "LineString":
|
|
16
|
+
case "Polygon":
|
|
17
|
+
case "MultiPoint":
|
|
18
|
+
case "MultiLineString":
|
|
19
|
+
case "MultiPolygon":
|
|
20
|
+
case "GeometryCollection":
|
|
21
|
+
return cloneGeometry(geojson);
|
|
22
|
+
default:
|
|
23
|
+
throw new Error("unknown GeoJSON type");
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
__name(clone, "clone");
|
|
27
|
+
function cloneFeature(geojson) {
|
|
28
|
+
const cloned = { type: "Feature" };
|
|
29
|
+
Object.keys(geojson).forEach((key) => {
|
|
30
|
+
switch (key) {
|
|
31
|
+
case "type":
|
|
32
|
+
case "properties":
|
|
33
|
+
case "geometry":
|
|
34
|
+
return;
|
|
35
|
+
default:
|
|
36
|
+
cloned[key] = geojson[key];
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
cloned.properties = cloneProperties(geojson.properties);
|
|
40
|
+
if (geojson.geometry == null) {
|
|
41
|
+
cloned.geometry = null;
|
|
42
|
+
} else {
|
|
43
|
+
cloned.geometry = cloneGeometry(geojson.geometry);
|
|
44
|
+
}
|
|
45
|
+
return cloned;
|
|
46
|
+
}
|
|
47
|
+
__name(cloneFeature, "cloneFeature");
|
|
48
|
+
function cloneProperties(properties) {
|
|
49
|
+
const cloned = {};
|
|
50
|
+
if (!properties) {
|
|
51
|
+
return cloned;
|
|
52
|
+
}
|
|
53
|
+
Object.keys(properties).forEach((key) => {
|
|
54
|
+
const value = properties[key];
|
|
55
|
+
if (typeof value === "object") {
|
|
56
|
+
if (value === null) {
|
|
57
|
+
cloned[key] = null;
|
|
58
|
+
} else if (Array.isArray(value)) {
|
|
59
|
+
cloned[key] = value.map((item) => {
|
|
60
|
+
return item;
|
|
61
|
+
});
|
|
62
|
+
} else {
|
|
63
|
+
cloned[key] = cloneProperties(value);
|
|
64
|
+
}
|
|
65
|
+
} else {
|
|
66
|
+
cloned[key] = value;
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
return cloned;
|
|
70
|
+
}
|
|
71
|
+
__name(cloneProperties, "cloneProperties");
|
|
72
|
+
function cloneFeatureCollection(geojson) {
|
|
73
|
+
const cloned = { type: "FeatureCollection" };
|
|
74
|
+
Object.keys(geojson).forEach((key) => {
|
|
75
|
+
switch (key) {
|
|
76
|
+
case "type":
|
|
77
|
+
case "features":
|
|
78
|
+
return;
|
|
79
|
+
default:
|
|
80
|
+
cloned[key] = geojson[key];
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
cloned.features = geojson.features.map((feature) => {
|
|
84
|
+
return cloneFeature(feature);
|
|
85
|
+
});
|
|
86
|
+
return cloned;
|
|
87
|
+
}
|
|
88
|
+
__name(cloneFeatureCollection, "cloneFeatureCollection");
|
|
89
|
+
function cloneGeometry(geometry) {
|
|
90
|
+
const geom = { type: geometry.type };
|
|
91
|
+
if (geometry.bbox) {
|
|
92
|
+
geom.bbox = geometry.bbox;
|
|
93
|
+
}
|
|
94
|
+
if (geometry.type === "GeometryCollection") {
|
|
95
|
+
geom.geometries = geometry.geometries.map((g) => {
|
|
96
|
+
return cloneGeometry(g);
|
|
97
|
+
});
|
|
98
|
+
return geom;
|
|
99
|
+
}
|
|
100
|
+
geom.coordinates = deepSlice(geometry.coordinates);
|
|
101
|
+
return geom;
|
|
102
|
+
}
|
|
103
|
+
__name(cloneGeometry, "cloneGeometry");
|
|
104
|
+
function deepSlice(coords) {
|
|
105
|
+
const cloned = coords;
|
|
106
|
+
if (typeof cloned[0] !== "object") {
|
|
107
|
+
return cloned.slice();
|
|
108
|
+
}
|
|
109
|
+
return cloned.map((coord) => {
|
|
110
|
+
return deepSlice(coord);
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
__name(deepSlice, "deepSlice");
|
|
114
|
+
var turf_clone_default = clone;
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
exports.clone = clone; exports.cloneProperties = cloneProperties; exports.default = turf_clone_default;
|
|
120
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../index.ts"],"names":[],"mappings":"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","sourcesContent":["import { Feature, GeoJsonProperties } from \"geojson\";\nimport { AllGeoJSON } from \"@turf/helpers\";\n\n/**\n * Returns a cloned copy of the passed GeoJSON Object, including possible 'Foreign Members'.\n * ~3-5x faster than the common JSON.parse + JSON.stringify combo method.\n *\n * @name clone\n * @param {GeoJSON} geojson GeoJSON Object\n * @returns {GeoJSON} cloned GeoJSON Object\n * @example\n * var line = turf.lineString([[-74, 40], [-78, 42], [-82, 35]], {color: 'red'});\n *\n * var lineCloned = turf.clone(line);\n */\nfunction clone<T extends AllGeoJSON>(geojson: T): T {\n if (!geojson) {\n throw new Error(\"geojson is required\");\n }\n\n switch (geojson.type) {\n case \"Feature\":\n return cloneFeature(geojson);\n case \"FeatureCollection\":\n return cloneFeatureCollection(geojson);\n case \"Point\":\n case \"LineString\":\n case \"Polygon\":\n case \"MultiPoint\":\n case \"MultiLineString\":\n case \"MultiPolygon\":\n case \"GeometryCollection\":\n return cloneGeometry(geojson);\n default:\n throw new Error(\"unknown GeoJSON type\");\n }\n}\n\n/**\n * Clone Feature\n *\n * @private\n * @param {Feature<any>} geojson GeoJSON Feature\n * @returns {Feature<any>} cloned Feature\n */\nfunction cloneFeature(geojson: any) {\n const cloned: any = { type: \"Feature\" };\n // Preserve Foreign Members\n Object.keys(geojson).forEach((key) => {\n switch (key) {\n case \"type\":\n case \"properties\":\n case \"geometry\":\n return;\n default:\n cloned[key] = geojson[key];\n }\n });\n // Add properties & geometry last\n cloned.properties = cloneProperties(geojson.properties);\n if (geojson.geometry == null) {\n cloned.geometry = null;\n } else {\n cloned.geometry = cloneGeometry(geojson.geometry);\n }\n return cloned;\n}\n\n/**\n * Clone Properties\n *\n * @private\n * @param {Object} properties GeoJSON Properties\n * @returns {Object} cloned Properties\n */\nfunction cloneProperties(properties: GeoJsonProperties) {\n const cloned: { [key: string]: any } = {};\n if (!properties) {\n return cloned;\n }\n Object.keys(properties).forEach((key) => {\n const value = properties[key];\n if (typeof value === \"object\") {\n if (value === null) {\n // handle null\n cloned[key] = null;\n } else if (Array.isArray(value)) {\n // handle Array\n cloned[key] = value.map((item) => {\n return item;\n });\n } else {\n // handle generic Object\n cloned[key] = cloneProperties(value);\n }\n } else {\n cloned[key] = value;\n }\n });\n return cloned;\n}\n\n/**\n * Clone Feature Collection\n *\n * @private\n * @param {FeatureCollection<any>} geojson GeoJSON Feature Collection\n * @returns {FeatureCollection<any>} cloned Feature Collection\n */\nfunction cloneFeatureCollection(geojson: any) {\n const cloned: any = { type: \"FeatureCollection\" };\n\n // Preserve Foreign Members\n Object.keys(geojson).forEach((key) => {\n switch (key) {\n case \"type\":\n case \"features\":\n return;\n default:\n cloned[key] = geojson[key];\n }\n });\n // Add features\n cloned.features = geojson.features.map((feature: Feature<any>) => {\n return cloneFeature(feature);\n });\n return cloned;\n}\n\n/**\n * Clone Geometry\n *\n * @private\n * @param {Geometry<any>} geometry GeoJSON Geometry\n * @returns {Geometry<any>} cloned Geometry\n */\nfunction cloneGeometry(geometry: any) {\n const geom: any = { type: geometry.type };\n if (geometry.bbox) {\n geom.bbox = geometry.bbox;\n }\n\n if (geometry.type === \"GeometryCollection\") {\n geom.geometries = geometry.geometries.map((g: any) => {\n return cloneGeometry(g);\n });\n return geom;\n }\n geom.coordinates = deepSlice(geometry.coordinates);\n return geom;\n}\n\n/**\n * Deep Slice coordinates\n *\n * @private\n * @param {Coordinates} coords Coordinates\n * @returns {Coordinates} all coordinates sliced\n */\nfunction deepSlice<C = any[]>(coords: C): C {\n const cloned: any = coords;\n if (typeof cloned[0] !== \"object\") {\n return cloned.slice();\n }\n return cloned.map((coord: any) => {\n return deepSlice(coord);\n });\n}\n\nexport { clone, cloneProperties };\nexport default clone;\n"]}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { GeoJsonProperties } from 'geojson';
|
|
2
|
+
import { AllGeoJSON } from '@turf/helpers';
|
|
3
|
+
|
|
2
4
|
/**
|
|
3
5
|
* Returns a cloned copy of the passed GeoJSON Object, including possible 'Foreign Members'.
|
|
4
6
|
* ~3-5x faster than the common JSON.parse + JSON.stringify combo method.
|
|
@@ -12,4 +14,15 @@ import { AllGeoJSON } from "@turf/helpers";
|
|
|
12
14
|
* var lineCloned = turf.clone(line);
|
|
13
15
|
*/
|
|
14
16
|
declare function clone<T extends AllGeoJSON>(geojson: T): T;
|
|
15
|
-
|
|
17
|
+
/**
|
|
18
|
+
* Clone Properties
|
|
19
|
+
*
|
|
20
|
+
* @private
|
|
21
|
+
* @param {Object} properties GeoJSON Properties
|
|
22
|
+
* @returns {Object} cloned Properties
|
|
23
|
+
*/
|
|
24
|
+
declare function cloneProperties(properties: GeoJsonProperties): {
|
|
25
|
+
[key: string]: any;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export { clone, cloneProperties, clone as default };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { GeoJsonProperties } from 'geojson';
|
|
2
|
+
import { AllGeoJSON } from '@turf/helpers';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Returns a cloned copy of the passed GeoJSON Object, including possible 'Foreign Members'.
|
|
6
|
+
* ~3-5x faster than the common JSON.parse + JSON.stringify combo method.
|
|
7
|
+
*
|
|
8
|
+
* @name clone
|
|
9
|
+
* @param {GeoJSON} geojson GeoJSON Object
|
|
10
|
+
* @returns {GeoJSON} cloned GeoJSON Object
|
|
11
|
+
* @example
|
|
12
|
+
* var line = turf.lineString([[-74, 40], [-78, 42], [-82, 35]], {color: 'red'});
|
|
13
|
+
*
|
|
14
|
+
* var lineCloned = turf.clone(line);
|
|
15
|
+
*/
|
|
16
|
+
declare function clone<T extends AllGeoJSON>(geojson: T): T;
|
|
17
|
+
/**
|
|
18
|
+
* Clone Properties
|
|
19
|
+
*
|
|
20
|
+
* @private
|
|
21
|
+
* @param {Object} properties GeoJSON Properties
|
|
22
|
+
* @returns {Object} cloned Properties
|
|
23
|
+
*/
|
|
24
|
+
declare function cloneProperties(properties: GeoJsonProperties): {
|
|
25
|
+
[key: string]: any;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export { clone, cloneProperties, clone as default };
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// index.ts
|
|
5
|
+
function clone(geojson) {
|
|
6
|
+
if (!geojson) {
|
|
7
|
+
throw new Error("geojson is required");
|
|
8
|
+
}
|
|
9
|
+
switch (geojson.type) {
|
|
10
|
+
case "Feature":
|
|
11
|
+
return cloneFeature(geojson);
|
|
12
|
+
case "FeatureCollection":
|
|
13
|
+
return cloneFeatureCollection(geojson);
|
|
14
|
+
case "Point":
|
|
15
|
+
case "LineString":
|
|
16
|
+
case "Polygon":
|
|
17
|
+
case "MultiPoint":
|
|
18
|
+
case "MultiLineString":
|
|
19
|
+
case "MultiPolygon":
|
|
20
|
+
case "GeometryCollection":
|
|
21
|
+
return cloneGeometry(geojson);
|
|
22
|
+
default:
|
|
23
|
+
throw new Error("unknown GeoJSON type");
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
__name(clone, "clone");
|
|
27
|
+
function cloneFeature(geojson) {
|
|
28
|
+
const cloned = { type: "Feature" };
|
|
29
|
+
Object.keys(geojson).forEach((key) => {
|
|
30
|
+
switch (key) {
|
|
31
|
+
case "type":
|
|
32
|
+
case "properties":
|
|
33
|
+
case "geometry":
|
|
34
|
+
return;
|
|
35
|
+
default:
|
|
36
|
+
cloned[key] = geojson[key];
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
cloned.properties = cloneProperties(geojson.properties);
|
|
40
|
+
if (geojson.geometry == null) {
|
|
41
|
+
cloned.geometry = null;
|
|
42
|
+
} else {
|
|
43
|
+
cloned.geometry = cloneGeometry(geojson.geometry);
|
|
44
|
+
}
|
|
45
|
+
return cloned;
|
|
46
|
+
}
|
|
47
|
+
__name(cloneFeature, "cloneFeature");
|
|
48
|
+
function cloneProperties(properties) {
|
|
49
|
+
const cloned = {};
|
|
50
|
+
if (!properties) {
|
|
51
|
+
return cloned;
|
|
52
|
+
}
|
|
53
|
+
Object.keys(properties).forEach((key) => {
|
|
54
|
+
const value = properties[key];
|
|
55
|
+
if (typeof value === "object") {
|
|
56
|
+
if (value === null) {
|
|
57
|
+
cloned[key] = null;
|
|
58
|
+
} else if (Array.isArray(value)) {
|
|
59
|
+
cloned[key] = value.map((item) => {
|
|
60
|
+
return item;
|
|
61
|
+
});
|
|
62
|
+
} else {
|
|
63
|
+
cloned[key] = cloneProperties(value);
|
|
64
|
+
}
|
|
65
|
+
} else {
|
|
66
|
+
cloned[key] = value;
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
return cloned;
|
|
70
|
+
}
|
|
71
|
+
__name(cloneProperties, "cloneProperties");
|
|
72
|
+
function cloneFeatureCollection(geojson) {
|
|
73
|
+
const cloned = { type: "FeatureCollection" };
|
|
74
|
+
Object.keys(geojson).forEach((key) => {
|
|
75
|
+
switch (key) {
|
|
76
|
+
case "type":
|
|
77
|
+
case "features":
|
|
78
|
+
return;
|
|
79
|
+
default:
|
|
80
|
+
cloned[key] = geojson[key];
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
cloned.features = geojson.features.map((feature) => {
|
|
84
|
+
return cloneFeature(feature);
|
|
85
|
+
});
|
|
86
|
+
return cloned;
|
|
87
|
+
}
|
|
88
|
+
__name(cloneFeatureCollection, "cloneFeatureCollection");
|
|
89
|
+
function cloneGeometry(geometry) {
|
|
90
|
+
const geom = { type: geometry.type };
|
|
91
|
+
if (geometry.bbox) {
|
|
92
|
+
geom.bbox = geometry.bbox;
|
|
93
|
+
}
|
|
94
|
+
if (geometry.type === "GeometryCollection") {
|
|
95
|
+
geom.geometries = geometry.geometries.map((g) => {
|
|
96
|
+
return cloneGeometry(g);
|
|
97
|
+
});
|
|
98
|
+
return geom;
|
|
99
|
+
}
|
|
100
|
+
geom.coordinates = deepSlice(geometry.coordinates);
|
|
101
|
+
return geom;
|
|
102
|
+
}
|
|
103
|
+
__name(cloneGeometry, "cloneGeometry");
|
|
104
|
+
function deepSlice(coords) {
|
|
105
|
+
const cloned = coords;
|
|
106
|
+
if (typeof cloned[0] !== "object") {
|
|
107
|
+
return cloned.slice();
|
|
108
|
+
}
|
|
109
|
+
return cloned.map((coord) => {
|
|
110
|
+
return deepSlice(coord);
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
__name(deepSlice, "deepSlice");
|
|
114
|
+
var turf_clone_default = clone;
|
|
115
|
+
export {
|
|
116
|
+
clone,
|
|
117
|
+
cloneProperties,
|
|
118
|
+
turf_clone_default as default
|
|
119
|
+
};
|
|
120
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../index.ts"],"sourcesContent":["import { Feature, GeoJsonProperties } from \"geojson\";\nimport { AllGeoJSON } from \"@turf/helpers\";\n\n/**\n * Returns a cloned copy of the passed GeoJSON Object, including possible 'Foreign Members'.\n * ~3-5x faster than the common JSON.parse + JSON.stringify combo method.\n *\n * @name clone\n * @param {GeoJSON} geojson GeoJSON Object\n * @returns {GeoJSON} cloned GeoJSON Object\n * @example\n * var line = turf.lineString([[-74, 40], [-78, 42], [-82, 35]], {color: 'red'});\n *\n * var lineCloned = turf.clone(line);\n */\nfunction clone<T extends AllGeoJSON>(geojson: T): T {\n if (!geojson) {\n throw new Error(\"geojson is required\");\n }\n\n switch (geojson.type) {\n case \"Feature\":\n return cloneFeature(geojson);\n case \"FeatureCollection\":\n return cloneFeatureCollection(geojson);\n case \"Point\":\n case \"LineString\":\n case \"Polygon\":\n case \"MultiPoint\":\n case \"MultiLineString\":\n case \"MultiPolygon\":\n case \"GeometryCollection\":\n return cloneGeometry(geojson);\n default:\n throw new Error(\"unknown GeoJSON type\");\n }\n}\n\n/**\n * Clone Feature\n *\n * @private\n * @param {Feature<any>} geojson GeoJSON Feature\n * @returns {Feature<any>} cloned Feature\n */\nfunction cloneFeature(geojson: any) {\n const cloned: any = { type: \"Feature\" };\n // Preserve Foreign Members\n Object.keys(geojson).forEach((key) => {\n switch (key) {\n case \"type\":\n case \"properties\":\n case \"geometry\":\n return;\n default:\n cloned[key] = geojson[key];\n }\n });\n // Add properties & geometry last\n cloned.properties = cloneProperties(geojson.properties);\n if (geojson.geometry == null) {\n cloned.geometry = null;\n } else {\n cloned.geometry = cloneGeometry(geojson.geometry);\n }\n return cloned;\n}\n\n/**\n * Clone Properties\n *\n * @private\n * @param {Object} properties GeoJSON Properties\n * @returns {Object} cloned Properties\n */\nfunction cloneProperties(properties: GeoJsonProperties) {\n const cloned: { [key: string]: any } = {};\n if (!properties) {\n return cloned;\n }\n Object.keys(properties).forEach((key) => {\n const value = properties[key];\n if (typeof value === \"object\") {\n if (value === null) {\n // handle null\n cloned[key] = null;\n } else if (Array.isArray(value)) {\n // handle Array\n cloned[key] = value.map((item) => {\n return item;\n });\n } else {\n // handle generic Object\n cloned[key] = cloneProperties(value);\n }\n } else {\n cloned[key] = value;\n }\n });\n return cloned;\n}\n\n/**\n * Clone Feature Collection\n *\n * @private\n * @param {FeatureCollection<any>} geojson GeoJSON Feature Collection\n * @returns {FeatureCollection<any>} cloned Feature Collection\n */\nfunction cloneFeatureCollection(geojson: any) {\n const cloned: any = { type: \"FeatureCollection\" };\n\n // Preserve Foreign Members\n Object.keys(geojson).forEach((key) => {\n switch (key) {\n case \"type\":\n case \"features\":\n return;\n default:\n cloned[key] = geojson[key];\n }\n });\n // Add features\n cloned.features = geojson.features.map((feature: Feature<any>) => {\n return cloneFeature(feature);\n });\n return cloned;\n}\n\n/**\n * Clone Geometry\n *\n * @private\n * @param {Geometry<any>} geometry GeoJSON Geometry\n * @returns {Geometry<any>} cloned Geometry\n */\nfunction cloneGeometry(geometry: any) {\n const geom: any = { type: geometry.type };\n if (geometry.bbox) {\n geom.bbox = geometry.bbox;\n }\n\n if (geometry.type === \"GeometryCollection\") {\n geom.geometries = geometry.geometries.map((g: any) => {\n return cloneGeometry(g);\n });\n return geom;\n }\n geom.coordinates = deepSlice(geometry.coordinates);\n return geom;\n}\n\n/**\n * Deep Slice coordinates\n *\n * @private\n * @param {Coordinates} coords Coordinates\n * @returns {Coordinates} all coordinates sliced\n */\nfunction deepSlice<C = any[]>(coords: C): C {\n const cloned: any = coords;\n if (typeof cloned[0] !== \"object\") {\n return cloned.slice();\n }\n return cloned.map((coord: any) => {\n return deepSlice(coord);\n });\n}\n\nexport { clone, cloneProperties };\nexport default clone;\n"],"mappings":"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","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turf/clone",
|
|
3
|
-
"version": "7.0.0-alpha.
|
|
3
|
+
"version": "7.0.0-alpha.110+1411d63a7",
|
|
4
4
|
"description": "turf clone module",
|
|
5
5
|
"author": "Turf Authors",
|
|
6
6
|
"contributors": [
|
|
@@ -23,43 +23,49 @@
|
|
|
23
23
|
"turf",
|
|
24
24
|
"clone"
|
|
25
25
|
],
|
|
26
|
-
"
|
|
27
|
-
"
|
|
26
|
+
"type": "commonjs",
|
|
27
|
+
"main": "dist/cjs/index.cjs",
|
|
28
|
+
"module": "dist/esm/index.mjs",
|
|
29
|
+
"types": "dist/cjs/index.d.ts",
|
|
28
30
|
"exports": {
|
|
29
31
|
"./package.json": "./package.json",
|
|
30
32
|
".": {
|
|
31
|
-
"import":
|
|
32
|
-
|
|
33
|
+
"import": {
|
|
34
|
+
"types": "./dist/esm/index.d.mts",
|
|
35
|
+
"default": "./dist/esm/index.mjs"
|
|
36
|
+
},
|
|
37
|
+
"require": {
|
|
38
|
+
"types": "./dist/cjs/index.d.ts",
|
|
39
|
+
"default": "./dist/cjs/index.cjs"
|
|
40
|
+
}
|
|
33
41
|
}
|
|
34
42
|
},
|
|
35
|
-
"types": "dist/js/index.d.ts",
|
|
36
43
|
"sideEffects": false,
|
|
37
44
|
"files": [
|
|
38
45
|
"dist"
|
|
39
46
|
],
|
|
40
47
|
"scripts": {
|
|
41
|
-
"bench": "
|
|
42
|
-
"build": "
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"test": "npm-run-all test:*",
|
|
47
|
-
"test:tape": "ts-node -r esm test.js",
|
|
48
|
+
"bench": "tsx bench.ts",
|
|
49
|
+
"build": "tsup --config ../../tsup.config.ts",
|
|
50
|
+
"docs": "tsx ../../scripts/generate-readmes.ts",
|
|
51
|
+
"test": "npm-run-all --npm-path npm test:*",
|
|
52
|
+
"test:tape": "tsx test.ts",
|
|
48
53
|
"test:types": "tsc --esModuleInterop --noEmit --strict types.ts"
|
|
49
54
|
},
|
|
50
55
|
"devDependencies": {
|
|
51
|
-
"@turf/meta": "^7.0.0-alpha.
|
|
52
|
-
"@types/
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
56
|
+
"@turf/meta": "^7.0.0-alpha.110+1411d63a7",
|
|
57
|
+
"@types/benchmark": "^2.1.5",
|
|
58
|
+
"@types/tape": "^4.2.32",
|
|
59
|
+
"benchmark": "^2.1.4",
|
|
60
|
+
"npm-run-all": "^4.1.5",
|
|
61
|
+
"tape": "^5.7.2",
|
|
62
|
+
"tsup": "^8.0.1",
|
|
63
|
+
"tsx": "^4.6.2",
|
|
64
|
+
"typescript": "^5.2.2"
|
|
59
65
|
},
|
|
60
66
|
"dependencies": {
|
|
61
|
-
"@turf/helpers": "^7.0.0-alpha.
|
|
62
|
-
"tslib": "^2.
|
|
67
|
+
"@turf/helpers": "^7.0.0-alpha.110+1411d63a7",
|
|
68
|
+
"tslib": "^2.6.2"
|
|
63
69
|
},
|
|
64
|
-
"gitHead": "
|
|
70
|
+
"gitHead": "1411d63a74c275c9216fe48e9d3cb2d48a359068"
|
|
65
71
|
}
|
package/dist/es/index.js
DELETED
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Returns a cloned copy of the passed GeoJSON Object, including possible 'Foreign Members'.
|
|
3
|
-
* ~3-5x faster than the common JSON.parse + JSON.stringify combo method.
|
|
4
|
-
*
|
|
5
|
-
* @name clone
|
|
6
|
-
* @param {GeoJSON} geojson GeoJSON Object
|
|
7
|
-
* @returns {GeoJSON} cloned GeoJSON Object
|
|
8
|
-
* @example
|
|
9
|
-
* var line = turf.lineString([[-74, 40], [-78, 42], [-82, 35]], {color: 'red'});
|
|
10
|
-
*
|
|
11
|
-
* var lineCloned = turf.clone(line);
|
|
12
|
-
*/
|
|
13
|
-
function clone(geojson) {
|
|
14
|
-
if (!geojson) {
|
|
15
|
-
throw new Error("geojson is required");
|
|
16
|
-
}
|
|
17
|
-
switch (geojson.type) {
|
|
18
|
-
case "Feature":
|
|
19
|
-
return cloneFeature(geojson);
|
|
20
|
-
case "FeatureCollection":
|
|
21
|
-
return cloneFeatureCollection(geojson);
|
|
22
|
-
case "Point":
|
|
23
|
-
case "LineString":
|
|
24
|
-
case "Polygon":
|
|
25
|
-
case "MultiPoint":
|
|
26
|
-
case "MultiLineString":
|
|
27
|
-
case "MultiPolygon":
|
|
28
|
-
case "GeometryCollection":
|
|
29
|
-
return cloneGeometry(geojson);
|
|
30
|
-
default:
|
|
31
|
-
throw new Error("unknown GeoJSON type");
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Clone Feature
|
|
36
|
-
*
|
|
37
|
-
* @private
|
|
38
|
-
* @param {Feature<any>} geojson GeoJSON Feature
|
|
39
|
-
* @returns {Feature<any>} cloned Feature
|
|
40
|
-
*/
|
|
41
|
-
function cloneFeature(geojson) {
|
|
42
|
-
const cloned = { type: "Feature" };
|
|
43
|
-
// Preserve Foreign Members
|
|
44
|
-
Object.keys(geojson).forEach((key) => {
|
|
45
|
-
switch (key) {
|
|
46
|
-
case "type":
|
|
47
|
-
case "properties":
|
|
48
|
-
case "geometry":
|
|
49
|
-
return;
|
|
50
|
-
default:
|
|
51
|
-
cloned[key] = geojson[key];
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
// Add properties & geometry last
|
|
55
|
-
cloned.properties = cloneProperties(geojson.properties);
|
|
56
|
-
if (geojson.geometry == null) {
|
|
57
|
-
cloned.geometry = null;
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
cloned.geometry = cloneGeometry(geojson.geometry);
|
|
61
|
-
}
|
|
62
|
-
return cloned;
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Clone Properties
|
|
66
|
-
*
|
|
67
|
-
* @private
|
|
68
|
-
* @param {Object} properties GeoJSON Properties
|
|
69
|
-
* @returns {Object} cloned Properties
|
|
70
|
-
*/
|
|
71
|
-
function cloneProperties(properties) {
|
|
72
|
-
const cloned = {};
|
|
73
|
-
if (!properties) {
|
|
74
|
-
return cloned;
|
|
75
|
-
}
|
|
76
|
-
Object.keys(properties).forEach((key) => {
|
|
77
|
-
const value = properties[key];
|
|
78
|
-
if (typeof value === "object") {
|
|
79
|
-
if (value === null) {
|
|
80
|
-
// handle null
|
|
81
|
-
cloned[key] = null;
|
|
82
|
-
}
|
|
83
|
-
else if (Array.isArray(value)) {
|
|
84
|
-
// handle Array
|
|
85
|
-
cloned[key] = value.map((item) => {
|
|
86
|
-
return item;
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
// handle generic Object
|
|
91
|
-
cloned[key] = cloneProperties(value);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
else {
|
|
95
|
-
cloned[key] = value;
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
return cloned;
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* Clone Feature Collection
|
|
102
|
-
*
|
|
103
|
-
* @private
|
|
104
|
-
* @param {FeatureCollection<any>} geojson GeoJSON Feature Collection
|
|
105
|
-
* @returns {FeatureCollection<any>} cloned Feature Collection
|
|
106
|
-
*/
|
|
107
|
-
function cloneFeatureCollection(geojson) {
|
|
108
|
-
const cloned = { type: "FeatureCollection" };
|
|
109
|
-
// Preserve Foreign Members
|
|
110
|
-
Object.keys(geojson).forEach((key) => {
|
|
111
|
-
switch (key) {
|
|
112
|
-
case "type":
|
|
113
|
-
case "features":
|
|
114
|
-
return;
|
|
115
|
-
default:
|
|
116
|
-
cloned[key] = geojson[key];
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
// Add features
|
|
120
|
-
cloned.features = geojson.features.map((feature) => {
|
|
121
|
-
return cloneFeature(feature);
|
|
122
|
-
});
|
|
123
|
-
return cloned;
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Clone Geometry
|
|
127
|
-
*
|
|
128
|
-
* @private
|
|
129
|
-
* @param {Geometry<any>} geometry GeoJSON Geometry
|
|
130
|
-
* @returns {Geometry<any>} cloned Geometry
|
|
131
|
-
*/
|
|
132
|
-
function cloneGeometry(geometry) {
|
|
133
|
-
const geom = { type: geometry.type };
|
|
134
|
-
if (geometry.bbox) {
|
|
135
|
-
geom.bbox = geometry.bbox;
|
|
136
|
-
}
|
|
137
|
-
if (geometry.type === "GeometryCollection") {
|
|
138
|
-
geom.geometries = geometry.geometries.map((g) => {
|
|
139
|
-
return cloneGeometry(g);
|
|
140
|
-
});
|
|
141
|
-
return geom;
|
|
142
|
-
}
|
|
143
|
-
geom.coordinates = deepSlice(geometry.coordinates);
|
|
144
|
-
return geom;
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* Deep Slice coordinates
|
|
148
|
-
*
|
|
149
|
-
* @private
|
|
150
|
-
* @param {Coordinates} coords Coordinates
|
|
151
|
-
* @returns {Coordinates} all coordinates sliced
|
|
152
|
-
*/
|
|
153
|
-
function deepSlice(coords) {
|
|
154
|
-
const cloned = coords;
|
|
155
|
-
if (typeof cloned[0] !== "object") {
|
|
156
|
-
return cloned.slice();
|
|
157
|
-
}
|
|
158
|
-
return cloned.map((coord) => {
|
|
159
|
-
return deepSlice(coord);
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
export default clone;
|
package/dist/es/package.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"type":"module"}
|
package/dist/js/index.js
DELETED
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
/**
|
|
4
|
-
* Returns a cloned copy of the passed GeoJSON Object, including possible 'Foreign Members'.
|
|
5
|
-
* ~3-5x faster than the common JSON.parse + JSON.stringify combo method.
|
|
6
|
-
*
|
|
7
|
-
* @name clone
|
|
8
|
-
* @param {GeoJSON} geojson GeoJSON Object
|
|
9
|
-
* @returns {GeoJSON} cloned GeoJSON Object
|
|
10
|
-
* @example
|
|
11
|
-
* var line = turf.lineString([[-74, 40], [-78, 42], [-82, 35]], {color: 'red'});
|
|
12
|
-
*
|
|
13
|
-
* var lineCloned = turf.clone(line);
|
|
14
|
-
*/
|
|
15
|
-
function clone(geojson) {
|
|
16
|
-
if (!geojson) {
|
|
17
|
-
throw new Error("geojson is required");
|
|
18
|
-
}
|
|
19
|
-
switch (geojson.type) {
|
|
20
|
-
case "Feature":
|
|
21
|
-
return cloneFeature(geojson);
|
|
22
|
-
case "FeatureCollection":
|
|
23
|
-
return cloneFeatureCollection(geojson);
|
|
24
|
-
case "Point":
|
|
25
|
-
case "LineString":
|
|
26
|
-
case "Polygon":
|
|
27
|
-
case "MultiPoint":
|
|
28
|
-
case "MultiLineString":
|
|
29
|
-
case "MultiPolygon":
|
|
30
|
-
case "GeometryCollection":
|
|
31
|
-
return cloneGeometry(geojson);
|
|
32
|
-
default:
|
|
33
|
-
throw new Error("unknown GeoJSON type");
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Clone Feature
|
|
38
|
-
*
|
|
39
|
-
* @private
|
|
40
|
-
* @param {Feature<any>} geojson GeoJSON Feature
|
|
41
|
-
* @returns {Feature<any>} cloned Feature
|
|
42
|
-
*/
|
|
43
|
-
function cloneFeature(geojson) {
|
|
44
|
-
const cloned = { type: "Feature" };
|
|
45
|
-
// Preserve Foreign Members
|
|
46
|
-
Object.keys(geojson).forEach((key) => {
|
|
47
|
-
switch (key) {
|
|
48
|
-
case "type":
|
|
49
|
-
case "properties":
|
|
50
|
-
case "geometry":
|
|
51
|
-
return;
|
|
52
|
-
default:
|
|
53
|
-
cloned[key] = geojson[key];
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
// Add properties & geometry last
|
|
57
|
-
cloned.properties = cloneProperties(geojson.properties);
|
|
58
|
-
if (geojson.geometry == null) {
|
|
59
|
-
cloned.geometry = null;
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
cloned.geometry = cloneGeometry(geojson.geometry);
|
|
63
|
-
}
|
|
64
|
-
return cloned;
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Clone Properties
|
|
68
|
-
*
|
|
69
|
-
* @private
|
|
70
|
-
* @param {Object} properties GeoJSON Properties
|
|
71
|
-
* @returns {Object} cloned Properties
|
|
72
|
-
*/
|
|
73
|
-
function cloneProperties(properties) {
|
|
74
|
-
const cloned = {};
|
|
75
|
-
if (!properties) {
|
|
76
|
-
return cloned;
|
|
77
|
-
}
|
|
78
|
-
Object.keys(properties).forEach((key) => {
|
|
79
|
-
const value = properties[key];
|
|
80
|
-
if (typeof value === "object") {
|
|
81
|
-
if (value === null) {
|
|
82
|
-
// handle null
|
|
83
|
-
cloned[key] = null;
|
|
84
|
-
}
|
|
85
|
-
else if (Array.isArray(value)) {
|
|
86
|
-
// handle Array
|
|
87
|
-
cloned[key] = value.map((item) => {
|
|
88
|
-
return item;
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
else {
|
|
92
|
-
// handle generic Object
|
|
93
|
-
cloned[key] = cloneProperties(value);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
97
|
-
cloned[key] = value;
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
return cloned;
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Clone Feature Collection
|
|
104
|
-
*
|
|
105
|
-
* @private
|
|
106
|
-
* @param {FeatureCollection<any>} geojson GeoJSON Feature Collection
|
|
107
|
-
* @returns {FeatureCollection<any>} cloned Feature Collection
|
|
108
|
-
*/
|
|
109
|
-
function cloneFeatureCollection(geojson) {
|
|
110
|
-
const cloned = { type: "FeatureCollection" };
|
|
111
|
-
// Preserve Foreign Members
|
|
112
|
-
Object.keys(geojson).forEach((key) => {
|
|
113
|
-
switch (key) {
|
|
114
|
-
case "type":
|
|
115
|
-
case "features":
|
|
116
|
-
return;
|
|
117
|
-
default:
|
|
118
|
-
cloned[key] = geojson[key];
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
// Add features
|
|
122
|
-
cloned.features = geojson.features.map((feature) => {
|
|
123
|
-
return cloneFeature(feature);
|
|
124
|
-
});
|
|
125
|
-
return cloned;
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* Clone Geometry
|
|
129
|
-
*
|
|
130
|
-
* @private
|
|
131
|
-
* @param {Geometry<any>} geometry GeoJSON Geometry
|
|
132
|
-
* @returns {Geometry<any>} cloned Geometry
|
|
133
|
-
*/
|
|
134
|
-
function cloneGeometry(geometry) {
|
|
135
|
-
const geom = { type: geometry.type };
|
|
136
|
-
if (geometry.bbox) {
|
|
137
|
-
geom.bbox = geometry.bbox;
|
|
138
|
-
}
|
|
139
|
-
if (geometry.type === "GeometryCollection") {
|
|
140
|
-
geom.geometries = geometry.geometries.map((g) => {
|
|
141
|
-
return cloneGeometry(g);
|
|
142
|
-
});
|
|
143
|
-
return geom;
|
|
144
|
-
}
|
|
145
|
-
geom.coordinates = deepSlice(geometry.coordinates);
|
|
146
|
-
return geom;
|
|
147
|
-
}
|
|
148
|
-
/**
|
|
149
|
-
* Deep Slice coordinates
|
|
150
|
-
*
|
|
151
|
-
* @private
|
|
152
|
-
* @param {Coordinates} coords Coordinates
|
|
153
|
-
* @returns {Coordinates} all coordinates sliced
|
|
154
|
-
*/
|
|
155
|
-
function deepSlice(coords) {
|
|
156
|
-
const cloned = coords;
|
|
157
|
-
if (typeof cloned[0] !== "object") {
|
|
158
|
-
return cloned.slice();
|
|
159
|
-
}
|
|
160
|
-
return cloned.map((coord) => {
|
|
161
|
-
return deepSlice(coord);
|
|
162
|
-
});
|
|
163
|
-
}
|
|
164
|
-
exports.default = clone;
|