@turf/clean-coords 6.5.0 → 7.0.0-alpha.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 CHANGED
@@ -6,13 +6,14 @@
6
6
 
7
7
  Removes redundant coordinates from any GeoJSON Geometry.
8
8
 
9
- **Parameters**
9
+ ### Parameters
10
10
 
11
- - `geojson` **([Geometry][1] \| [Feature][2])** Feature or Geometry
12
- - `options` **[Object][3]** Optional parameters (optional, default `{}`)
13
- - `options.mutate` **[boolean][4]** allows GeoJSON input to be mutated (optional, default `false`)
11
+ * `geojson` **([Geometry][1] | [Feature][2])** Feature or Geometry
12
+ * `options` **[Object][3]** Optional parameters (optional, default `{}`)
14
13
 
15
- **Examples**
14
+ * `options.mutate` **[boolean][4]** allows GeoJSON input to be mutated (optional, default `false`)
15
+
16
+ ### Examples
16
17
 
17
18
  ```javascript
18
19
  var line = turf.lineString([[0, 0], [0, 2], [0, 5], [0, 8], [0, 8], [0, 10]]);
@@ -25,7 +26,7 @@ turf.cleanCoords(multiPoint).geometry.coordinates;
25
26
  //= [[0, 0], [2, 2]]
26
27
  ```
27
28
 
28
- Returns **([Geometry][1] \| [Feature][2])** the cleaned input Feature/Geometry
29
+ Returns **([Geometry][1] | [Feature][2])** the cleaned input Feature/Geometry
29
30
 
30
31
  [1]: https://tools.ietf.org/html/rfc7946#section-3.1
31
32
 
package/dist/es/index.js CHANGED
@@ -19,8 +19,7 @@ import { getCoords, getType } from "@turf/invariant";
19
19
  * turf.cleanCoords(multiPoint).geometry.coordinates;
20
20
  * //= [[0, 0], [2, 2]]
21
21
  */
