Skip to content

Commit

Permalink
Migrating remaining JS modules to Typescript (part 1) (#2543)
Browse files Browse the repository at this point in the history
* Converting turf-ellipse to typescript.

* Converting turf-flip to typescript.

* Forgot to remove redundant turf-flip index.d.ts

* Converting turf-flatten to typescript.

* Converting turf-explode to typescript.

* Converting turf-envelope to typescript.

* Converting turf-dissolve to Typescript. Missed a couple of changes to turf-flip package.json in an earlier commit.

* Converting turf-rewind to Typescript.

* Converting turf-tag to Typescript.

* Converting turf-sample to Typescript. Forgot to delete d.ts from turf-tag during last commit.

* Converting turf-point-on-feature to Typescript.

* Converting turf-points-within-polygon to Typescript. One type test isn't working just yet (mixed Points and MultiPoints). Need to confirm this is actually valid use of the function.

* Converting turf-polygon-smooth to Typescript.

* Revisiting turf-points-within-polygon as it needs to be able to accept a mixture of Point and MultiPoint features.

* Converting turf-polygon-tangents to Typescript. Few oddities uncovered here. First was that an unnecessary parameter (polygon) was being passed to processPolygon. Second was enext param passed into processPolygon was simply being overwritten and not returned - converted to a local const. Noting in history in case a future developer is chasing a bug.

* Converting turf-sector to Typescript. Forgot to remove d.ts from turf-polygon-tangents in a prior commit.

* Converting turf-shortest-path to Typescript. Adding type test step into turf-sector (test existed, just weren't being run).

* Converting turf-simplify to Typescript.

* Converting turf-difference to Typescript.

* Removing rollup from freshly converted typescript modules. Lots of minor monorepolint fixes. Overloading a few function signatures so types.ts test pass. Fixing a few places where option parsing / defaults was half baked. Fixing a couple of modules with built in libs that were building locally but not when doing the final turf CDN build.

* Removing minDistance param from turf-shortest-path per this convo #2538 (comment) Changing options handling to not change the options object in place. Removing a couple of missed d.ts files.

* Incorporating review feedback, including: subbing ?? for || in option handling in a few places, fixing overloaded function typedefs, reinstating some incorrectly removed runtime checks, adding license details for types we pulled in for locally hosted libraries.
  • Loading branch information
smallsaucepan authored Nov 28, 2023
1 parent 407619b commit 66f9b2d
Show file tree
Hide file tree
Showing 76 changed files with 934 additions and 676 deletions.
2 changes: 1 addition & 1 deletion packages/turf-angle/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require("path");
const { glob } = require("glob");
const { loadJsonFileSync } = require("load-json-file");
const { writeJsonFileSync } = require("write-json-file");
const sector = require("@turf/sector");
const sector = require("@turf/sector").default;
const bearing = require("@turf/bearing").default;
const truncate = require("@turf/truncate").default;
const distance = require("@turf/distance").default;
Expand Down
8 changes: 0 additions & 8 deletions packages/turf-difference/index.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import polygonClipping from "polygon-clipping";
import { Polygon, MultiPolygon, Feature, FeatureCollection } from "geojson";
import polygonClipping, { Geom } from "polygon-clipping";
import { polygon, multiPolygon } from "@turf/helpers";
import { geomEach } from "@turf/meta";

Expand Down Expand Up @@ -35,20 +36,22 @@ import { geomEach } from "@turf/meta";
* //addToMap
* var addToMap = [polygon1, polygon2, difference];
*/
function difference(features) {
const geoms = [];
function difference(
features: FeatureCollection<Polygon | MultiPolygon>
): Feature<Polygon | MultiPolygon> | null {
const geoms: Array<Geom> = [];

geomEach(features, (geom) => {
geoms.push(geom.coordinates);
geoms.push(geom.coordinates as Geom);
});

if (geoms.length < 2) {
throw new Error("Must have at least two features");
}

var properties = features.features[0].properties || {};
const properties = features.features[0].properties || {};

var differenced = polygonClipping.difference(geoms[0], ...geoms.slice(1));
const differenced = polygonClipping.difference(geoms[0], ...geoms.slice(1));
if (differenced.length === 0) return null;
if (differenced.length === 1) return polygon(differenced[0], properties);
return multiPolygon(differenced, properties);
Expand Down
17 changes: 10 additions & 7 deletions packages/turf-difference/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,40 @@
"exports": {
"./package.json": "./package.json",
".": {
"types": "./index.d.ts",
"types": "./dist/js/index.d.ts",
"import": "./dist/es/index.js",
"require": "./dist/js/index.js"
}
},
"types": "index.d.ts",
"types": "dist/js/index.d.ts",
"sideEffects": false,
"files": [
"dist",
"index.d.ts"
"dist"
],
"scripts": {
"bench": "tsx bench.js",
"build": "rollup -c ../../rollup.config.js && echo '{\"type\":\"module\"}' > dist/es/package.json",
"build": "npm-run-all --npm-path npm build:*",
"build:es": "tsc --outDir dist/es --module esnext --declaration false && echo '{\"type\":\"module\"}' > dist/es/package.json",
"build:js": "tsc",
"docs": "tsx ../../scripts/generate-readmes.ts",
"test": "npm-run-all --npm-path npm test:*",
"test:tape": "tsx test.js"
},
"devDependencies": {
"@types/tape": "^4.2.32",
"benchmark": "^2.1.4",
"glob": "^10.3.10",
"load-json-file": "^7.0.1",
"npm-run-all": "^4.1.5",
"rollup": "^2.79.1",
"tape": "^5.7.2",
"tsx": "^3.14.0",
"typescript": "^5.2.2",
"write-json-file": "^5.0.0"
},
"dependencies": {
"@turf/helpers": "^7.0.0-alpha.2",
"@turf/meta": "^7.0.0-alpha.2",
"polygon-clipping": "^0.15.3"
"polygon-clipping": "^0.15.3",
"tslib": "^2.6.2"
}
}
7 changes: 7 additions & 0 deletions packages/turf-difference/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.shared.json",
"compilerOptions": {
"outDir": "dist/js"
},
"files": ["index.ts"]
}
2 changes: 1 addition & 1 deletion packages/turf-directional-mean/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default function directionalMean(
} = {}
): DirectionalMeanLine {
const isPlanar = !!options.planar; // you can't use options.planar || true here.
const isSegment: boolean = options.segment || false;
const isSegment: boolean = options.segment ?? false;
let sigmaSin = 0;
let sigmaCos = 0;
let countOfLines = 0;
Expand Down
11 changes: 0 additions & 11 deletions packages/turf-dissolve/index.d.ts

This file was deleted.

62 changes: 39 additions & 23 deletions packages/turf-dissolve/index.js → packages/turf-dissolve/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { featureCollection, multiPolygon, isObject } from "@turf/helpers";
import { Feature, FeatureCollection, Polygon } from "geojson";
import { featureCollection, isObject, multiPolygon } from "@turf/helpers";
import { collectionOf } from "@turf/invariant";
import { featureEach } from "@turf/meta";
import flatten from "@turf/flatten";
import polygonClipping from "polygon-clipping";
import polygonClipping, { Geom } from "polygon-clipping";

/**
* Dissolves a FeatureCollection of {@link polygon} features, filtered by an optional property name:value.
Expand All @@ -25,53 +26,68 @@ import polygonClipping from "polygon-clipping";
* //addToMap
* var addToMap = [features, dissolved]
*/
function dissolve(fc, options) {
function dissolve(
fc: FeatureCollection<Polygon>,
options: {
propertyName?: string;
} = {}
): FeatureCollection<Polygon> {
// Optional parameters
options = options || {};
if (!isObject(options)) throw new Error("options is invalid");
var propertyName = options.propertyName;
const { propertyName } = options;

// Input validation
collectionOf(fc, "Polygon", "dissolve");

// Main
var outFeatures = [];
if (!options.propertyName) {
const outFeatures = [];
if (!propertyName) {
return flatten(
multiPolygon(
polygonClipping.union.apply(
null,
// List of polygons expressed as Position[][][] a.k.a. Geom[]
fc.features.map(function (f) {
return f.geometry.coordinates;
})
}) as [Geom, ...Geom[]]
)
)
);
} else {
var uniquePropertyVals = {};
// Group polygons by the value of their property named by propertyName
const uniquePropertyVals: { [key: string]: Feature[] } = {};
featureEach(fc, function (feature) {
if (
!Object.prototype.hasOwnProperty.call(
uniquePropertyVals,
feature.properties[propertyName]
)
) {
uniquePropertyVals[feature.properties[propertyName]] = [];
if (feature.properties) {
if (
!Object.prototype.hasOwnProperty.call(
uniquePropertyVals,
feature.properties[propertyName]
)
) {
uniquePropertyVals[feature.properties[propertyName]] =
[] as Feature[];
}
uniquePropertyVals[feature.properties[propertyName]].push(feature);
}
uniquePropertyVals[feature.properties[propertyName]].push(feature);
});
var vals = Object.keys(uniquePropertyVals);
for (var i = 0; i < vals.length; i++) {
var mp = multiPolygon(
const vals = Object.keys(uniquePropertyVals);

// Export each group of polygons as a separate feature.
for (let i = 0; i < vals.length; i++) {
const mp = multiPolygon(
polygonClipping.union.apply(
null,
uniquePropertyVals[vals[i]].map(function (f) {
// List of polygons expressed as Position[][][] a.k.a. Geom[]
(uniquePropertyVals[vals[i]] as Feature<Polygon>[]).map(function (f) {
return f.geometry.coordinates;
})
}) as [Geom, ...Geom[]]
)
);
mp.properties[propertyName] = vals[i];
outFeatures.push(mp);
if (mp && mp.properties) {
mp.properties[propertyName] = vals[i];
outFeatures.push(mp);
}
}
}

Expand Down
17 changes: 10 additions & 7 deletions packages/turf-dissolve/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,41 @@
"exports": {
"./package.json": "./package.json",
".": {
"types": "./index.d.ts",
"types": "./dist/js/index.d.ts",
"import": "./dist/es/index.js",
"require": "./dist/js/index.js"
}
},
"types": "index.d.ts",
"types": "dist/js/index.d.ts",
"sideEffects": false,
"files": [
"dist",
"index.d.ts"
"dist"
],
"scripts": {
"bench": "tsx bench.js",
"build": "rollup -c ../../rollup.config.js && echo '{\"type\":\"module\"}' > dist/es/package.json",
"build": "npm-run-all --npm-path npm build:*",
"build:es": "tsc --outDir dist/es --module esnext --declaration false && echo '{\"type\":\"module\"}' > dist/es/package.json",
"build:js": "tsc",
"docs": "tsx ../../scripts/generate-readmes.ts",
"test": "npm-run-all --npm-path npm test:*",
"test:tape": "tsx test.js"
},
"devDependencies": {
"@types/tape": "^4.2.32",
"benchmark": "^2.1.4",
"load-json-file": "^7.0.1",
"npm-run-all": "^4.1.5",
"rollup": "^2.79.1",
"tape": "^5.7.2",
"tsx": "^3.14.0",
"typescript": "^5.2.2",
"write-json-file": "^5.0.0"
},
"dependencies": {
"@turf/flatten": "^7.0.0-alpha.2",
"@turf/helpers": "^7.0.0-alpha.2",
"@turf/invariant": "^7.0.0-alpha.2",
"@turf/meta": "^7.0.0-alpha.2",
"polygon-clipping": "^0.15.3"
"polygon-clipping": "^0.15.3",
"tslib": "^2.6.2"
}
}
7 changes: 7 additions & 0 deletions packages/turf-dissolve/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.shared.json",
"compilerOptions": {
"outDir": "dist/js"
},
"files": ["index.ts"]
}
4 changes: 2 additions & 2 deletions packages/turf-distance-weight/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ export default function distanceWeight(
options = options || {};
const threshold = options.threshold || 10000;
const p = options.p || 2;
const binary = options.binary || false;
const binary = options.binary ?? false;
const alpha = options.alpha || -1;
const rowTransform = options.standardization || false;
const rowTransform = options.standardization ?? false;

const features: Array<Feature<Point>> = [];
featureEach(fc, (feature) => {
Expand Down
29 changes: 0 additions & 29 deletions packages/turf-ellipse/index.d.ts

This file was deleted.

Loading

0 comments on commit 66f9b2d

Please sign in to comment.