@turf/truncate 7.0.0-alpha.2 → 7.1.0-alpha.7

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
@@ -41,26 +41,21 @@ Returns **[GeoJSON][1]** layer with truncated geometry
41
41
 
42
42
  [4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
43
43
 
44
- <!-- This file is automatically generated. Please don't edit it directly:
45
- if you find an error, edit the source file (likely index.js), and re-run
46
- ./scripts/generate-readmes in the turf project. -->
44
+ <!-- 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. -->
47
45
 
48
46
  ---
49
47
 
50
- This module is part of the [Turfjs project](http://turfjs.org/), an open source
51
- module collection dedicated to geographic algorithms. It is maintained in the
52
- [Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create
53
- PRs and issues.
48
+ 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.
54
49
 
55
50
  ### Installation
56
51
 
57
- Install this module individually:
52
+ Install this single module individually:
58
53
 
59
54
  ```sh
60
55
  $ npm install @turf/truncate
61
56
  ```
62
57
 
63
- Or install the Turf module that includes it as a function:
58
+ Or install the all-encompassing @turf/turf module that includes all modules as functions:
64
59
 
65
60
  ```sh
66
61
  $ npm install @turf/turf
@@ -0,0 +1,40 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.ts
2
+ var _meta = require('@turf/meta');
3
+ var _helpers = require('@turf/helpers');
4
+ function truncate(geojson, options) {
5
+ options = options != null ? options : {};
6
+ if (!_helpers.isObject.call(void 0, options))
7
+ throw new Error("options is invalid");
8
+ var precision = options.precision;
9
+ var coordinates = options.coordinates;
10
+ var mutate = options.mutate;
11
+ precision = precision === void 0 || precision === null || isNaN(precision) ? 6 : precision;
12
+ coordinates = coordinates === void 0 || coordinates === null || isNaN(coordinates) ? 3 : coordinates;
13
+ if (!geojson)
14
+ throw new Error("<geojson> is required");
15
+ if (typeof precision !== "number")
16
+ throw new Error("<precision> must be a number");
17
+ if (typeof coordinates !== "number")
18
+ throw new Error("<coordinates> must be a number");
19
+ if (mutate === false || mutate === void 0)
20
+ geojson = JSON.parse(JSON.stringify(geojson));
21
+ var factor = Math.pow(10, precision);
22
+ _meta.coordEach.call(void 0, geojson, function(coords) {
23
+ truncateCoords(coords, factor, coordinates);
24
+ });
25
+ return geojson;
26
+ }
27
+ function truncateCoords(coords, factor, coordinates) {
28
+ if (coords.length > coordinates)
29
+ coords.splice(coordinates, coords.length);
30
+ for (var i = 0; i < coords.length; i++) {
31
+ coords[i] = Math.round(coords[i] * factor) / factor;
32
+ }
33
+ return coords;
34
+ }
35
+ var turf_truncate_default = truncate;
36
+
37
+
38
+
39
+ exports.default = turf_truncate_default; exports.truncate = truncate;
40
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../index.ts"],"names":[],"mappings":";AAAA,SAAS,iBAAiB;AAC1B,SAAqB,gBAAgB;AAyBrC,SAAS,SACP,SACA,SAKG;AAEH,YAAU,4BAAW,CAAC;AACtB,MAAI,CAAC,SAAS,OAAO;AAAG,UAAM,IAAI,MAAM,oBAAoB;AAC5D,MAAI,YAAY,QAAQ;AACxB,MAAI,cAAc,QAAQ;AAC1B,MAAI,SAAS,QAAQ;AAGrB,cACE,cAAc,UAAa,cAAc,QAAQ,MAAM,SAAS,IAC5D,IACA;AACN,gBACE,gBAAgB,UAAa,gBAAgB,QAAQ,MAAM,WAAW,IAClE,IACA;AAGN,MAAI,CAAC;AAAS,UAAM,IAAI,MAAM,uBAAuB;AACrD,MAAI,OAAO,cAAc;AACvB,UAAM,IAAI,MAAM,8BAA8B;AAChD,MAAI,OAAO,gBAAgB;AACzB,UAAM,IAAI,MAAM,gCAAgC;AAGlD,MAAI,WAAW,SAAS,WAAW;AACjC,cAAU,KAAK,MAAM,KAAK,UAAU,OAAO,CAAC;AAE9C,MAAI,SAAS,KAAK,IAAI,IAAI,SAAS;AAGnC,YAAU,SAAS,SAAU,QAAQ;AACnC,mBAAe,QAAQ,QAAQ,WAAY;AAAA,EAC7C,CAAC;AACD,SAAO;AACT;AAWA,SAAS,eAAe,QAAkB,QAAgB,aAAqB;AAE7E,MAAI,OAAO,SAAS;AAAa,WAAO,OAAO,aAAa,OAAO,MAAM;AAGzE,WAAS,IAAI,GAAG,IAAI,OAAO,QAAQ,KAAK;AACtC,WAAO,CAAC,IAAI,KAAK,MAAM,OAAO,CAAC,IAAI,MAAM,IAAI;AAAA,EAC/C;AACA,SAAO;AACT;AAGA,IAAO,wBAAQ","sourcesContent":["import { coordEach } from \"@turf/meta\";\nimport { AllGeoJSON, isObject } from \"@turf/helpers\";\n\n/**\n * Takes a GeoJSON Feature or FeatureCollection and truncates the precision of the geometry.\n *\n * @name truncate\n * @param {GeoJSON} geojson any GeoJSON Feature, FeatureCollection, Geometry or GeometryCollection.\n * @param {Object} [options={}] Optional parameters\n * @param {number} [options.precision=6] coordinate decimal precision\n * @param {number} [options.coordinates=3] maximum number of coordinates (primarly used to remove z coordinates)\n * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)\n * @returns {GeoJSON} layer with truncated geometry\n * @example\n * var point = turf.point([\n * 70.46923055566859,\n * 58.11088890802906,\n * 1508\n * ]);\n * var options = {precision: 3, coordinates: 2};\n * var truncated = turf.truncate(point, options);\n * //=truncated.geometry.coordinates => [70.469, 58.111]\n *\n * //addToMap\n * var addToMap = [truncated];\n */\nfunction truncate<T extends AllGeoJSON>(\n geojson: T,\n options?: {\n precision?: number;\n coordinates?: number;\n mutate?: boolean;\n }\n): T {\n // Optional parameters\n options = options ?? {};\n if (!isObject(options)) throw new Error(\"options is invalid\");\n var precision = options.precision;\n var coordinates = options.coordinates;\n var mutate = options.mutate;\n\n // default params\n precision =\n precision === undefined || precision === null || isNaN(precision)\n ? 6\n : precision;\n coordinates =\n coordinates === undefined || coordinates === null || isNaN(coordinates)\n ? 3\n : coordinates;\n\n // validation\n if (!geojson) throw new Error(\"<geojson> is required\");\n if (typeof precision !== \"number\")\n throw new Error(\"<precision> must be a number\");\n if (typeof coordinates !== \"number\")\n throw new Error(\"<coordinates> must be a number\");\n\n // prevent input mutation\n if (mutate === false || mutate === undefined)\n geojson = JSON.parse(JSON.stringify(geojson));\n\n var factor = Math.pow(10, precision);\n\n // Truncate Coordinates\n coordEach(geojson, function (coords) {\n truncateCoords(coords, factor, coordinates!);\n });\n return geojson;\n}\n\n/**\n * Truncate Coordinates - Mutates coordinates in place\n *\n * @private\n * @param {Array<any>} coords Geometry Coordinates\n * @param {number} factor rounding factor for coordinate decimal precision\n * @param {number} coordinates maximum number of coordinates (primarly used to remove z coordinates)\n * @returns {Array<any>} mutated coordinates\n */\nfunction truncateCoords(coords: number[], factor: number, coordinates: number) {\n // Remove extra coordinates (usually elevation coordinates and more)\n if (coords.length > coordinates) coords.splice(coordinates, coords.length);\n\n // Truncate coordinate decimals\n for (var i = 0; i < coords.length; i++) {\n coords[i] = Math.round(coords[i] * factor) / factor;\n }\n return coords;\n}\n\nexport { truncate };\nexport default truncate;\n"]}
@@ -0,0 +1,32 @@
1
+ import { AllGeoJSON } from '@turf/helpers';
2
+
3
+ /**
4
+ * Takes a GeoJSON Feature or FeatureCollection and truncates the precision of the geometry.
5
+ *
6
+ * @name truncate
7
+ * @param {GeoJSON} geojson any GeoJSON Feature, FeatureCollection, Geometry or GeometryCollection.
8
+ * @param {Object} [options={}] Optional parameters
9
+ * @param {number} [options.precision=6] coordinate decimal precision
10
+ * @param {number} [options.coordinates=3] maximum number of coordinates (primarly used to remove z coordinates)
11
+ * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
12
+ * @returns {GeoJSON} layer with truncated geometry
13
+ * @example
14
+ * var point = turf.point([
15
+ * 70.46923055566859,
16
+ * 58.11088890802906,
17
+ * 1508
18
+ * ]);
19
+ * var options = {precision: 3, coordinates: 2};
20
+ * var truncated = turf.truncate(point, options);
21
+ * //=truncated.geometry.coordinates => [70.469, 58.111]
22
+ *
23
+ * //addToMap
24
+ * var addToMap = [truncated];
25
+ */
26
+ declare function truncate<T extends AllGeoJSON>(geojson: T, options?: {
27
+ precision?: number;
28
+ coordinates?: number;
29
+ mutate?: boolean;
30
+ }): T;
31
+
32
+ export { truncate as default, truncate };
@@ -1,4 +1,5 @@
1
- import { AllGeoJSON } from "@turf/helpers";
1
+ import { AllGeoJSON } from '@turf/helpers';
2
+
2
3
  /**
3
4
  * Takes a GeoJSON Feature or FeatureCollection and truncates the precision of the geometry.
4
5
  *
@@ -27,4 +28,5 @@ declare function truncate<T extends AllGeoJSON>(geojson: T, options?: {
27
28
  coordinates?: number;
28
29
  mutate?: boolean;
29
30
  }): T;
30
- export default truncate;
31
+
32
+ export { truncate as default, truncate };
@@ -0,0 +1,40 @@
1
+ // index.ts
2
+ import { coordEach } from "@turf/meta";
3
+ import { isObject } from "@turf/helpers";
4
+ function truncate(geojson, options) {
5
+ options = options != null ? options : {};
6
+ if (!isObject(options))
7
+ throw new Error("options is invalid");
8
+ var precision = options.precision;
9
+ var coordinates = options.coordinates;
10
+ var mutate = options.mutate;
11
+ precision = precision === void 0 || precision === null || isNaN(precision) ? 6 : precision;
12
+ coordinates = coordinates === void 0 || coordinates === null || isNaN(coordinates) ? 3 : coordinates;
13
+ if (!geojson)
14
+ throw new Error("<geojson> is required");
15
+ if (typeof precision !== "number")
16
+ throw new Error("<precision> must be a number");
17
+ if (typeof coordinates !== "number")
18
+ throw new Error("<coordinates> must be a number");
19
+ if (mutate === false || mutate === void 0)
20
+ geojson = JSON.parse(JSON.stringify(geojson));
21
+ var factor = Math.pow(10, precision);
22
+ coordEach(geojson, function(coords) {
23
+ truncateCoords(coords, factor, coordinates);
24
+ });
25
+ return geojson;
26
+ }
27
+ function truncateCoords(coords, factor, coordinates) {
28
+ if (coords.length > coordinates)
29
+ coords.splice(coordinates, coords.length);
30
+ for (var i = 0; i < coords.length; i++) {
31
+ coords[i] = Math.round(coords[i] * factor) / factor;
32
+ }
33
+ return coords;
34
+ }
35
+ var turf_truncate_default = truncate;
36
+ export {
37
+ turf_truncate_default as default,
38
+ truncate
39
+ };
40
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../index.ts"],"sourcesContent":["import { coordEach } from \"@turf/meta\";\nimport { AllGeoJSON, isObject } from \"@turf/helpers\";\n\n/**\n * Takes a GeoJSON Feature or FeatureCollection and truncates the precision of the geometry.\n *\n * @name truncate\n * @param {GeoJSON} geojson any GeoJSON Feature, FeatureCollection, Geometry or GeometryCollection.\n * @param {Object} [options={}] Optional parameters\n * @param {number} [options.precision=6] coordinate decimal precision\n * @param {number} [options.coordinates=3] maximum number of coordinates (primarly used to remove z coordinates)\n * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)\n * @returns {GeoJSON} layer with truncated geometry\n * @example\n * var point = turf.point([\n * 70.46923055566859,\n * 58.11088890802906,\n * 1508\n * ]);\n * var options = {precision: 3, coordinates: 2};\n * var truncated = turf.truncate(point, options);\n * //=truncated.geometry.coordinates => [70.469, 58.111]\n *\n * //addToMap\n * var addToMap = [truncated];\n */\nfunction truncate<T extends AllGeoJSON>(\n geojson: T,\n options?: {\n precision?: number;\n coordinates?: number;\n mutate?: boolean;\n }\n): T {\n // Optional parameters\n options = options ?? {};\n if (!isObject(options)) throw new Error(\"options is invalid\");\n var precision = options.precision;\n var coordinates = options.coordinates;\n var mutate = options.mutate;\n\n // default params\n precision =\n precision === undefined || precision === null || isNaN(precision)\n ? 6\n : precision;\n coordinates =\n coordinates === undefined || coordinates === null || isNaN(coordinates)\n ? 3\n : coordinates;\n\n // validation\n if (!geojson) throw new Error(\"<geojson> is required\");\n if (typeof precision !== \"number\")\n throw new Error(\"<precision> must be a number\");\n if (typeof coordinates !== \"number\")\n throw new Error(\"<coordinates> must be a number\");\n\n // prevent input mutation\n if (mutate === false || mutate === undefined)\n geojson = JSON.parse(JSON.stringify(geojson));\n\n var factor = Math.pow(10, precision);\n\n // Truncate Coordinates\n coordEach(geojson, function (coords) {\n truncateCoords(coords, factor, coordinates!);\n });\n return geojson;\n}\n\n/**\n * Truncate Coordinates - Mutates coordinates in place\n *\n * @private\n * @param {Array<any>} coords Geometry Coordinates\n * @param {number} factor rounding factor for coordinate decimal precision\n * @param {number} coordinates maximum number of coordinates (primarly used to remove z coordinates)\n * @returns {Array<any>} mutated coordinates\n */\nfunction truncateCoords(coords: number[], factor: number, coordinates: number) {\n // Remove extra coordinates (usually elevation coordinates and more)\n if (coords.length > coordinates) coords.splice(coordinates, coords.length);\n\n // Truncate coordinate decimals\n for (var i = 0; i < coords.length; i++) {\n coords[i] = Math.round(coords[i] * factor) / factor;\n }\n return coords;\n}\n\nexport { truncate };\nexport default truncate;\n"],"mappings":";AAAA,SAAS,iBAAiB;AAC1B,SAAqB,gBAAgB;AAyBrC,SAAS,SACP,SACA,SAKG;AAEH,YAAU,4BAAW,CAAC;AACtB,MAAI,CAAC,SAAS,OAAO;AAAG,UAAM,IAAI,MAAM,oBAAoB;AAC5D,MAAI,YAAY,QAAQ;AACxB,MAAI,cAAc,QAAQ;AAC1B,MAAI,SAAS,QAAQ;AAGrB,cACE,cAAc,UAAa,cAAc,QAAQ,MAAM,SAAS,IAC5D,IACA;AACN,gBACE,gBAAgB,UAAa,gBAAgB,QAAQ,MAAM,WAAW,IAClE,IACA;AAGN,MAAI,CAAC;AAAS,UAAM,IAAI,MAAM,uBAAuB;AACrD,MAAI,OAAO,cAAc;AACvB,UAAM,IAAI,MAAM,8BAA8B;AAChD,MAAI,OAAO,gBAAgB;AACzB,UAAM,IAAI,MAAM,gCAAgC;AAGlD,MAAI,WAAW,SAAS,WAAW;AACjC,cAAU,KAAK,MAAM,KAAK,UAAU,OAAO,CAAC;AAE9C,MAAI,SAAS,KAAK,IAAI,IAAI,SAAS;AAGnC,YAAU,SAAS,SAAU,QAAQ;AACnC,mBAAe,QAAQ,QAAQ,WAAY;AAAA,EAC7C,CAAC;AACD,SAAO;AACT;AAWA,SAAS,eAAe,QAAkB,QAAgB,aAAqB;AAE7E,MAAI,OAAO,SAAS;AAAa,WAAO,OAAO,aAAa,OAAO,MAAM;AAGzE,WAAS,IAAI,GAAG,IAAI,OAAO,QAAQ,KAAK;AACtC,WAAO,CAAC,IAAI,KAAK,MAAM,OAAO,CAAC,IAAI,MAAM,IAAI;AAAA,EAC/C;AACA,SAAO;AACT;AAGA,IAAO,wBAAQ;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turf/truncate",
3
- "version": "7.0.0-alpha.2",
3
+ "version": "7.1.0-alpha.7+0ce6ecca0",
4
4
  "description": "turf truncate module",
5
5
  "author": "Turf Authors",
6
6
  "contributors": [
@@ -25,46 +25,51 @@
25
25
  "gis",
26
26
  "truncate"
27
27
  ],
28
- "main": "dist/js/index.js",
29
- "module": "dist/es/index.js",
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
- "types": "./dist/js/index.d.ts",
34
- "import": "./dist/es/index.js",
35
- "require": "./dist/js/index.js"
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.js",
45
- "build": "npm-run-all build:*",
46
- "build:es": "tsc --outDir dist/es --module esnext --declaration false && echo '{\"type\":\"module\"}' > dist/es/package.json",
47
- "build:js": "tsc",
48
- "docs": "tsx ../../scripts/generate-readmes",
49
- "test": "npm-run-all 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
- "@types/tape": "*",
55
- "benchmark": "*",
56
- "load-json-file": "*",
57
- "npm-run-all": "*",
58
- "tape": "*",
59
- "tslint": "*",
60
- "tsx": "*",
61
- "typescript": "*",
62
- "write-json-file": "*"
58
+ "@types/benchmark": "^2.1.5",
59
+ "@types/tape": "^4.2.32",
60
+ "benchmark": "^2.1.4",
61
+ "load-json-file": "^7.0.1",
62
+ "npm-run-all": "^4.1.5",
63
+ "tape": "^5.7.2",
64
+ "tsup": "^8.0.1",
65
+ "tsx": "^4.6.2",
66
+ "typescript": "^5.2.2",
67
+ "write-json-file": "^5.0.0"
63
68
  },
64
69
  "dependencies": {
65
- "@turf/helpers": "^7.0.0-alpha.2",
66
- "@turf/meta": "^7.0.0-alpha.2",
67
- "tslib": "^2.3.0"
70
+ "@turf/helpers": "^7.1.0-alpha.7+0ce6ecca0",
71
+ "@turf/meta": "^7.1.0-alpha.7+0ce6ecca0",
72
+ "tslib": "^2.6.2"
68
73
  },
69
- "gitHead": "dd35b52725945b4fa29a98d9a550733e06cc222e"
74
+ "gitHead": "0ce6ecca05829690270fec6d6bed2003495fe0ea"
70
75
  }
package/dist/es/index.js DELETED
@@ -1,75 +0,0 @@
1
- import { coordEach } from "@turf/meta";
2
- /**
3
- * Takes a GeoJSON Feature or FeatureCollection and truncates the precision of the geometry.
4
- *
5
- * @name truncate
6
- * @param {GeoJSON} geojson any GeoJSON Feature, FeatureCollection, Geometry or GeometryCollection.
7
- * @param {Object} [options={}] Optional parameters
8
- * @param {number} [options.precision=6] coordinate decimal precision
9
- * @param {number} [options.coordinates=3] maximum number of coordinates (primarly used to remove z coordinates)
10
- * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
11
- * @returns {GeoJSON} layer with truncated geometry
12
- * @example
13
- * var point = turf.point([
14
- * 70.46923055566859,
15
- * 58.11088890802906,
16
- * 1508
17
- * ]);
18
- * var options = {precision: 3, coordinates: 2};
19
- * var truncated = turf.truncate(point, options);
20
- * //=truncated.geometry.coordinates => [70.469, 58.111]
21
- *
22
- * //addToMap
23
- * var addToMap = [truncated];
24
- */
25
- function truncate(geojson, options = {}) {
26
- // Optional parameters
27
- var precision = options.precision;
28
- var coordinates = options.coordinates;
29
- var mutate = options.mutate;
30
- // default params
31
- precision =
32
- precision === undefined || precision === null || isNaN(precision)
33
- ? 6
34
- : precision;
35
- coordinates =
36
- coordinates === undefined || coordinates === null || isNaN(coordinates)
37
- ? 3
38
- : coordinates;
39
- // validation
40
- if (!geojson)
41
- throw new Error("<geojson> is required");
42
- if (typeof precision !== "number")
43
- throw new Error("<precision> must be a number");
44
- if (typeof coordinates !== "number")
45
- throw new Error("<coordinates> must be a number");
46
- // prevent input mutation
47
- if (mutate === false || mutate === undefined)
48
- geojson = JSON.parse(JSON.stringify(geojson));
49
- var factor = Math.pow(10, precision);
50
- // Truncate Coordinates
51
- coordEach(geojson, function (coords) {
52
- truncateCoords(coords, factor, coordinates);
53
- });
54
- return geojson;
55
- }
56
- /**
57
- * Truncate Coordinates - Mutates coordinates in place
58
- *
59
- * @private
60
- * @param {Array<any>} coords Geometry Coordinates
61
- * @param {number} factor rounding factor for coordinate decimal precision
62
- * @param {number} coordinates maximum number of coordinates (primarly used to remove z coordinates)
63
- * @returns {Array<any>} mutated coordinates
64
- */
65
- function truncateCoords(coords, factor, coordinates) {
66
- // Remove extra coordinates (usually elevation coordinates and more)
67
- if (coords.length > coordinates)
68
- coords.splice(coordinates, coords.length);
69
- // Truncate coordinate decimals
70
- for (var i = 0; i < coords.length; i++) {
71
- coords[i] = Math.round(coords[i] * factor) / factor;
72
- }
73
- return coords;
74
- }
75
- export default truncate;
@@ -1 +0,0 @@
1
- {"type":"module"}
package/dist/js/index.js DELETED
@@ -1,77 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const meta_1 = require("@turf/meta");
4
- /**
5
- * Takes a GeoJSON Feature or FeatureCollection and truncates the precision of the geometry.
6
- *
7
- * @name truncate
8
- * @param {GeoJSON} geojson any GeoJSON Feature, FeatureCollection, Geometry or GeometryCollection.
9
- * @param {Object} [options={}] Optional parameters
10
- * @param {number} [options.precision=6] coordinate decimal precision
11
- * @param {number} [options.coordinates=3] maximum number of coordinates (primarly used to remove z coordinates)
12
- * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
13
- * @returns {GeoJSON} layer with truncated geometry
14
- * @example
15
- * var point = turf.point([
16
- * 70.46923055566859,
17
- * 58.11088890802906,
18
- * 1508
19
- * ]);
20
- * var options = {precision: 3, coordinates: 2};
21
- * var truncated = turf.truncate(point, options);
22
- * //=truncated.geometry.coordinates => [70.469, 58.111]
23
- *
24
- * //addToMap
25
- * var addToMap = [truncated];
26
- */
27
- function truncate(geojson, options = {}) {
28
- // Optional parameters
29
- var precision = options.precision;
30
- var coordinates = options.coordinates;
31
- var mutate = options.mutate;
32
- // default params
33
- precision =
34
- precision === undefined || precision === null || isNaN(precision)
35
- ? 6
36
- : precision;
37
- coordinates =
38
- coordinates === undefined || coordinates === null || isNaN(coordinates)
39
- ? 3
40
- : coordinates;
41
- // validation
42
- if (!geojson)
43
- throw new Error("<geojson> is required");
44
- if (typeof precision !== "number")
45
- throw new Error("<precision> must be a number");
46
- if (typeof coordinates !== "number")
47
- throw new Error("<coordinates> must be a number");
48
- // prevent input mutation
49
- if (mutate === false || mutate === undefined)
50
- geojson = JSON.parse(JSON.stringify(geojson));
51
- var factor = Math.pow(10, precision);
52
- // Truncate Coordinates
53
- meta_1.coordEach(geojson, function (coords) {
54
- truncateCoords(coords, factor, coordinates);
55
- });
56
- return geojson;
57
- }
58
- /**
59
- * Truncate Coordinates - Mutates coordinates in place
60
- *
61
- * @private
62
- * @param {Array<any>} coords Geometry Coordinates
63
- * @param {number} factor rounding factor for coordinate decimal precision
64
- * @param {number} coordinates maximum number of coordinates (primarly used to remove z coordinates)
65
- * @returns {Array<any>} mutated coordinates
66
- */
67
- function truncateCoords(coords, factor, coordinates) {
68
- // Remove extra coordinates (usually elevation coordinates and more)
69
- if (coords.length > coordinates)
70
- coords.splice(coordinates, coords.length);
71
- // Truncate coordinate decimals
72
- for (var i = 0; i < coords.length; i++) {
73
- coords[i] = Math.round(coords[i] * factor) / factor;
74
- }
75
- return coords;
76
- }
77
- exports.default = truncate;