22
- function cleanCoords(geojson, options) {
23
- if (options === void 0) { options = {}; }
22
+ function cleanCoords(geojson, options = {}) {
24
23
  // Backwards compatible with v4.0
25
24
  var mutate = typeof options === "object" ? options.mutate : options;
26
25
  if (!geojson)
@@ -30,19 +29,19 @@ function cleanCoords(geojson, options) {
30
29
  var newCoords = [];
31
30
  switch (type) {
32
31
  case "LineString":
33
- newCoords = cleanLine(geojson);
32
+ newCoords = cleanLine(geojson, type);
34
33
  break;
35
34
  case "MultiLineString":
36
35
  case "Polygon":
37
36
  getCoords(geojson).forEach(function (line) {
38
- newCoords.push(cleanLine(line));
37
+ newCoords.push(cleanLine(line, type));
39
38
  });
40
39
  break;
41
40
  case "MultiPolygon":
42
41
  getCoords(geojson).forEach(function (polygons) {
43
42
  var polyPoints = [];
44
43
  polygons.forEach(function (ring) {
45
- polyPoints.push(cleanLine(ring));
44
+ polyPoints.push(cleanLine(ring, type));
46
45
  });
47
46
  newCoords.push(polyPoints);
48
47
  });
@@ -86,9 +85,10 @@ function cleanCoords(geojson, options) {
86
85
  *
87
86
  * @private
88
87
  * @param {Array<number>|LineString} line Line
88
+ * @param {string} type Type of geometry
89
89
  * @returns {Array<number>} Cleaned coordinates
90
90
  */
91
- function cleanLine(line) {
91
+ function cleanLine(line, type) {
92
92
  var points = getCoords(line);
93
93
  // handle "clean" segment
94
94
  if (points.length === 2 && !equals(points[0], points[1]))
@@ -113,8 +113,12 @@ function cleanLine(line) {
113
113
  }
114
114
  newPoints.push(points[points.length - 1]);
115
115
  newPointsLength = newPoints.length;
116
- if (equals(points[0], points[points.length - 1]) && newPointsLength < 4)
116
+ // (Multi)Polygons must have at least 4 points, but a closed LineString with only 3 points is acceptable
117
+ if ((type === "Polygon" || type === "MultiPolygon") &&
118
+ equals(points[0], points[points.length - 1]) &&
119
+ newPointsLength < 4) {
117
120
  throw new Error("invalid polygon");
121
+ }
118
122
  if (isPointOnLineSegment(newPoints[newPointsLength - 3], newPoints[newPointsLength - 1], newPoints[newPointsLength - 2]))
119
123
  newPoints.splice(newPoints.length - 2, 1);
120
124
  return newPoints;
File without changes
package/dist/js/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- var helpers_1 = require("@turf/helpers");
4
- var invariant_1 = require("@turf/invariant");
3
+ const helpers_1 = require("@turf/helpers");
4
+ const invariant_1 = require("@turf/invariant");
5
5
  // To-Do => Improve Typescript GeoJSON handling
6
6
  /**
7
7
  * Removes redundant coordinates from any GeoJSON Geometry.
@@ -21,8 +21,7 @@ var invariant_1 = require("@turf/invariant");
21
21
  * turf.cleanCoords(multiPoint).geometry.coordinates;
22
22
  * //= [[0, 0], [2, 2]]
23
23
  */
24
- function cleanCoords(geojson, options) {
25
- if (options === void 0) { options = {}; }
24
+ function cleanCoords(geojson, options = {}) {
26
25
  // Backwards compatible with v4.0
27
26
  var mutate = typeof options === "object" ? options.mutate : options;
28
27
  if (!geojson)
@@ -32,19 +31,19 @@ function cleanCoords(geojson, options) {
32
31
  var newCoords = [];
33
32
  switch (type) {
34
33
  case "LineString":
35
- newCoords = cleanLine(geojson);
34
+ newCoords = cleanLine(geojson, type);
36
35
  break;
37
36
  case "MultiLineString":
38
37
  case "Polygon":
39
38
  invariant_1.getCoords(geojson).forEach(function (line) {
40
- newCoords.push(cleanLine(line));
39
+ newCoords.push(cleanLine(line, type));
41
40
  });
42
41
  break;
43
42
  case "MultiPolygon":
44
43
  invariant_1.getCoords(geojson).forEach(function (polygons) {
45
44
  var polyPoints = [];
46
45
  polygons.forEach(function (ring) {
47
- polyPoints.push(cleanLine(ring));
46
+ polyPoints.push(cleanLine(ring, type));
48
47
  });
49
48
  newCoords.push(polyPoints);
50
49
  });
@@ -88,9 +87,10 @@ function cleanCoords(geojson, options) {
88
87
  *
89
88
  * @private
90
89
  * @param {Array<number>|LineString} line Line
90
+ * @param {string} type Type of geometry
91
91
  * @returns {Array<number>} Cleaned coordinates
92
92
  */
93
- function cleanLine(line) {
93
+ function cleanLine(line, type) {
94
94
  var points = invariant_1.getCoords(line);
95
95
  // handle "clean" segment
96
96
  if (points.length === 2 && !equals(points[0], points[1]))
@@ -115,8 +115,12 @@ function cleanLine(line) {
115
115
  }
116
116
  newPoints.push(points[points.length - 1]);
117
117
  newPointsLength = newPoints.length;
118
- if (equals(points[0], points[points.length - 1]) && newPointsLength < 4)
118
+ // (Multi)Polygons must have at least 4 points, but a closed LineString with only 3 points is acceptable
119
+ if ((type === "Polygon" || type === "MultiPolygon") &&
120
+ equals(points[0], points[points.length - 1]) &&
121
+ newPointsLength < 4) {
119
122
  throw new Error("invalid polygon");
123
+ }
120
124
  if (isPointOnLineSegment(newPoints[newPointsLength - 3], newPoints[newPointsLength - 1], newPoints[newPointsLength - 2]))
121
125
  newPoints.splice(newPoints.length - 2, 1);
122
126
  return newPoints;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turf/clean-coords",
3
- "version": "6.5.0",
3
+ "version": "7.0.0-alpha.0",
4
4
  "description": "turf clean-coords module",
5
5
  "author": "Turf Authors",
6
6
  "contributors": [
@@ -46,10 +46,10 @@
46
46
  "docs": "node ../../scripts/generate-readmes",
47
47
  "test": "npm-run-all test:*",
48
48
  "test:tape": "ts-node -r esm test.js",
49
- "test:types": "tsc --esModuleInterop --noEmit types.ts"
49
+ "test:types": "tsc --esModuleInterop --noEmit --strict types.ts"
50
50
  },
51
51
  "devDependencies": {
52
- "@turf/truncate": "^6.5.0",
52
+ "@turf/truncate": "^7.0.0-alpha.0",
53
53
  "@types/tape": "*",
54
54
  "benchmark": "*",
55
55
  "load-json-file": "*",
@@ -61,8 +61,9 @@
61
61
  "write-json-file": "*"
62
62
  },
63
63
  "dependencies": {
64
- "@turf/helpers": "^6.5.0",
65
- "@turf/invariant": "^6.5.0"
64
+ "@turf/helpers": "^7.0.0-alpha.0",
65
+ "@turf/invariant": "^7.0.0-alpha.0",
66
+ "tslib": "^2.3.0"
66
67
  },
67
- "gitHead": "5375941072b90d489389db22b43bfe809d5e451e"
68
+ "gitHead": "0edc4c491b999e5ace770a61e1cf549f7c004189"
68
69
  }