@turf/clone 4.6.0 → 4.7.3

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 CHANGED
@@ -2,19 +2,17 @@
2
2
 
3
3
  # clone
4
4
 
5
- Returns a cloned copy of the passed GeoJSON Object.
6
- By default it duplicates only the standard GeoJSON fields of the object; if `cloneAll` is set to `true` all
7
- fields of the Object, thus including 'Foreign Members', will be cloned (3-20x slower).
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.
8
7
 
9
8
  **Parameters**
10
9
 
11
10
  - `geojson` **[GeoJSON](http://geojson.org/geojson-spec.html#geojson-objects)** GeoJSON Object
12
- - `cloneAll` **\[[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)]** clones entire GeoJSON object, using JSON.parse(JSON.stringify(geojson)) (optional, default `false`)
13
11
 
14
12
  **Examples**
15
13
 
16
14
  ```javascript
17
- var line = turf.lineString([[-74, 40], [-78, 42], [-82, 35]]);
15
+ var line = turf.lineString([[-74, 40], [-78, 42], [-82, 35]], {color: 'red'});
18
16
 
19
17
  var lineCloned = turf.clone(line);
20
18
  ```
package/index.d.ts CHANGED
@@ -5,6 +5,6 @@ type Types = GeoJSON.FeatureCollection<any> | GeoJSON.Feature<any> | GeoJSON.Geo
5
5
  /**
6
6
  * http://turfjs.org/docs/#clone
7
7
  */
8
- declare function clone<T extends Types>(geojson: T, cloneAll?: boolean): T;
8
+ declare function clone<T extends Types>(geojson: T): T;
9
9
  declare namespace clone { }
10
10
  export = clone;
package/index.js CHANGED
@@ -1,64 +1,83 @@
1
1
  /**
2
- * Returns a cloned copy of the passed GeoJSON Object.
3
- * By default it duplicates only the standard GeoJSON fields of the object; if `cloneAll` is set to `true` all
4
- * fields of the Object, thus including 'Foreign Members', will be cloned (3-20x slower).
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.
5
4
  *
6
5
  * @name clone
7
6
  * @param {GeoJSON} geojson GeoJSON Object
8
- * @param {Boolean} [cloneAll=false] clones entire GeoJSON object, using JSON.parse(JSON.stringify(geojson))
9
7
  * @returns {GeoJSON} cloned GeoJSON Object
10
8
  * @example
11
- * var line = turf.lineString([[-74, 40], [-78, 42], [-82, 35]]);
9
+ * var line = turf.lineString([[-74, 40], [-78, 42], [-82, 35]], {color: 'red'});
12
10
  *
13
11
  * var lineCloned = turf.clone(line);
14
12
  */
15
- module.exports = function (geojson, cloneAll) {
13
+ module.exports = function (geojson) {
16
14
  if (!geojson) throw new Error('geojson is required');
17
- if (cloneAll && typeof cloneAll !== 'boolean') throw new Error('cloneAll must be a Boolean');
18
15
 
19
- // Clone entire object (3-20x slower)
20
- if (cloneAll) return JSON.parse(JSON.stringify(geojson));
21
-
22
- // Clones only GeoJSON fields
23
- return clone(geojson);
16
+ switch (geojson.type) {
17
+ case 'Feature':
18
+ return cloneFeature(geojson);
19
+ case 'FeatureCollection':
20
+ return cloneFeatureCollection(geojson);
21
+ case 'Point':
22
+ case 'LineString':
23
+ case 'Polygon':
24
+ case 'MultiPoint':
25
+ case 'MultiLineString':
26
+ case 'MultiPolygon':
27
+ case 'GeometryCollection':
28
+ return cloneGeometry(geojson);
29
+ default:
30
+ throw new Error('unknown GeoJSON type');
31
+ }
24
32
  };
25
33
 
26
34
  /**
27
- * Clone
35
+ * Clone Feature
28
36
  *
29
37
  * @private
30
- * @param {GeoJSON} geojson GeoJSON Feature or Geometry
31
- * @returns {GeoJSON} cloned Feature
38
+ * @param {Feature<any>} geojson GeoJSON Feature
39
+ * @returns {Feature<any>} cloned Feature
32
40
  */
33
- function clone(geojson) {
34
- // Geometry Object
35
- if (geojson.coordinates) return cloneGeometry(geojson);
36
-
37
- // Feature
38
- if (geojson.type === 'Feature') return cloneFeature(geojson);
39
-
40
- // Feature Collection
41
- if (geojson.type === 'FeatureCollection') return cloneFeatureCollection(geojson);
42
-
43
- // Geometry Collection
44
- if (geojson.type === 'GeometryCollection') return cloneGeometry(geojson);
41
+ function cloneFeature(geojson) {
42
+ var cloned = {type: 'Feature'};
43
+ // Preserve Foreign Members
44
+ Object.keys(geojson).forEach(function (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
+ cloned.geometry = cloneGeometry(geojson.geometry);
57
+ return cloned;
45
58
  }
46
59
 
47
60
  /**
48
- * Clone Feature
61
+ * Clone Properties
49
62
  *
50
63
  * @private
51
- * @param {Feature<any>} feature GeoJSON Feature
52
- * @returns {Feature<any>} cloned Feature
64
+ * @param {Object} properties GeoJSON Properties
65
+ * @returns {Object} cloned Properties
53
66
  */
54
- function cloneFeature(feature) {
55
- var cloned = {
56
- type: 'Feature',
57
- properties: feature.properties || {},
58
- geometry: cloneGeometry(feature.geometry)
59
- };
60
- if (feature.id) cloned.id = feature.id;
61
- if (feature.bbox) cloned.bbox = feature.bbox;
67
+ function cloneProperties(properties) {
68
+ var cloned = {};
69
+ if (!properties) return cloned;
70
+ Object.keys(properties).forEach(function (key) {
71
+ var value = properties[key];
72
+ if (typeof value === 'object') {
73
+ // handle Array
74
+ if (value.length) cloned[key] = value.map(function (item) {
75
+ return item;
76
+ });
77
+ // handle Object
78
+ cloned[key] = cloneProperties(value);
79
+ } else cloned[key] = value;
80
+ });
62
81
  return cloned;
63
82
  }
64
83
 
@@ -70,12 +89,23 @@ function cloneFeature(feature) {
70
89
  * @returns {FeatureCollection<any>} cloned Feature Collection
71
90
  */
72
91
  function cloneFeatureCollection(geojson) {
73
- return {
74
- type: 'FeatureCollection',
75
- features: geojson.features.map(function (feature) {
76
- return cloneFeature(feature);
77
- })
78
- };
92
+ var cloned = {type: 'FeatureCollection'};
93
+
94
+ // Preserve Foreign Members
95
+ Object.keys(geojson).forEach(function (key) {
96
+ switch (key) {
97
+ case 'type':
98
+ case 'features':
99
+ return;
100
+ default:
101
+ cloned[key] = geojson[key];
102
+ }
103
+ });
104
+ // Add features
105
+ cloned.features = geojson.features.map(function (feature) {
106
+ return cloneFeature(feature);
107
+ });
108
+ return cloned;
79
109
  }
80
110
 
81
111
  /**
@@ -86,18 +116,17 @@ function cloneFeatureCollection(geojson) {
86
116
  * @returns {Geometry<any>} cloned Geometry
87
117
  */
88
118
  function cloneGeometry(geometry) {
119
+ var geom = {type: geometry.type};
120
+ if (geometry.bbox) geom.bbox = geometry.bbox;
121
+
89
122
  if (geometry.type === 'GeometryCollection') {
90
- return {
91
- type: 'GeometryCollection',
92
- geometries: geometry.geometries.map(function (geom) {
93
- return cloneGeometry(geom);
94
- })
95
- };
123
+ geom.geometries = geometry.geometries.map(function (geom) {
124
+ return cloneGeometry(geom);
125
+ });
126
+ return geom;
96
127
  }
97
- return {
98
- type: geometry.type,
99
- coordinates: deepSlice(geometry.coordinates)
100
- };
128
+ geom.coordinates = deepSlice(geometry.coordinates);
129
+ return geom;
101
130
  }
102
131
 
103
132
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turf/clone",
3
- "version": "4.6.0",
3
+ "version": "4.7.3",
4
4
  "description": "turf clone module",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -30,8 +30,8 @@
30
30
  },
31
31
  "homepage": "https://github.com/Turfjs/turf",
32
32
  "devDependencies": {
33
- "@turf/helpers": "^4.6.0",
34
- "@turf/meta": "^4.6.0",
33
+ "@turf/helpers": "^4.7.3",
34
+ "@turf/meta": "^4.7.3",
35
35
  "benchmark": "2.1.4",
36
36
  "tape": "4.7.0"
37
37
  }