From 584acbc287c7e43ca2af3c629fe543628aabc329 Mon Sep 17 00:00:00 2001 From: James Beard Date: Tue, 23 Jul 2024 20:37:05 +1000 Subject: [PATCH] Converted turf-unkink-polygon to Typescript (#2654) Converted to Typescript. Synced up JSDoc types with parameter types. Minor error in on of the JSDoc params type. --- packages/turf-unkink-polygon/README.md | 20 +++++++++-------- packages/turf-unkink-polygon/bench.ts | 9 +++++--- packages/turf-unkink-polygon/index.d.ts | 11 ---------- .../{index.js => index.ts} | 22 ++++++++++++------- .../lib/simplepolygon.d.ts | 8 +++++++ packages/turf-unkink-polygon/package.json | 5 ++++- packages/turf-unkink-polygon/test.ts | 13 +++++++---- pnpm-lock.yaml | 9 ++++++++ 8 files changed, 61 insertions(+), 36 deletions(-) delete mode 100644 packages/turf-unkink-polygon/index.d.ts rename packages/turf-unkink-polygon/{index.js => index.ts} (50%) create mode 100644 packages/turf-unkink-polygon/lib/simplepolygon.d.ts diff --git a/packages/turf-unkink-polygon/README.md b/packages/turf-unkink-polygon/README.md index f3f3a6c121..9a6d3eef04 100644 --- a/packages/turf-unkink-polygon/README.md +++ b/packages/turf-unkink-polygon/README.md @@ -4,35 +4,37 @@ ## unkinkPolygon -Takes a kinked polygon and returns a feature collection of polygons that have no kinks. +Takes a kinked polygon and returns a feature collection of polygons that have +no kinks. + Uses [simplepolygon][1] internally. ### Parameters -* `geojson` **([FeatureCollection][2] | [Feature][3]<([Polygon][4] | [MultiPolygon][5])>)** GeoJSON Polygon or MultiPolygon +* `geojson` **([FeatureCollection][2]<([Polygon][3] | [MultiPolygon][4])> | [Feature][5]<([Polygon][3] | [MultiPolygon][4])> | [Polygon][3] | [MultiPolygon][4])** polygons to unkink ### Examples ```javascript -var poly = turf.polygon([[[0, 0], [2, 0], [0, 2], [2, 2], [0, 0]]]); +const poly = turf.polygon([[[0, 0], [2, 0], [0, 2], [2, 2], [0, 0]]]); -var result = turf.unkinkPolygon(poly); +const result = turf.unkinkPolygon(poly); //addToMap -var addToMap = [poly, result] +const addToMap = [poly, result] ``` -Returns **[FeatureCollection][2]<[Polygon][4]>** Unkinked polygons +Returns **[FeatureCollection][2]<[Polygon][3]>** Unkinked polygons [1]: https://github.com/mclaeysb/simplepolygon [2]: https://tools.ietf.org/html/rfc7946#section-3.3 -[3]: https://tools.ietf.org/html/rfc7946#section-3.2 +[3]: https://tools.ietf.org/html/rfc7946#section-3.1.6 -[4]: https://tools.ietf.org/html/rfc7946#section-3.1.6 +[4]: https://tools.ietf.org/html/rfc7946#section-3.1.7 -[5]: https://tools.ietf.org/html/rfc7946#section-3.1.7 +[5]: https://tools.ietf.org/html/rfc7946#section-3.2 diff --git a/packages/turf-unkink-polygon/bench.ts b/packages/turf-unkink-polygon/bench.ts index b9689b6dee..ee37d01ee6 100644 --- a/packages/turf-unkink-polygon/bench.ts +++ b/packages/turf-unkink-polygon/bench.ts @@ -1,8 +1,9 @@ +import { Feature, FeatureCollection, Polygon } from "geojson"; import fs from "fs"; import path from "path"; import { fileURLToPath } from "url"; import { loadJsonFileSync } from "load-json-file"; -import Benchmark from "benchmark"; +import Benchmark, { Event } from "benchmark"; import { unkinkPolygon as unkink } from "./index.js"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); @@ -12,7 +13,9 @@ const directories = { }; const fixtures = fs.readdirSync(directories.in).map((filename) => { - return { filename, geojson: loadJsonFileSync(directories.in + filename) }; + const geojson: FeatureCollection | Feature = + loadJsonFileSync(directories.in + filename); + return { filename, geojson }; }); const suite = new Benchmark.Suite("unkink-polygon"); @@ -23,7 +26,7 @@ for (const fixture of fixtures) { } suite - .on("cycle", (event) => { + .on("cycle", (event: Event) => { console.log(String(event.target)); }) .run(); diff --git a/packages/turf-unkink-polygon/index.d.ts b/packages/turf-unkink-polygon/index.d.ts deleted file mode 100644 index a603833ae8..0000000000 --- a/packages/turf-unkink-polygon/index.d.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { Polygon, MultiPolygon, Feature, FeatureCollection } from "geojson"; - -/** - * http://turfjs.org/docs/#unkink-polygon - */ -declare function unkinkPolygon( - geojson: Feature | FeatureCollection | T -): FeatureCollection; - -export { unkinkPolygon }; -export default unkinkPolygon; diff --git a/packages/turf-unkink-polygon/index.js b/packages/turf-unkink-polygon/index.ts similarity index 50% rename from packages/turf-unkink-polygon/index.js rename to packages/turf-unkink-polygon/index.ts index 3a47207f12..44401c608a 100644 --- a/packages/turf-unkink-polygon/index.js +++ b/packages/turf-unkink-polygon/index.ts @@ -1,27 +1,33 @@ +import { Polygon, MultiPolygon, Feature, FeatureCollection } from "geojson"; import { flattenEach, featureEach } from "@turf/meta"; import { polygon, featureCollection } from "@turf/helpers"; import { simplepolygon } from "./lib/simplepolygon.js"; /** - * Takes a kinked polygon and returns a feature collection of polygons that have no kinks. + * Takes a kinked polygon and returns a feature collection of polygons that have + * no kinks. + * * Uses [simplepolygon](https://github.com/mclaeysb/simplepolygon) internally. * * @name unkinkPolygon - * @param {FeatureCollection|Feature} geojson GeoJSON Polygon or MultiPolygon + * @param {FeatureCollection|Feature|Polygon|MultiPolygon} geojson polygons to unkink * @returns {FeatureCollection} Unkinked polygons * @example - * var poly = turf.polygon([[[0, 0], [2, 0], [0, 2], [2, 2], [0, 0]]]); + * const poly = turf.polygon([[[0, 0], [2, 0], [0, 2], [2, 2], [0, 0]]]); * - * var result = turf.unkinkPolygon(poly); + * const result = turf.unkinkPolygon(poly); * * //addToMap - * var addToMap = [poly, result] + * const addToMap = [poly, result] */ -function unkinkPolygon(geojson) { - var features = []; +function unkinkPolygon( + geojson: Feature | FeatureCollection | T +): FeatureCollection { + var features: Feature[] = []; flattenEach(geojson, function (feature) { if (feature.geometry.type !== "Polygon") return; - featureEach(simplepolygon(feature), function (poly) { + // Safe to treat feature as Feature + featureEach(simplepolygon(feature as Feature), function (poly) { features.push(polygon(poly.geometry.coordinates, feature.properties)); }); }); diff --git a/packages/turf-unkink-polygon/lib/simplepolygon.d.ts b/packages/turf-unkink-polygon/lib/simplepolygon.d.ts new file mode 100644 index 0000000000..2ac093af32 --- /dev/null +++ b/packages/turf-unkink-polygon/lib/simplepolygon.d.ts @@ -0,0 +1,8 @@ +import { Polygon, Feature, FeatureCollection } from "geojson"; + +declare function simplepolygon( + feature: Feature +): FeatureCollection; + +export { simplepolygon }; +export default simplepolygon; diff --git a/packages/turf-unkink-polygon/package.json b/packages/turf-unkink-polygon/package.json index 360b0fb75b..6c365891ae 100644 --- a/packages/turf-unkink-polygon/package.json +++ b/packages/turf-unkink-polygon/package.json @@ -62,6 +62,7 @@ "tape": "^5.7.2", "tsup": "^8.0.1", "tsx": "^4.6.2", + "typescript": "^5.2.2", "write-json-file": "^5.0.0" }, "dependencies": { @@ -69,6 +70,8 @@ "@turf/boolean-point-in-polygon": "workspace:^", "@turf/helpers": "workspace:^", "@turf/meta": "workspace:^", - "rbush": "^3.0.1" + "@types/geojson": "7946.0.8", + "rbush": "^3.0.1", + "tslib": "^2.6.2" } } diff --git a/packages/turf-unkink-polygon/test.ts b/packages/turf-unkink-polygon/test.ts index c01034ef8b..addf8e6ddf 100644 --- a/packages/turf-unkink-polygon/test.ts +++ b/packages/turf-unkink-polygon/test.ts @@ -1,3 +1,4 @@ +import { Feature, FeatureCollection, Polygon } from "geojson"; import fs from "fs"; import path from "path"; import { fileURLToPath } from "url"; @@ -17,7 +18,9 @@ const directories = { }; const fixtures = fs.readdirSync(directories.in).map((filename) => { - return { filename, geojson: loadJsonFileSync(directories.in + filename) }; + const geojson: FeatureCollection | Feature = + loadJsonFileSync(directories.in + filename); + return { filename, geojson }; }); test("unkink-polygon", (t) => { @@ -55,7 +58,7 @@ test("issue #2504", (t) => { t.pass( "large number of coordinates in a single ring should not cause an error" ); - } catch (e) { + } catch (e: any) { t.fail(e); } @@ -68,17 +71,19 @@ test("unkink-polygon -- throws", (t) => { t.true(value !== "isUnique", "isUnique"); t.true(value !== "getUnique", "getUnique"); } + // @ts-expect-error intentional non-existent function t.throws(() => Array.isUnique(), "isUnique()"); + // @ts-expect-error intentional non-existent function t.throws(() => Array.getUnique(), "getUnique()"); t.end(); }); function colorize( - features, + features: FeatureCollection, colors = ["#F00", "#00F", "#0F0", "#F0F", "#FFF"], width = 6 ) { - const results = []; + const results: Feature[] = []; featureEach(features, (feature, index) => { const color = colors[index % colors.length]; feature.properties = Object.assign( diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c4cbf6a0bb..8b4eab03a8 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -6120,9 +6120,15 @@ importers: '@turf/meta': specifier: workspace:^ version: link:../turf-meta + '@types/geojson': + specifier: 7946.0.8 + version: 7946.0.8 rbush: specifier: ^3.0.1 version: 3.0.1 + tslib: + specifier: ^2.6.2 + version: 2.6.2 devDependencies: '@turf/kinks': specifier: workspace:^ @@ -6151,6 +6157,9 @@ importers: tsx: specifier: ^4.6.2 version: 4.6.2 + typescript: + specifier: ^5.2.2 + version: 5.3.3 write-json-file: specifier: ^5.0.0 version: 5.0.0