Skip to content

Migrating remaining JS modules to Typescript (part 1) #2543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
4057efb
Converting turf-ellipse to typescript.
smallsaucepan Nov 12, 2023
0aa0ba3
Converting turf-flip to typescript.
smallsaucepan Nov 12, 2023
8a6b3c0
Forgot to remove redundant turf-flip index.d.ts
smallsaucepan Nov 12, 2023
5065c14
Converting turf-flatten to typescript.
smallsaucepan Nov 12, 2023
c582eb0
Converting turf-explode to typescript.
smallsaucepan Nov 12, 2023
2ada574
Converting turf-envelope to typescript.
smallsaucepan Nov 12, 2023
53f8495
Converting turf-dissolve to Typescript. Missed a couple of changes to…
smallsaucepan Nov 15, 2023
8ed7bc3
Converting turf-rewind to Typescript.
smallsaucepan Nov 16, 2023
27826c6
Converting turf-tag to Typescript.
smallsaucepan Nov 16, 2023
0b7ac3a
Converting turf-sample to Typescript. Forgot to delete d.ts from turf…
smallsaucepan Nov 16, 2023
a988eb2
Converting turf-point-on-feature to Typescript.
smallsaucepan Nov 17, 2023
83c9877
Converting turf-points-within-polygon to Typescript. One type test is…
smallsaucepan Nov 17, 2023
e4c991b
Converting turf-polygon-smooth to Typescript.
smallsaucepan Nov 18, 2023
0752cb6
Revisiting turf-points-within-polygon as it needs to be able to accep…
smallsaucepan Nov 18, 2023
e44b26b
Converting turf-polygon-tangents to Typescript. Few oddities uncovere…
smallsaucepan Nov 18, 2023
4458c9c
Converting turf-sector to Typescript. Forgot to remove d.ts from turf…
smallsaucepan Nov 18, 2023
9234a47
Converting turf-shortest-path to Typescript. Adding type test step in…
smallsaucepan Nov 20, 2023
34b5fef
Converting turf-simplify to Typescript.
smallsaucepan Nov 20, 2023
228aed1
Converting turf-difference to Typescript.
smallsaucepan Nov 20, 2023
12f7452
Removing rollup from freshly converted typescript modules. Lots of mi…
smallsaucepan Nov 25, 2023
18f5a6e
Removing minDistance param from turf-shortest-path per this convo htt…
smallsaucepan Nov 25, 2023
1e85bee
Incorporating review feedback, including: subbing ?? for || in option…
smallsaucepan Nov 28, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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